code: def extract_bart_word_embeddings(
      model_name,
      tokenizer,
      model,
      data,
      device,
      keep_endpoints,
      num_layers,
      detach=True,
  ):
      """
      Handles vi-bart.  May need testing before using on other bart
  
      https://github.com/VinAIResearch/BARTpho
      """
      processed = (
          []
      )  # final product, returns the list of list of word representation
  
      sentences = [
          " ".join([word.replace(" ", "_") for word in sentence])
          for sentence in data
      ]
      tokenized = tokenizer(
          sentences,
          return_tensors="pt",
          padding=True,
          return_attention_mask=True,
      )
      input_ids = tokenized["input_ids"].to(device)
      attention_mask = tokenized["attention_mask"].to(device)
  
      for i in range(int(math.ceil(len(sentences) / 128))):
          start_sentence = i * 128
          end_sentence = min(start_sentence + 128, len(sentences))
          input_ids = input_ids[start_sentence:end_sentence]
          attention_mask = attention_mask[start_sentence:end_sentence]
  
          if detach:
              with torch.no_grad():
                  features = model(
                      input_ids,
                      attention_mask=attention_mask,
                      output_hidden_states=True,
                  )
                  features = cloned_feature(
                      features.decoder_hidden_states, num_layers, detach
                  )
          else:
              features = model(
                  input_ids,
                  attention_mask=attention_mask,
                  output_hidden_states=True,
              )
              features = cloned_feature(
                  features.decoder_hidden_states, num_layers, detach
              )
  
          for feature, sentence in zip(features, data):
              # +2 for the endpoints
              feature = feature[: len(sentence) + 2]
              if not keep_endpoints:
                  feature = feature[1:-1]
              processed.append(feature)
  
      return processed
  
imports: import math
  import logging
  import numpy
  import torch
  import torch.nn
  import torch.nn.functional
  from torch.nn.utils.rnn import pad_packed_sequence, pack_padded_sequence, pack_sequence, PackedSequence
label_desc: Use image node_id 7 for calling a global function with example usage: extract_bart_word_embeddings(model_name, tokenizer, model, data, device, keep_endpoints, num_layers, detach) and returns: processed