MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / dropout_lower

Function dropout_lower

imperative/python/megengine/xla/rules/random.py:67–77  ·  view source on GitHub ↗
(ctx, *args: Union[HLOTensor, Sequence[HLOTensor]])

Source from the content-addressed store, hash-verified

65
66@register_lower_rule(mops.Dropout)
67def dropout_lower(ctx, *args: Union[HLOTensor, Sequence[HLOTensor]]):
68 assert len(ctx.vars_in) == 2 and len(args) == 2 and len(ctx.vars_out) == 3
69 inp, key = args
70 random_val, new_key = rng_uint_generator(key, inp.shape, "uint32")
71 mask = random_val > np.array(ctx.op.drop_prob * np.iinfo(np.uint32).max, np.uint32)
72 multiplier = mask.astype(inp.dtype)
73 assert ctx.op.drop_prob < 1 and ctx.op.drop_prob >= 0, ctx.op.drop_prob
74 multiplier = multiplier / (1.0 - ctx.op.drop_prob)
75 out = (inp * multiplier).astype(inp.dtype)
76 mask = mask.reshape((-1,)).astype("uint8")
77 return out, mask, new_key
78
79
80@register_lower_rule("DropoutBackward")

Callers

nothing calls this directly

Calls 4

rng_uint_generatorFunction · 0.85
arrayMethod · 0.80
astypeMethod · 0.45
reshapeMethod · 0.45

Tested by

no test coverage detected