PanguAlpha evaluation net Args: backbone: backbone network of PanguAlpha generate: enable generate mode Inputs: input_ids: the tokenized inpus current_index: the index of current token init_reset: whether reset saved states Returns: ou
| 545 | |
| 546 | |
| 547 | class EvalNet(nn.Cell): |
| 548 | """ |
| 549 | PanguAlpha evaluation net |
| 550 | Args: |
| 551 | backbone: backbone network of PanguAlpha |
| 552 | generate: enable generate mode |
| 553 | Inputs: |
| 554 | input_ids: the tokenized inpus |
| 555 | current_index: the index of current token |
| 556 | init_reset: whether reset saved states |
| 557 | Returns: |
| 558 | outputs: Tensor, corresponding output for different tasks |
| 559 | """ |
| 560 | |
| 561 | def __init__(self, backbone, generate=False, pad_token=6, seq_length=2048): |
| 562 | super(EvalNet, self).__init__(auto_prefix=False) |
| 563 | self.backbone = backbone |
| 564 | self.pad_token = pad_token |
| 565 | self.argmax = P.Argmax() |
| 566 | self.generate = generate |
| 567 | self.topk = P.TopK(sorted=True).shard(((1, 1),)) |
| 568 | self.gather = P.Gather().shard(((1, 1), (1,))) |
| 569 | self.log_softmax = P.LogSoftmax().shard(((1, 1, 1),)) |
| 570 | self.get_attention_mask = AttentionMask(seq_length) |
| 571 | self.expand = P.ExpandDims().shard(((1, 1, 1),)) |
| 572 | self.all_ones_attention_mask = Tensor(np.ones((1, 1, seq_length)), mstype.float32) |
| 573 | self.not_equal = P.NotEqual().shard(((1, 1), ())) |
| 574 | |
| 575 | def construct(self, input_ids, current_index, init_reset=True, batch_valid_length=None): |
| 576 | """evaluation net""" |
| 577 | # input_mask = F.cast(F.not_equal(input_ids, self.pad_token), mstype.float32) |
| 578 | input_mask = F.cast(self.not_equal(input_ids, self.pad_token), mstype.float32) |
| 579 | bs, seq_length = F.shape(input_ids) |
| 580 | if self.is_first_iteration is False: |
| 581 | attention_mask = P.Tile()(self.all_ones_attention_mask, (bs, 1, 1)) |
| 582 | else: |
| 583 | attention_mask = self.get_attention_mask(input_mask) |
| 584 | input_position = F.tuple_to_array(F.make_range(seq_length)) |
| 585 | input_position = P.Tile()(input_position, (bs, 1)) |
| 586 | logits = self.backbone(input_ids, input_position, attention_mask, |
| 587 | init_reset, batch_valid_length) |
| 588 | index = current_index.view(-1, ) |
| 589 | # P.Print()("==logits_is:", logits, ",shape is:", logits.shape) |
| 590 | # P.Print()("==index_is:", index, ",shape is:", index.shape) |
| 591 | logits = self.gather(logits, index, 0) |
| 592 | logits = logits.view(bs, 1, -1) |
| 593 | log_probs = self.log_softmax(logits) |
| 594 | return log_probs |
| 595 | |
| 596 | |
| 597 | class LogitsNet(nn.Cell): |
no outgoing calls
no test coverage detected