philippguevorguian commited on
Commit
f09d0fa
·
verified ·
1 Parent(s): 7128c93

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +20 -14
README.md CHANGED
@@ -45,9 +45,10 @@ pip install perceptron
45
  ### Usage
46
 
47
  ```python
 
48
  from transformers import AutoModelForCausalLM, AutoProcessor
49
- from transformers.utils.import_utils import is_torch_cuda_available
50
  from transformers.image_utils import load_image
 
51
 
52
  def document_to_messages(document: list[dict]):
53
  messages, images = [], []
@@ -62,14 +63,18 @@ def document_to_messages(document: list[dict]):
62
  messages.append({"role": role, "content": content})
63
  return messages, images
64
 
 
 
 
65
  # Load model/processor from the checkpoint
66
- checkpoint_path = "PerceptronAI/Isaac-0.2-2B-Preview"
67
- processor = AutoProcessor.from_pretrained(checkpoint_path, trust_remote_code=True)
68
- device, dtype = ("cuda","bfloat16") if is_torch_cuda_available() else ("cpu","float32")
69
  model = AutoModelForCausalLM.from_pretrained(
70
- checkpoint_path, trust_remote_code=True, vision_attn_implementation="flash_attention_2", dtype = dtype
71
- ).to(device=device)
 
 
72
 
 
73
  document = [
74
  {
75
  "type": "text",
@@ -87,20 +92,21 @@ document = [
87
  "role": "user",
88
  },
89
  ]
90
-
91
- # Prepare inputs for generation
92
  messages, images = document_to_messages(document)
93
- text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
 
 
94
  inputs = processor(text=text, images=images, return_tensors="pt")
95
 
96
- # Generation
97
  generated_ids = model.generate(
98
- tensor_stream=inputs["tensor_stream"].to(device),
99
  max_new_tokens=256,
100
  do_sample=False,
101
  )
102
- generated_text = processor.tokenizer.decode(generated_ids[0], skip_special_tokens=False)
103
- print(f"\n Output: {generated_text}")
104
-
 
105
  ```
106
 
 
45
  ### Usage
46
 
47
  ```python
48
+ import torch
49
  from transformers import AutoModelForCausalLM, AutoProcessor
 
50
  from transformers.image_utils import load_image
51
+ from transformers.utils.import_utils import is_torch_cuda_available
52
 
53
  def document_to_messages(document: list[dict]):
54
  messages, images = [], []
 
63
  messages.append({"role": role, "content": content})
64
  return messages, images
65
 
66
+ hf_path = "PerceptronAI/Isaac-0.2-2B-Preview"
67
+ device, dtype = ("cuda",torch.bfloat16) if is_torch_cuda_available() else ("cpu",torch.float32)
68
+
69
  # Load model/processor from the checkpoint
70
+ processor = AutoProcessor.from_pretrained(hf_path, trust_remote_code=True)
 
 
71
  model = AutoModelForCausalLM.from_pretrained(
72
+ hf_path, trust_remote_code=True, vision_attn_implementation="flash_attention_2"
73
+ )
74
+ model = model.to(device=device, dtype=dtype)
75
+ model.eval()
76
 
77
+ # Prepare input for generation
78
  document = [
79
  {
80
  "type": "text",
 
92
  "role": "user",
93
  },
94
  ]
 
 
95
  messages, images = document_to_messages(document)
96
+ text = processor.apply_chat_template(
97
+ messages, tokenize=False, add_generation_prompt=True
98
+ )
99
  inputs = processor(text=text, images=images, return_tensors="pt")
100
 
101
+ # Generate text using the model
102
  generated_ids = model.generate(
103
+ tensor_stream=inputs["tensor_stream"].to(next(model.parameters()).device),
104
  max_new_tokens=256,
105
  do_sample=False,
106
  )
107
+ generated_text = processor.tokenizer.decode(
108
+ generated_ids[0], skip_special_tokens=False
109
+ )
110
+ print(f"\nFull generated output:\n{generated_text}")
111
  ```
112