@@ -446,12 +446,14 @@ static ggml_tallocr_t node_tallocr(ggml_gallocr_t galloc, struct ggml_tensor * n
446446 return galloc -> hash_allocs [ggml_hash_find_or_insert (galloc -> hash_set , node )];
447447}
448448
449- static void init_view (ggml_gallocr_t galloc , struct ggml_tensor * view ) {
449+ static void init_view (ggml_gallocr_t galloc , struct ggml_tensor * view , bool update_backend ) {
450450 ggml_tallocr_t alloc = node_tallocr (galloc , view );
451451
452452 //printf("init_view: %s from src %s\n", view->name, view->view_src->name);
453453 GGML_ASSERT (view -> view_src != NULL && view -> view_src -> data != NULL );
454- view -> backend = view -> view_src -> backend ;
454+ if (update_backend ) {
455+ view -> backend = view -> view_src -> backend ;
456+ }
455457 view -> buffer = view -> view_src -> buffer ;
456458 view -> data = (char * )view -> view_src -> data + view -> view_offs ;
457459
@@ -469,7 +471,7 @@ static void allocate_node(ggml_gallocr_t galloc, struct ggml_tensor * node) {
469471
470472 if (node -> data == NULL ) {
471473 if (ggml_is_view (node )) {
472- init_view (galloc , node );
474+ init_view (galloc , node , true );
473475 } else {
474476 // see if we can reuse a parent's buffer (inplace)
475477 if (ggml_op_can_inplace (node -> op )) {
@@ -499,15 +501,14 @@ static void allocate_node(ggml_gallocr_t galloc, struct ggml_tensor * node) {
499501 AT_PRINTF ("reusing view parent %s (%s) for %s\n" , parent -> name , view_src -> name , node -> name );
500502 node -> view_src = view_src ;
501503 view_src_hn -> n_views += 1 ;
502- init_view (galloc , node );
504+ init_view (galloc , node , false );
503505 return ;
504506 }
505- }
506- else {
507+ } else {
507508 AT_PRINTF ("reusing parent %s for %s\n" , parent -> name , node -> name );
508509 node -> view_src = parent ;
509510 p_hn -> n_views += 1 ;
510- init_view (galloc , node );
511+ init_view (galloc , node , false );
511512 return ;
512513 }
513514 }
@@ -537,7 +538,7 @@ static void ggml_tallocr_alloc_graph_impl(ggml_gallocr_t galloc, struct ggml_cgr
537538 hash_get (galloc , view_src )-> n_views += 1 ;
538539 if (node -> buffer == NULL && node -> data != NULL ) {
539540 // view of a pre-allocated tensor, didn't call init_view() yet
540- init_view (galloc , node );
541+ init_view (galloc , node , true );
541542 }
542543 }
543544
@@ -548,7 +549,7 @@ static void ggml_tallocr_alloc_graph_impl(ggml_gallocr_t galloc, struct ggml_cgr
548549 }
549550 hash_get (galloc , parent )-> n_children += 1 ;
550551 if (ggml_is_view (parent ) && parent -> buffer == NULL && parent -> data != NULL ) {
551- init_view (galloc , parent );
552+ init_view (galloc , parent , true );
552553 }
553554 }
554555 }
@@ -663,7 +664,7 @@ size_t ggml_gallocr_alloc_graph(ggml_gallocr_t galloc, ggml_tallocr_t talloc, st
663664 return max_size ;
664665}
665666
666- void ggml_gallocr_alloc_graph_n (ggml_gallocr_t galloc , struct ggml_cgraph * graph , struct ggml_hash_set hash_set , ggml_tallocr_t * hash_node_alloct ) {
667+ void ggml_gallocr_alloc_graph_n (ggml_gallocr_t galloc , struct ggml_cgraph * graph , struct ggml_hash_set hash_set , ggml_tallocr_t * hash_node_talloc ) {
667668 const size_t hash_size = hash_set .size ;
668669
669670 GGML_ASSERT (hash_size >= (size_t )(graph -> n_nodes + graph -> n_leafs ));
@@ -686,7 +687,7 @@ void ggml_gallocr_alloc_graph_n(ggml_gallocr_t galloc, struct ggml_cgraph * grap
686687 // reset hash values
687688 memset (galloc -> hash_values , 0 , sizeof (struct hash_node ) * hash_size );
688689
689- galloc -> hash_allocs = hash_node_alloct ;
690+ galloc -> hash_allocs = hash_node_talloc ;
690691
691692 ggml_tallocr_alloc_graph_impl (galloc , graph );
692693
0 commit comments