MCPcopy Index your code
hub / github.com/zai-org/CodeGeeX / sampler

Function sampler

codegeex/mindspore/src/generate.py:38–81  ·  view source on GitHub ↗

Convert the log_probs to probability

(log_probs_revised, top_p, top_k_num, use_pynative=False)

Source from the content-addressed store, hash-verified

36
37
38def sampler(log_probs_revised, top_p, top_k_num, use_pynative=False):
39 """Convert the log_probs to probability"""
40 if use_pynative:
41 logits = P.Pow()(np.e, Tensor(log_probs_revised, mstype.float32))
42 else:
43 logits = np.power(np.e, np.array(log_probs_revised, np.float32))
44
45 # If top_p is less than 1.0, use top_p sampling
46 if top_p < 1.0:
47 # Only consider the 5000 largest logits to reduce computation
48 if use_pynative:
49 sorted_logits, index = P.TopK(sorted=True)(logits, 5000)
50 index = index.asnumpy()
51 sorted_logits = sorted_logits.asnumpy()
52 else:
53 sorted_logits, index = topk_fun(logits, 5000)
54
55 index = index[0]
56 sorted_p = sorted_logits / sum(sorted_logits)
57 cumsum_p = np.cumsum(sorted_p, axis=1)
58 sorted_logits = sorted_logits[0]
59 cumsum_p = cumsum_p[0]
60 top_p_num = sum(cumsum_p < top_p) + 1
61
62 # Get the corresponding probs and indices
63 probs = sorted_logits[:top_p_num]
64 p_args = index[:top_p_num]
65 p = probs / sum(probs)
66 # if top_p is set to 1.0, use top_k sampling
67 else:
68 # Get the corresponding probs and indices
69 if use_pynative:
70 probs, p_args = P.TopK(sorted=True)(logits, top_k_num)
71 probs = probs.asnumpy()
72 p_args = p_args.asnumpy()
73 else:
74 probs, p_args = topk_fun(logits, top_k_num)
75 probs = probs[0]
76 p_args = p_args[0]
77 # Avoid rounding error
78 # if sum(probs) == 0:
79 # probs = np.array([1 / top_k_num for _ in range(top_k_num)])
80 p = probs / sum(probs)
81 return p, p_args
82
83
84def generate(model, origin_inputs, config, verbose=False):

Callers 2

generateFunction · 0.70
generate_incrementFunction · 0.70

Calls 2

cumsumMethod · 0.80
topk_funFunction · 0.70

Tested by

no test coverage detected