Skip to content

Commit 1294f5e

Browse files
qhzhuangYour Name
and
Your Name
authored
fix pyboost bug, adapt to new orangePi image, disable multi thread (#1969)
Co-authored-by: Your Name <[email protected]>
1 parent fd860f0 commit 1294f5e

File tree

11 files changed

+94
-201
lines changed

11 files changed

+94
-201
lines changed
Loading

llm/inference/janus_pro/generation.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import os
22
import PIL.Image
33
import mindspore
4+
from mindspore._c_expression import disable_multi_thread
5+
disable_multi_thread()
46
import mindspore as ms
57
import numpy as np
68
from mindnlp.core import ops
@@ -76,7 +78,7 @@ def generate(
7678
generated_tokens = ops.zeros(parallel_size, image_token_num_per_image, dtype=ms.int32)
7779

7880
for i in range(image_token_num_per_image):
79-
print(str(i)+'='*60)
81+
print(f"generating token {i}")
8082
outputs = mmgpt.language_model.model(inputs_embeds=inputs_embeds, use_cache=True, past_key_values=outputs.past_key_values if i != 0 else None)
8183
hidden_states = outputs.last_hidden_state # (parallel_size*2, len(input_ids), 2048)
8284

@@ -95,6 +97,7 @@ def generate(
9597
# print("img_embeds.shape:", img_embeds.shape)
9698
# print("img_embeds.dtype:", img_embeds.dtype)
9799
inputs_embeds = img_embeds.unsqueeze(dim=1) #(parallel_size*2, 2048)
100+
print("generated one token")
98101

99102
if image_token_num_per_image==576:
100103
dec = mmgpt.gen_vision_model.decode_code(generated_tokens.astype(ms.int32), shape=[parallel_size, 8, img_size//patch_size, img_size//patch_size])

llm/inference/janus_pro/janus/models/modeling_vlm.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,8 @@ def prepare_inputs_embeds(
264264
# replace with the image embeddings
265265
# 627 576
266266
# inputs_embeds[images_seq_mask] = images_embeds[images_emb_mask]
267-
print("inputs_embeds:", inputs_embeds.shape)
268-
print("images_embeds[images_emb_mask].dtype", images_embeds[images_emb_mask].dtype)
267+
# print("inputs_embeds:", inputs_embeds.shape)
268+
# print("images_embeds[images_emb_mask].dtype", images_embeds[images_emb_mask].dtype)
269269
print("inputs_embeds.dtype", inputs_embeds.dtype)
270270
padding_size = images_seq_mask.shape[1] - images_emb_mask.shape[1]
271271
padding = Tensor(np.full((images_seq_mask.shape[0], padding_size), False), dtype=images_emb_mask.dtype)

llm/inference/janus_pro/understanding.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import mindspore
2+
from mindspore._c_expression import disable_multi_thread
3+
disable_multi_thread()
24
from mindnlp.transformers import AutoModelForCausalLM
35
from janus.models import MultiModalityCausalLM, VLChatProcessor
46
from janus.utils.io import load_pil_images
57
from mindnlp.configs import set_pyboost, use_pyboost
68
from mindnlp.core import nn, Tensor
79
from mindnlp.core import no_grad
810

11+
912
from mindnlp.configs import use_pyboost, set_pyboost
1013
print('use_pyboost:', use_pyboost()) # 这里默认是False
1114
mindspore.set_context(
@@ -27,7 +30,8 @@
2730
model_path, trust_remote_code=True, ms_dtype=mindspore.float16
2831
)
2932
print('loaded processor and ckpt ')
30-
question = 'describe this image'
33+
# question = 'describe this image'
34+
question = 'what is the animal in the image'
3135
image = "./inpain_model_cat.png"
3236
conversation = [
3337
{

mindnlp/core/nn/functional.py

+12-11
Original file line numberDiff line numberDiff line change
@@ -169,13 +169,16 @@ def drop_and_mask(keep_prob, seed=None):
169169

170170
dense_ = ops.Dense()
171171
def linear(input, weight, bias=None):
172-
input = input.to(mindspore.float16)
173-
weight = weight.to(mindspore.float16)
174-
if bias is not None:
175-
bias = bias.to(mindspore.float16)
176-
return dense_(input, weight) + bias
177-
return dense_(input, weight)
178-
172+
if ON_ORANGE_PI:
173+
input = input.to(mindspore.float16)
174+
weight = weight.to(mindspore.float16)
175+
if bias is not None:
176+
bias = bias.to(mindspore.float16)
177+
return dense_(input, weight) + bias
178+
return dense_(input, weight)
179+
if use_pyboost():
180+
return mindspore.mint.nn.functional.linear(input, weight, bias)
181+
return dense_(input, weight, bias)
179182

180183

181184
def binary_cross_entropy_with_logits(input, target, weight=None, reduction='mean', pos_weight=None):
@@ -479,8 +482,8 @@ def addcmul(input, tensor1, tensor2, value=1):
479482
return input + value*tensor1*tensor2
480483

481484
def group_norm(input, num_groups, weight=None, bias=None, eps=1e-5):
482-
# if use_pyboost():
483-
# return mindspore.mint.nn.functional.group_norm(input, num_groups, weight, bias, eps)
485+
if use_pyboost():
486+
return mindspore.mint.nn.functional.group_norm(input, num_groups, weight, bias, eps)
484487

485488
input_shape = input.shape
486489
N = input_shape[0]
@@ -491,8 +494,6 @@ def group_norm(input, num_groups, weight=None, bias=None, eps=1e-5):
491494
affine_param_shape = [1] * input.ndim
492495
affine_param_shape[1] = C
493496
affine_param_shape = tuple(affine_param_shape)
494-
print(affine_param_shape)
495-
print(out.shape)
496497
if weight is not None and bias is not None:
497498
# out = bias.view(affine_param_shape).addcmul(out, weight.view(affine_param_shape), 1)
498499
out = addcmul(bias.view(affine_param_shape), out, weight.view(affine_param_shape), 1)

mindnlp/core/nn/modules/linear.py

-12
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,6 @@
1010
from .. import functional as F
1111
from ... import ops
1212

13-
14-
def contains_nan_or_inf(tensor, info):
15-
tensor = tensor.astype(mindspore.float16)
16-
havenan = mops.isnan(tensor).any()
17-
haveinf = mops.isinf(tensor).any()
18-
if haveinf:
19-
print(info+'haveinf')
20-
if havenan:
21-
print(info+'havenan')
22-
2313
class Linear(Module):
2414
r"""Applies a linear transformation to the incoming data: :math:`y = Ax + b`
2515
@@ -74,8 +64,6 @@ def forward(self, input):
7464
self.weight = Parameter(self.weight.astype(mindspore.float16))
7565
if self.bias is not None and self.bias.dtype == mindspore.float32:
7666
self.bias = Parameter(self.bias.astype(mindspore.float16))
77-
print("self.weight.dtype:", self.weight.dtype)
78-
contains_nan_or_inf(input, 'Linear.input ')
7967
return F.linear(input, self.weight, self.bias)
8068

8169
def __repr__(self):

mindnlp/core/nn/modules/module.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1225,7 +1225,7 @@ def train(self, mode=True):
12251225
Module: self
12261226
"""
12271227
if ON_ORANGE_PI:
1228-
set_pyboost(not mode)
1228+
set_pyboost(False)
12291229
self.training = mode
12301230
for module in self.children():
12311231
module.train(mode)

mindnlp/core/ops/array.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ def argwhere(input):
2121
# cat
2222
has_cat = hasattr(mindspore.mint, 'cat')
2323
def cat(tensors, dim=0):
24-
# if use_pyboost() and has_cat:
25-
# return mindspore.mint.cat(tensors, dim)
24+
if use_pyboost() and has_cat:
25+
return mindspore.mint.cat(tensors, dim)
2626
return ops.cat(tensors, dim)
2727

2828
# concat

mindnlp/core/ops/blas.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ def bmm(input, other):
3131
if ON_ORANGE_PI:
3232
input = input.to(mindspore.float16)
3333
other = other.to(mindspore.float16)
34-
# if use_pyboost() and has_bmm:
35-
# return mindspore.mint.bmm(input, other)
34+
if use_pyboost() and has_bmm:
35+
return mindspore.mint.bmm(input, other)
3636
return ops.bmm(input, other)
3737

3838
# chain_matmul

mindnlp/transformers/cache_utils.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -378,8 +378,7 @@ def update(
378378
self.key_cache[layer_idx] = key_states
379379
self.value_cache[layer_idx] = value_states
380380
else:
381-
self.key_cache[layer_idx] = ops.cat(
382-
[self.key_cache[layer_idx].astype(mindspore.float16), key_states.astype(mindspore.float16)], dim=-2)
381+
self.key_cache[layer_idx] = ops.cat([self.key_cache[layer_idx].astype(mindspore.float16), key_states.astype(mindspore.float16)], dim=-2)
383382
self.value_cache[layer_idx] = ops.cat([self.value_cache[layer_idx], value_states], dim=-2)
384383

385384
return self.key_cache[layer_idx], self.value_cache[layer_idx]

0 commit comments

Comments
 (0)