MCPcopy Create free account
hub / github.com/zai-org/CodeGeeX / sampler

Function sampler

codegeex/mindspore/src/generate_humaneval.py:77–127  ·  view source on GitHub ↗
(log_probs_revised, top_p, top_k_num, use_pynative=False, bad_words_index=[])

Source from the content-addressed store, hash-verified

75
76
77def sampler(log_probs_revised, top_p, top_k_num, use_pynative=False, bad_words_index=[]):
78 for i, bad_words in enumerate(bad_words_index):
79 for bad_word in bad_words:
80 log_probs_revised[i, bad_word] = -10000
81 """Convert the log_probs to probability"""
82 if use_pynative:
83 logits = P.Pow()(np.e, Tensor(log_probs_revised, mstype.float32))
84 else:
85 logits = np.power(np.e, np.array(log_probs_revised, np.float32))
86
87 # If top_p is less than 1.0, use top_p sampling
88 if top_p < 1.0:
89 # Only consider the 5000 largest logits to reduce computation
90 if use_pynative:
91 sorted_logits, index = P.TopK(sorted=True)(logits, 5000)
92 index = index.asnumpy()
93 sorted_logits = sorted_logits.asnumpy()
94 else:
95 sorted_logits, index = topk_fun(logits, 5000)
96
97 sorted_p = sorted_logits / sorted_logits.sum(axis=1).reshape(-1, 1)
98 cumsum_p = np.cumsum(sorted_p, axis=1)
99 # index = index[0]
100 # sorted_logits = sorted_logits[0]
101 # cumsum_p = cumsum_p[0]
102 top_p_num = (cumsum_p < top_p).sum(axis=1) + 1
103
104 # Get the corresponding probs and indices
105 probs = sorted_logits
106 for i, top_p in enumerate(top_p_num):
107 probs[i][top_p:] = 0
108 # probs = sorted_logits[:top_p_num]
109 p_args = index
110 # p_args = index[:top_p_num]
111 p = probs / probs.sum(axis=1).reshape(-1, 1)
112 # if top_p is set to 1.0, use top_k sampling
113 else:
114 # Get the corresponding probs and indices
115 if use_pynative:
116 probs, p_args = P.TopK(sorted=True)(logits, top_k_num)
117 probs = probs.asnumpy()
118 p_args = p_args.asnumpy()
119 else:
120 probs, p_args = topk_fun(logits, top_k_num)
121 # probs = probs[0]
122 # p_args = p_args[0]
123 # Avoid rounding error
124 # if sum(probs) == 0:
125 # probs = np.array([1 / top_k_num for _ in range(top_k_num)])
126 p = probs / probs.sum(axis=1).reshape(-1, 1)
127 return p, p_args
128
129
130def generate_increment(model, origin_inputs, config, tokenizer, verbose=False):

Callers 1

generate_incrementFunction · 0.70

Calls 2

cumsumMethod · 0.80
topk_funFunction · 0.70

Tested by

no test coverage detected