repo
stringlengths
1
99
file
stringlengths
13
215
code
stringlengths
12
59.2M
file_length
int64
12
59.2M
avg_line_length
float64
3.82
1.48M
max_line_length
int64
12
2.51M
extension_type
stringclasses
1 value
data-to-text-hierarchical
data-to-text-hierarchical-master/onmt/tests/test_copy_generator.py
import unittest from onmt.modules.copy_generator import CopyGenerator, CopyGeneratorLoss import itertools from copy import deepcopy import torch from torch.nn.functional import softmax from onmt.tests.utils_for_tests import product_dict class TestCopyGenerator(unittest.TestCase): INIT_CASES = list(product_dict( input_size=[172], output_size=[319], pad_idx=[0, 39], )) PARAMS = list(product_dict( batch_size=[1, 14], max_seq_len=[23], tgt_max_len=[50], n_extra_words=[107] )) @classmethod def dummy_inputs(cls, params, init_case): hidden = torch.randn((params["batch_size"] * params["tgt_max_len"], init_case["input_size"])) attn = torch.randn((params["batch_size"] * params["tgt_max_len"], params["max_seq_len"])) src_map = torch.randn((params["max_seq_len"], params["batch_size"], params["n_extra_words"])) return hidden, attn, src_map @classmethod def expected_shape(cls, params, init_case): return params["tgt_max_len"] * params["batch_size"], \ init_case["output_size"] + params["n_extra_words"] def test_copy_gen_forward_shape(self): for params, init_case in itertools.product( self.PARAMS, self.INIT_CASES): cgen = CopyGenerator(**init_case) dummy_in = self.dummy_inputs(params, init_case) res = cgen(*dummy_in) expected_shape = self.expected_shape(params, init_case) self.assertEqual(res.shape, expected_shape, init_case.__str__()) def test_copy_gen_outp_has_no_prob_of_pad(self): for params, init_case in itertools.product( self.PARAMS, self.INIT_CASES): cgen = CopyGenerator(**init_case) dummy_in = self.dummy_inputs(params, init_case) res = cgen(*dummy_in) self.assertTrue( res[:, init_case["pad_idx"]].allclose(torch.tensor(0.0))) def test_copy_gen_trainable_params_update(self): for params, init_case in itertools.product( self.PARAMS, self.INIT_CASES): cgen = CopyGenerator(**init_case) trainable_params = {n: p for n, p in cgen.named_parameters() if p.requires_grad} assert len(trainable_params) > 0 # sanity check old_weights = deepcopy(trainable_params) dummy_in = self.dummy_inputs(params, init_case) res = cgen(*dummy_in) pretend_loss = res.sum() pretend_loss.backward() dummy_optim = torch.optim.SGD(trainable_params.values(), 1) dummy_optim.step() for param_name in old_weights.keys(): self.assertTrue( trainable_params[param_name] .ne(old_weights[param_name]).any(), param_name + " " + init_case.__str__()) class TestCopyGeneratorLoss(unittest.TestCase): INIT_CASES = list(product_dict( vocab_size=[172], unk_index=[0, 39], ignore_index=[1, 17], # pad idx force_copy=[True, False] )) PARAMS = list(product_dict( batch_size=[1, 14], tgt_max_len=[50], n_extra_words=[107] )) @classmethod def dummy_inputs(cls, params, init_case): n_unique_src_words = 13 scores = torch.randn((params["batch_size"] * params["tgt_max_len"], init_case["vocab_size"] + n_unique_src_words)) scores = softmax(scores, dim=1) align = torch.randint(0, n_unique_src_words, (params["batch_size"] * params["tgt_max_len"],)) target = torch.randint(0, init_case["vocab_size"], (params["batch_size"] * params["tgt_max_len"],)) target[0] = init_case["unk_index"] target[1] = init_case["ignore_index"] return scores, align, target @classmethod def expected_shape(cls, params, init_case): return (params["batch_size"] * params["tgt_max_len"],) def test_copy_loss_forward_shape(self): for params, init_case in itertools.product( self.PARAMS, self.INIT_CASES): loss = CopyGeneratorLoss(**init_case) dummy_in = self.dummy_inputs(params, init_case) res = loss(*dummy_in) expected_shape = self.expected_shape(params, init_case) self.assertEqual(res.shape, expected_shape, init_case.__str__()) def test_copy_loss_ignore_index_is_ignored(self): for params, init_case in itertools.product( self.PARAMS, self.INIT_CASES): loss = CopyGeneratorLoss(**init_case) scores, align, target = self.dummy_inputs(params, init_case) res = loss(scores, align, target) should_be_ignored = (target == init_case["ignore_index"]).nonzero() assert len(should_be_ignored) > 0 # otherwise not testing anything self.assertTrue(res[should_be_ignored].allclose(torch.tensor(0.0))) def test_copy_loss_output_range_is_positive(self): for params, init_case in itertools.product( self.PARAMS, self.INIT_CASES): loss = CopyGeneratorLoss(**init_case) dummy_in = self.dummy_inputs(params, init_case) res = loss(*dummy_in) self.assertTrue((res >= 0).all())
5,518
39.284672
79
py
data-to-text-hierarchical
data-to-text-hierarchical-master/onmt/tests/test_text_dataset.py
import unittest from onmt.inputters.text_dataset import TextMultiField, TextDataReader import itertools import os from copy import deepcopy from torchtext.data import Field from onmt.tests.utils_for_tests import product_dict class TestTextMultiField(unittest.TestCase): INIT_CASES = list(product_dict( base_name=["base_field", "zbase_field"], base_field=[Field], feats_fields=[ [], [("a", Field)], [("r", Field), ("b", Field)]])) PARAMS = list(product_dict( include_lengths=[False, True])) @classmethod def initialize_case(cls, init_case, params): # initialize fields at the top of each unit test to prevent # any undesired stateful effects case = deepcopy(init_case) case["base_field"] = case["base_field"]( include_lengths=params["include_lengths"]) for i, (n, f_cls) in enumerate(case["feats_fields"]): case["feats_fields"][i] = (n, f_cls(sequential=True)) return case def test_process_shape(self): dummy_input_bs_1 = [[ ["this", "is", "for", "the", "unittest"], ["NOUN", "VERB", "PREP", "ART", "NOUN"], ["", "", "", "", "MODULE"]]] dummy_input_bs_5 = [ [["this", "is", "for", "the", "unittest"], ["NOUN", "VERB", "PREP", "ART", "NOUN"], ["", "", "", "", "MODULE"]], [["batch", "2"], ["NOUN", "NUM"], ["", ""]], [["batch", "3", "is", "the", "longest", "batch"], ["NOUN", "NUM", "VERB", "ART", "ADJ", "NOUN"], ["", "", "", "", "", ""]], [["fourth", "batch"], ["ORD", "NOUN"], ["", ""]], [["and", "another", "one"], ["CONJ", "?", "NUM"], ["", "", ""]]] for bs, max_len, dummy_input in [ (1, 5, dummy_input_bs_1), (5, 6, dummy_input_bs_5)]: for init_case, params in itertools.product( self.INIT_CASES, self.PARAMS): init_case = self.initialize_case(init_case, params) mf = TextMultiField(**init_case) fields = [init_case["base_field"]] \ + [f for _, f in init_case["feats_fields"]] nfields = len(fields) for i, f in enumerate(fields): all_sents = [b[i] for b in dummy_input] f.build_vocab(all_sents) inp_only_desired_fields = [b[:nfields] for b in dummy_input] data = mf.process(inp_only_desired_fields) if params["include_lengths"]: data, lengths = data self.assertEqual(lengths.shape, (bs,)) expected_shape = (max_len, bs, nfields) self.assertEqual(data.shape, expected_shape) def test_preprocess_shape(self): for init_case, params in itertools.product( self.INIT_CASES, self.PARAMS): init_case = self.initialize_case(init_case, params) mf = TextMultiField(**init_case) sample_str = "dummy input here ." proc = mf.preprocess(sample_str) self.assertEqual(len(proc), len(init_case["feats_fields"]) + 1) def test_base_field(self): for init_case, params in itertools.product( self.INIT_CASES, self.PARAMS): init_case = self.initialize_case(init_case, params) mf = TextMultiField(**init_case) self.assertIs(mf.base_field, init_case["base_field"]) def test_correct_n_fields(self): for init_case, params in itertools.product( self.INIT_CASES, self.PARAMS): init_case = self.initialize_case(init_case, params) mf = TextMultiField(**init_case) self.assertEqual(len(mf.fields), len(init_case["feats_fields"]) + 1) def test_fields_order_correct(self): for init_case, params in itertools.product( self.INIT_CASES, self.PARAMS): init_case = self.initialize_case(init_case, params) mf = TextMultiField(**init_case) fnames = [name for name, _ in init_case["feats_fields"]] correct_order = [init_case["base_name"]] + list(sorted(fnames)) self.assertEqual([name for name, _ in mf.fields], correct_order) def test_getitem_0_returns_correct_field(self): for init_case, params in itertools.product( self.INIT_CASES, self.PARAMS): init_case = self.initialize_case(init_case, params) mf = TextMultiField(**init_case) self.assertEqual(mf[0][0], init_case["base_name"]) self.assertIs(mf[0][1], init_case["base_field"]) def test_getitem_nonzero_returns_correct_field(self): for init_case, params in itertools.product( self.INIT_CASES, self.PARAMS): init_case = self.initialize_case(init_case, params) mf = TextMultiField(**init_case) fnames = [name for name, _ in init_case["feats_fields"]] if len(fnames) > 0: ordered_names = list(sorted(fnames)) name2field = dict(init_case["feats_fields"]) for i, name in enumerate(ordered_names, 1): expected_field = name2field[name] self.assertIs(mf[i][1], expected_field) def test_getitem_has_correct_number_of_indexes(self): for init_case, params in itertools.product( self.INIT_CASES, self.PARAMS): init_case = self.initialize_case(init_case, params) mf = TextMultiField(**init_case) nfields = len(init_case["feats_fields"]) + 1 with self.assertRaises(IndexError): mf[nfields] class TestTextDataReader(unittest.TestCase): def test_read(self): strings = [ "hello world".encode("utf-8"), "this's a string with punctuation .".encode("utf-8"), "ThIs Is A sTrInG wItH oDD CapitALIZAtion".encode("utf-8") ] rdr = TextDataReader() for i, ex in enumerate(rdr.read(strings, "src")): self.assertEqual(ex["src"], strings[i].decode("utf-8")) class TestTextDataReaderFromFS(unittest.TestCase): # this test touches the file system, so it could be considered an # integration test STRINGS = [ "hello world\n".encode("utf-8"), "this's a string with punctuation . \n".encode("utf-8"), "ThIs Is A sTrInG wItH oDD CapitALIZAtion\n".encode("utf-8") ] FILE_NAME = "test_strings.txt" @classmethod def setUpClass(cls): # write utf-8 bytes with open(cls.FILE_NAME, "wb") as f: for str_ in cls.STRINGS: f.write(str_) @classmethod def tearDownClass(cls): os.remove(cls.FILE_NAME) def test_read(self): rdr = TextDataReader() for i, ex in enumerate(rdr.read(self.FILE_NAME, "src")): self.assertEqual(ex["src"], self.STRINGS[i].decode("utf-8"))
7,251
39.741573
76
py
data-to-text-hierarchical
data-to-text-hierarchical-master/onmt/tests/test_beam_search.py
import unittest from onmt.translate.beam_search import BeamSearch, GNMTGlobalScorer from copy import deepcopy import torch class GlobalScorerStub(object): alpha = 0 beta = 0 def __init__(self): self.length_penalty = lambda x, alpha: 1. self.cov_penalty = lambda cov, beta: torch.zeros( (1, cov.shape[-2]), device=cov.device, dtype=torch.float) self.has_cov_pen = False self.has_len_pen = False def update_global_state(self, beam): pass def score(self, beam, scores): return scores class TestBeamSearch(unittest.TestCase): BLOCKED_SCORE = -10e20 def test_advance_with_all_repeats_gets_blocked(self): # all beams repeat (beam >= 1 repeat dummy scores) beam_sz = 5 n_words = 100 repeat_idx = 47 ngram_repeat = 3 device_init = torch.zeros(1, 1) for batch_sz in [1, 3]: beam = BeamSearch( beam_sz, batch_sz, 0, 1, 2, 2, GlobalScorerStub(), 0, 30, False, ngram_repeat, set(), False, 0.) beam.initialize(device_init, torch.randint(0, 30, (batch_sz,))) for i in range(ngram_repeat + 4): # predict repeat_idx over and over again word_probs = torch.full( (batch_sz * beam_sz, n_words), -float('inf')) word_probs[0::beam_sz, repeat_idx] = 0 attns = torch.randn(1, batch_sz * beam_sz, 53) beam.advance(word_probs, attns) if i < ngram_repeat: # before repeat, scores are either 0 or -inf expected_scores = torch.tensor( [0] + [-float('inf')] * (beam_sz - 1))\ .repeat(batch_sz, 1) self.assertTrue(beam.topk_log_probs.equal(expected_scores)) elif i % ngram_repeat == 0: # on repeat, `repeat_idx` score is BLOCKED_SCORE # (but it's still the best score, thus we have # [BLOCKED_SCORE, -inf, -inf, -inf, -inf] expected_scores = torch.tensor( [0] + [-float('inf')] * (beam_sz - 1))\ .repeat(batch_sz, 1) expected_scores[:, 0] = self.BLOCKED_SCORE self.assertTrue(beam.topk_log_probs.equal(expected_scores)) else: # repetitions keeps maximizing score # index 0 has been blocked, so repeating=>+0.0 score # other indexes are -inf so repeating=>BLOCKED_SCORE # which is higher expected_scores = torch.tensor( [0] + [-float('inf')] * (beam_sz - 1))\ .repeat(batch_sz, 1) expected_scores[:, :] = self.BLOCKED_SCORE expected_scores = torch.tensor( self.BLOCKED_SCORE).repeat(batch_sz, beam_sz) def test_advance_with_some_repeats_gets_blocked(self): # beam 0 and beam >=2 will repeat (beam >= 2 repeat dummy scores) beam_sz = 5 n_words = 100 repeat_idx = 47 ngram_repeat = 3 no_repeat_score = -2.3 repeat_score = -0.1 device_init = torch.zeros(1, 1) for batch_sz in [1, 3]: beam = BeamSearch( beam_sz, batch_sz, 0, 1, 2, 2, GlobalScorerStub(), 0, 30, False, ngram_repeat, set(), False, 0.) beam.initialize(device_init, torch.randint(0, 30, (batch_sz,))) for i in range(ngram_repeat + 4): # non-interesting beams are going to get dummy values word_probs = torch.full( (batch_sz * beam_sz, n_words), -float('inf')) if i == 0: # on initial round, only predicted scores for beam 0 # matter. Make two predictions. Top one will be repeated # in beam zero, second one will live on in beam 1. word_probs[0::beam_sz, repeat_idx] = repeat_score word_probs[0::beam_sz, repeat_idx + i + 1] = no_repeat_score else: # predict the same thing in beam 0 word_probs[0::beam_sz, repeat_idx] = 0 # continue pushing around what beam 1 predicts word_probs[1::beam_sz, repeat_idx + i + 1] = 0 attns = torch.randn(1, batch_sz * beam_sz, 53) beam.advance(word_probs, attns) if i < ngram_repeat: self.assertFalse( beam.topk_log_probs[0::beam_sz].eq( self.BLOCKED_SCORE).any()) self.assertFalse( beam.topk_log_probs[1::beam_sz].eq( self.BLOCKED_SCORE).any()) elif i == ngram_repeat: # now beam 0 dies (along with the others), beam 1 -> beam 0 self.assertFalse( beam.topk_log_probs[:, 0].eq( self.BLOCKED_SCORE).any()) expected = torch.full([batch_sz, beam_sz], float("-inf")) expected[:, 0] = no_repeat_score expected[:, 1] = self.BLOCKED_SCORE self.assertTrue( beam.topk_log_probs[:, :].equal(expected)) else: # now beam 0 dies (along with the others), beam 1 -> beam 0 self.assertFalse( beam.topk_log_probs[:, 0].eq( self.BLOCKED_SCORE).any()) expected = torch.full([batch_sz, beam_sz], float("-inf")) expected[:, 0] = no_repeat_score expected[:, 1:] = self.BLOCKED_SCORE self.assertTrue( beam.topk_log_probs.equal(expected)) def test_repeating_excluded_index_does_not_die(self): # beam 0 and beam >= 2 will repeat (beam 2 repeats excluded idx) beam_sz = 5 n_words = 100 repeat_idx = 47 # will be repeated and should be blocked repeat_idx_ignored = 7 # will be repeated and should not be blocked ngram_repeat = 3 device_init = torch.zeros(1, 1) for batch_sz in [1, 3]: beam = BeamSearch( beam_sz, batch_sz, 0, 1, 2, 2, GlobalScorerStub(), 0, 30, False, ngram_repeat, {repeat_idx_ignored}, False, 0.) beam.initialize(device_init, torch.randint(0, 30, (batch_sz,))) for i in range(ngram_repeat + 4): # non-interesting beams are going to get dummy values word_probs = torch.full( (batch_sz * beam_sz, n_words), -float('inf')) if i == 0: word_probs[0::beam_sz, repeat_idx] = -0.1 word_probs[0::beam_sz, repeat_idx + i + 1] = -2.3 word_probs[0::beam_sz, repeat_idx_ignored] = -5.0 else: # predict the same thing in beam 0 word_probs[0::beam_sz, repeat_idx] = 0 # continue pushing around what beam 1 predicts word_probs[1::beam_sz, repeat_idx + i + 1] = 0 # predict the allowed-repeat again in beam 2 word_probs[2::beam_sz, repeat_idx_ignored] = 0 attns = torch.randn(1, batch_sz * beam_sz, 53) beam.advance(word_probs, attns) if i < ngram_repeat: self.assertFalse(beam.topk_log_probs[:, 0].eq( self.BLOCKED_SCORE).any()) self.assertFalse(beam.topk_log_probs[:, 1].eq( self.BLOCKED_SCORE).any()) self.assertFalse(beam.topk_log_probs[:, 2].eq( self.BLOCKED_SCORE).any()) else: # now beam 0 dies, beam 1 -> beam 0, beam 2 -> beam 1 # and the rest die self.assertFalse(beam.topk_log_probs[:, 0].eq( self.BLOCKED_SCORE).any()) # since all preds after i=0 are 0, we can check # that the beam is the correct idx by checking that # the curr score is the initial score self.assertTrue(beam.topk_log_probs[:, 0].eq(-2.3).all()) self.assertFalse(beam.topk_log_probs[:, 1].eq( self.BLOCKED_SCORE).all()) self.assertTrue(beam.topk_log_probs[:, 1].eq(-5.0).all()) self.assertTrue(beam.topk_log_probs[:, 2].eq( self.BLOCKED_SCORE).all()) def test_doesnt_predict_eos_if_shorter_than_min_len(self): # beam 0 will always predict EOS. The other beams will predict # non-eos scores. for batch_sz in [1, 3]: beam_sz = 5 n_words = 100 _non_eos_idxs = [47, 51, 13, 88, 99] valid_score_dist = torch.log_softmax(torch.tensor( [6., 5., 4., 3., 2., 1.]), dim=0) min_length = 5 eos_idx = 2 lengths = torch.randint(0, 30, (batch_sz,)) beam = BeamSearch(beam_sz, batch_sz, 0, 1, 2, 2, GlobalScorerStub(), min_length, 30, False, 0, set(), False, 0.) device_init = torch.zeros(1, 1) beam.initialize(device_init, lengths) all_attns = [] for i in range(min_length + 4): # non-interesting beams are going to get dummy values word_probs = torch.full( (batch_sz * beam_sz, n_words), -float('inf')) if i == 0: # "best" prediction is eos - that should be blocked word_probs[0::beam_sz, eos_idx] = valid_score_dist[0] # include at least beam_sz predictions OTHER than EOS # that are greater than -1e20 for j, score in zip(_non_eos_idxs, valid_score_dist[1:]): word_probs[0::beam_sz, j] = score else: # predict eos in beam 0 word_probs[0::beam_sz, eos_idx] = valid_score_dist[0] # provide beam_sz other good predictions for k, (j, score) in enumerate( zip(_non_eos_idxs, valid_score_dist[1:])): beam_idx = min(beam_sz - 1, k) word_probs[beam_idx::beam_sz, j] = score attns = torch.randn(1, batch_sz * beam_sz, 53) all_attns.append(attns) beam.advance(word_probs, attns) if i < min_length: expected_score_dist = \ (i + 1) * valid_score_dist[1:].unsqueeze(0) self.assertTrue( beam.topk_log_probs.allclose( expected_score_dist)) elif i == min_length: # now the top beam has ended and no others have self.assertTrue(beam.is_finished[:, 0].eq(1).all()) self.assertTrue(beam.is_finished[:, 1:].eq(0).all()) else: # i > min_length # not of interest, but want to make sure it keeps running # since only beam 0 terminates and n_best = 2 pass def test_beam_is_done_when_n_best_beams_eos_using_min_length(self): # this is also a test that when block_ngram_repeat=0, # repeating is acceptable beam_sz = 5 batch_sz = 3 n_words = 100 _non_eos_idxs = [47, 51, 13, 88, 99] valid_score_dist = torch.log_softmax(torch.tensor( [6., 5., 4., 3., 2., 1.]), dim=0) min_length = 5 eos_idx = 2 beam = BeamSearch( beam_sz, batch_sz, 0, 1, 2, 2, GlobalScorerStub(), min_length, 30, False, 0, set(), False, 0.) device_init = torch.zeros(1, 1) beam.initialize(device_init, torch.randint(0, 30, (batch_sz,))) for i in range(min_length + 4): # non-interesting beams are going to get dummy values word_probs = torch.full( (batch_sz * beam_sz, n_words), -float('inf')) if i == 0: # "best" prediction is eos - that should be blocked word_probs[0::beam_sz, eos_idx] = valid_score_dist[0] # include at least beam_sz predictions OTHER than EOS # that are greater than -1e20 for j, score in zip(_non_eos_idxs, valid_score_dist[1:]): word_probs[0::beam_sz, j] = score elif i <= min_length: # predict eos in beam 1 word_probs[1::beam_sz, eos_idx] = valid_score_dist[0] # provide beam_sz other good predictions in other beams for k, (j, score) in enumerate( zip(_non_eos_idxs, valid_score_dist[1:])): beam_idx = min(beam_sz - 1, k) word_probs[beam_idx::beam_sz, j] = score else: word_probs[0::beam_sz, eos_idx] = valid_score_dist[0] word_probs[1::beam_sz, eos_idx] = valid_score_dist[0] # provide beam_sz other good predictions in other beams for k, (j, score) in enumerate( zip(_non_eos_idxs, valid_score_dist[1:])): beam_idx = min(beam_sz - 1, k) word_probs[beam_idx::beam_sz, j] = score attns = torch.randn(1, batch_sz * beam_sz, 53) beam.advance(word_probs, attns) if i < min_length: self.assertFalse(beam.done) elif i == min_length: # beam 1 dies on min_length self.assertTrue(beam.is_finished[:, 1].all()) beam.update_finished() self.assertFalse(beam.done) else: # i > min_length # beam 0 dies on the step after beam 1 dies self.assertTrue(beam.is_finished[:, 0].all()) beam.update_finished() self.assertTrue(beam.done) def test_beam_returns_attn_with_correct_length(self): beam_sz = 5 batch_sz = 3 n_words = 100 _non_eos_idxs = [47, 51, 13, 88, 99] valid_score_dist = torch.log_softmax(torch.tensor( [6., 5., 4., 3., 2., 1.]), dim=0) min_length = 5 eos_idx = 2 inp_lens = torch.randint(1, 30, (batch_sz,)) beam = BeamSearch( beam_sz, batch_sz, 0, 1, 2, 2, GlobalScorerStub(), min_length, 30, True, 0, set(), False, 0.) device_init = torch.zeros(1, 1) _, _, inp_lens, _ = beam.initialize(device_init, inp_lens) # inp_lens is tiled in initialize, reassign to make attn match for i in range(min_length + 2): # non-interesting beams are going to get dummy values word_probs = torch.full( (batch_sz * beam_sz, n_words), -float('inf')) if i == 0: # "best" prediction is eos - that should be blocked word_probs[0::beam_sz, eos_idx] = valid_score_dist[0] # include at least beam_sz predictions OTHER than EOS # that are greater than -1e20 for j, score in zip(_non_eos_idxs, valid_score_dist[1:]): word_probs[0::beam_sz, j] = score elif i <= min_length: # predict eos in beam 1 word_probs[1::beam_sz, eos_idx] = valid_score_dist[0] # provide beam_sz other good predictions in other beams for k, (j, score) in enumerate( zip(_non_eos_idxs, valid_score_dist[1:])): beam_idx = min(beam_sz - 1, k) word_probs[beam_idx::beam_sz, j] = score else: word_probs[0::beam_sz, eos_idx] = valid_score_dist[0] word_probs[1::beam_sz, eos_idx] = valid_score_dist[0] # provide beam_sz other good predictions in other beams for k, (j, score) in enumerate( zip(_non_eos_idxs, valid_score_dist[1:])): beam_idx = min(beam_sz - 1, k) word_probs[beam_idx::beam_sz, j] = score attns = torch.randn(1, batch_sz * beam_sz, 53) beam.advance(word_probs, attns) if i < min_length: self.assertFalse(beam.done) # no top beams are finished yet for b in range(batch_sz): self.assertEqual(beam.attention[b], []) elif i == min_length: # beam 1 dies on min_length self.assertTrue(beam.is_finished[:, 1].all()) beam.update_finished() self.assertFalse(beam.done) # no top beams are finished yet for b in range(batch_sz): self.assertEqual(beam.attention[b], []) else: # i > min_length # beam 0 dies on the step after beam 1 dies self.assertTrue(beam.is_finished[:, 0].all()) beam.update_finished() self.assertTrue(beam.done) # top beam is finished now so there are attentions for b in range(batch_sz): # two beams are finished in each batch self.assertEqual(len(beam.attention[b]), 2) for k in range(2): # second dim is cut down to the non-padded src length self.assertEqual(beam.attention[b][k].shape[-1], inp_lens[b]) # first dim is equal to the time of death # (beam 0 died at current step - adjust for SOS) self.assertEqual(beam.attention[b][0].shape[0], i + 1) # (beam 1 died at last step - adjust for SOS) self.assertEqual(beam.attention[b][1].shape[0], i) # behavior gets weird when beam is already done so just stop break class TestBeamSearchAgainstReferenceCase(unittest.TestCase): # this is just test_beam.TestBeamAgainstReferenceCase repeated # in each batch. BEAM_SZ = 5 EOS_IDX = 2 # don't change this - all the scores would need updated N_WORDS = 8 # also don't change for same reason N_BEST = 3 DEAD_SCORE = -1e20 BATCH_SZ = 3 INP_SEQ_LEN = 53 def random_attn(self): return torch.randn(1, self.BATCH_SZ * self.BEAM_SZ, self.INP_SEQ_LEN) def init_step(self, beam, expected_len_pen): # init_preds: [4, 3, 5, 6, 7] - no EOS's init_scores = torch.log_softmax(torch.tensor( [[0, 0, 0, 4, 5, 3, 2, 1]], dtype=torch.float), dim=1) init_scores = deepcopy(init_scores.repeat( self.BATCH_SZ * self.BEAM_SZ, 1)) new_scores = init_scores + beam.topk_log_probs.view(-1).unsqueeze(1) expected_beam_scores, expected_preds_0 = new_scores \ .view(self.BATCH_SZ, self.BEAM_SZ * self.N_WORDS) \ .topk(self.BEAM_SZ, dim=-1) beam.advance(deepcopy(init_scores), self.random_attn()) self.assertTrue(beam.topk_log_probs.allclose(expected_beam_scores)) self.assertTrue(beam.topk_ids.equal(expected_preds_0)) self.assertFalse(beam.is_finished.any()) self.assertFalse(beam.done) return expected_beam_scores def first_step(self, beam, expected_beam_scores, expected_len_pen): # no EOS's yet assert beam.is_finished.sum() == 0 scores_1 = torch.log_softmax(torch.tensor( [[0, 0, 0, .3, 0, .51, .2, 0], [0, 0, 1.5, 0, 0, 0, 0, 0], [0, 0, 0, 0, .49, .48, 0, 0], [0, 0, 0, .2, .2, .2, .2, .2], [0, 0, 0, .2, .2, .2, .2, .2]] ), dim=1) scores_1 = scores_1.repeat(self.BATCH_SZ, 1) beam.advance(deepcopy(scores_1), self.random_attn()) new_scores = scores_1 + expected_beam_scores.view(-1).unsqueeze(1) expected_beam_scores, unreduced_preds = new_scores\ .view(self.BATCH_SZ, self.BEAM_SZ * self.N_WORDS)\ .topk(self.BEAM_SZ, -1) expected_bptr_1 = unreduced_preds / self.N_WORDS # [5, 3, 2, 6, 0], so beam 2 predicts EOS! expected_preds_1 = unreduced_preds - expected_bptr_1 * self.N_WORDS self.assertTrue(beam.topk_log_probs.allclose(expected_beam_scores)) self.assertTrue(beam.topk_scores.allclose( expected_beam_scores / expected_len_pen)) self.assertTrue(beam.topk_ids.equal(expected_preds_1)) self.assertTrue(beam.current_backptr.equal(expected_bptr_1)) self.assertEqual(beam.is_finished.sum(), self.BATCH_SZ) self.assertTrue(beam.is_finished[:, 2].all()) # beam 2 finished beam.update_finished() self.assertFalse(beam.top_beam_finished.any()) self.assertFalse(beam.done) return expected_beam_scores def second_step(self, beam, expected_beam_scores, expected_len_pen): # assumes beam 2 finished on last step scores_2 = torch.log_softmax(torch.tensor( [[0, 0, 0, .3, 0, .51, .2, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 5000, .48, 0, 0], # beam 2 shouldn't continue [0, 0, 50, .2, .2, .2, .2, .2], # beam 3 -> beam 0 should die [0, 0, 0, .2, .2, .2, .2, .2]] ), dim=1) scores_2 = scores_2.repeat(self.BATCH_SZ, 1) beam.advance(deepcopy(scores_2), self.random_attn()) # ended beam 2 shouldn't continue expected_beam_scores[:, 2::self.BEAM_SZ] = self.DEAD_SCORE new_scores = scores_2 + expected_beam_scores.view(-1).unsqueeze(1) expected_beam_scores, unreduced_preds = new_scores\ .view(self.BATCH_SZ, self.BEAM_SZ * self.N_WORDS)\ .topk(self.BEAM_SZ, -1) expected_bptr_2 = unreduced_preds / self.N_WORDS # [2, 5, 3, 6, 0] repeat self.BATCH_SZ, so beam 0 predicts EOS! expected_preds_2 = unreduced_preds - expected_bptr_2 * self.N_WORDS # [-2.4879, -3.8910, -4.1010, -4.2010, -4.4010] repeat self.BATCH_SZ self.assertTrue(beam.topk_log_probs.allclose(expected_beam_scores)) self.assertTrue(beam.topk_scores.allclose( expected_beam_scores / expected_len_pen)) self.assertTrue(beam.topk_ids.equal(expected_preds_2)) self.assertTrue(beam.current_backptr.equal(expected_bptr_2)) # another beam is finished in all batches self.assertEqual(beam.is_finished.sum(), self.BATCH_SZ) # new beam 0 finished self.assertTrue(beam.is_finished[:, 0].all()) # new beam 0 is old beam 3 self.assertTrue(expected_bptr_2[:, 0].eq(3).all()) beam.update_finished() self.assertTrue(beam.top_beam_finished.all()) self.assertFalse(beam.done) return expected_beam_scores def third_step(self, beam, expected_beam_scores, expected_len_pen): # assumes beam 0 finished on last step scores_3 = torch.log_softmax(torch.tensor( [[0, 0, 5000, 0, 5000, .51, .2, 0], # beam 0 shouldn't cont [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 5000, 0, 0], [0, 0, 0, .2, .2, .2, .2, .2], [0, 0, 50, 0, .2, .2, .2, .2]] # beam 4 -> beam 1 should die ), dim=1) scores_3 = scores_3.repeat(self.BATCH_SZ, 1) beam.advance(deepcopy(scores_3), self.random_attn()) expected_beam_scores[:, 0::self.BEAM_SZ] = self.DEAD_SCORE new_scores = scores_3 + expected_beam_scores.view(-1).unsqueeze(1) expected_beam_scores, unreduced_preds = new_scores\ .view(self.BATCH_SZ, self.BEAM_SZ * self.N_WORDS)\ .topk(self.BEAM_SZ, -1) expected_bptr_3 = unreduced_preds / self.N_WORDS # [5, 2, 6, 1, 0] repeat self.BATCH_SZ, so beam 1 predicts EOS! expected_preds_3 = unreduced_preds - expected_bptr_3 * self.N_WORDS self.assertTrue(beam.topk_log_probs.allclose( expected_beam_scores)) self.assertTrue(beam.topk_scores.allclose( expected_beam_scores / expected_len_pen)) self.assertTrue(beam.topk_ids.equal(expected_preds_3)) self.assertTrue(beam.current_backptr.equal(expected_bptr_3)) self.assertEqual(beam.is_finished.sum(), self.BATCH_SZ) # new beam 1 finished self.assertTrue(beam.is_finished[:, 1].all()) # new beam 1 is old beam 4 self.assertTrue(expected_bptr_3[:, 1].eq(4).all()) beam.update_finished() self.assertTrue(beam.top_beam_finished.all()) self.assertTrue(beam.done) return expected_beam_scores def test_beam_advance_against_known_reference(self): beam = BeamSearch( self.BEAM_SZ, self.BATCH_SZ, 0, 1, 2, self.N_BEST, GlobalScorerStub(), 0, 30, False, 0, set(), False, 0.) device_init = torch.zeros(1, 1) beam.initialize(device_init, torch.randint(0, 30, (self.BATCH_SZ,))) expected_beam_scores = self.init_step(beam, 1) expected_beam_scores = self.first_step(beam, expected_beam_scores, 1) expected_beam_scores = self.second_step(beam, expected_beam_scores, 1) self.third_step(beam, expected_beam_scores, 1) class TestBeamWithLengthPenalty(TestBeamSearchAgainstReferenceCase): # this could be considered an integration test because it tests # interactions between the GNMT scorer and the beam def test_beam_advance_against_known_reference(self): scorer = GNMTGlobalScorer(0.7, 0., "avg", "none") beam = BeamSearch( self.BEAM_SZ, self.BATCH_SZ, 0, 1, 2, self.N_BEST, scorer, 0, 30, False, 0, set(), False, 0.) device_init = torch.zeros(1, 1) beam.initialize(device_init, torch.randint(0, 30, (self.BATCH_SZ,))) expected_beam_scores = self.init_step(beam, 1.) expected_beam_scores = self.first_step(beam, expected_beam_scores, 3) expected_beam_scores = self.second_step(beam, expected_beam_scores, 4) self.third_step(beam, expected_beam_scores, 5)
27,033
46.345009
79
py
data-to-text-hierarchical
data-to-text-hierarchical-master/onmt/tests/test_translation_server.py
import unittest from onmt.translate.translation_server import ServerModel, TranslationServer import os from six import string_types from textwrap import dedent import torch from onmt.translate.translator import Translator TEST_DIR = os.path.dirname(os.path.abspath(__file__)) class TestServerModel(unittest.TestCase): def test_deferred_loading_model_and_unload(self): model_id = 0 opt = {"models": ["test_model.pt"]} model_root = TEST_DIR sm = ServerModel(opt, model_id, model_root=model_root, load=False) self.assertFalse(sm.loaded) sm.load() self.assertTrue(sm.loaded) self.assertIsInstance(sm.translator, Translator) sm.unload() self.assertFalse(sm.loaded) def test_load_model_on_init_and_unload(self): model_id = 0 opt = {"models": ["test_model.pt"]} model_root = TEST_DIR sm = ServerModel(opt, model_id, model_root=model_root, load=True) self.assertTrue(sm.loaded) self.assertIsInstance(sm.translator, Translator) sm.unload() self.assertFalse(sm.loaded) def test_tokenizing_with_no_tokenizer_fails(self): model_id = 0 opt = {"models": ["test_model.pt"]} model_root = TEST_DIR sm = ServerModel(opt, model_id, model_root=model_root, load=True) with self.assertRaises(ValueError): sm.tokenize("hello world") def test_detokenizing_with_no_tokenizer_fails(self): model_id = 0 opt = {"models": ["test_model.pt"]} model_root = TEST_DIR sm = ServerModel(opt, model_id, model_root=model_root, load=True) with self.assertRaises(ValueError): sm.detokenize("hello world") if torch.cuda.is_available(): def test_moving_to_gpu_and_back(self): torch.cuda.set_device(torch.device("cuda", 0)) model_id = 0 opt = {"models": ["test_model.pt"]} model_root = TEST_DIR sm = ServerModel(opt, model_id, model_root=model_root, load=True) for p in sm.translator.model.parameters(): self.assertEqual(p.device.type, "cpu") sm.to_gpu() for p in sm.translator.model.parameters(): self.assertEqual(p.device.type, "cuda") self.assertEqual(p.device.index, 0) sm.to_cpu() for p in sm.translator.model.parameters(): self.assertEqual(p.device.type, "cpu") def test_initialize_on_gpu_and_move_back(self): torch.cuda.set_device(torch.device("cuda", 0)) model_id = 0 opt = {"models": ["test_model.pt"], "gpu": 0} model_root = TEST_DIR sm = ServerModel(opt, model_id, model_root=model_root, load=True) for p in sm.translator.model.parameters(): self.assertEqual(p.device.type, "cuda") self.assertEqual(p.device.index, 0) sm.to_gpu() for p in sm.translator.model.parameters(): self.assertEqual(p.device.type, "cuda") self.assertEqual(p.device.index, 0) sm.to_cpu() for p in sm.translator.model.parameters(): self.assertEqual(p.device.type, "cpu") if torch.cuda.device_count() > 1: def test_initialize_on_nonzero_gpu_and_back(self): torch.cuda.set_device(torch.device("cuda", 1)) model_id = 0 opt = {"models": ["test_model.pt"], "gpu": 1} model_root = TEST_DIR sm = ServerModel(opt, model_id, model_root=model_root, load=True) for p in sm.translator.model.parameters(): self.assertEqual(p.device.type, "cuda") self.assertEqual(p.device.index, 1) sm.to_gpu() for p in sm.translator.model.parameters(): self.assertEqual(p.device.type, "cuda") self.assertEqual(p.device.index, 1) sm.to_cpu() for p in sm.translator.model.parameters(): self.assertEqual(p.device.type, "cpu") def test_run(self): model_id = 0 opt = {"models": ["test_model.pt"]} model_root = TEST_DIR sm = ServerModel(opt, model_id, model_root=model_root, load=True) inp = [{"src": "hello how are you today"}, {"src": "good morning to you ."}] results, scores, n_best, time, aligns = sm.run(inp) self.assertIsInstance(results, list) for sentence_string in results: self.assertIsInstance(sentence_string, string_types) self.assertIsInstance(scores, list) for elem in scores: self.assertIsInstance(elem, float) self.assertIsInstance(aligns, list) for align_string in aligns: if align_string is not None: self.assertIsInstance(align_string, string_types) self.assertEqual(len(results), len(scores)) self.assertEqual(len(scores), len(inp) * n_best) self.assertEqual(len(time), 1) self.assertIsInstance(time, dict) self.assertIn("translation", time) class TestTranslationServer(unittest.TestCase): # this could be considered an integration test because it touches # the filesystem for the config file (and the models) CFG_F = os.path.join( TEST_DIR, "test_translation_server_config_file.json") def tearDown(self): if os.path.exists(self.CFG_F): os.remove(self.CFG_F) def write(self, cfg): with open(self.CFG_F, "w") as f: f.write(cfg) CFG_NO_LOAD = dedent("""\ { "models_root": "%s", "models": [ { "id": 100, "model": "test_model.pt", "timeout": -1, "on_timeout": "to_cpu", "load": false, "opt": { "beam_size": 5 } } ] } """ % TEST_DIR) def test_start_without_initial_loading(self): self.write(self.CFG_NO_LOAD) sv = TranslationServer() sv.start(self.CFG_F) self.assertFalse(sv.models[100].loaded) self.assertEqual(set(sv.models.keys()), {100}) CFG_LOAD = dedent("""\ { "models_root": "%s", "models": [ { "id": 100, "model": "test_model.pt", "timeout": -1, "on_timeout": "to_cpu", "load": true, "opt": { "beam_size": 5 } } ] } """ % TEST_DIR) def test_start_with_initial_loading(self): self.write(self.CFG_LOAD) sv = TranslationServer() sv.start(self.CFG_F) self.assertTrue(sv.models[100].loaded) self.assertEqual(set(sv.models.keys()), {100}) CFG_2_MODELS = dedent("""\ { "models_root": "%s", "models": [ { "id": 100, "model": "test_model.pt", "timeout": -1, "on_timeout": "to_cpu", "load": true, "opt": { "beam_size": 5 } }, { "id": 1000, "model": "test_model2.pt", "timeout": -1, "on_timeout": "to_cpu", "load": false, "opt": { "beam_size": 5 } } ] } """ % TEST_DIR) def test_start_with_two_models(self): self.write(self.CFG_2_MODELS) sv = TranslationServer() sv.start(self.CFG_F) self.assertTrue(sv.models[100].loaded) self.assertFalse(sv.models[1000].loaded) self.assertEqual(set(sv.models.keys()), {100, 1000})
8,233
34.339056
77
py
data-to-text-hierarchical
data-to-text-hierarchical-master/onmt/tests/test_greedy_search.py
import unittest from onmt.translate.greedy_search import GreedySearch import torch class TestGreedySearch(unittest.TestCase): BATCH_SZ = 3 INP_SEQ_LEN = 53 DEAD_SCORE = -1e20 BLOCKED_SCORE = -10e20 def test_doesnt_predict_eos_if_shorter_than_min_len(self): # batch 0 will always predict EOS. The other batches will predict # non-eos scores. for batch_sz in [1, 3]: n_words = 100 _non_eos_idxs = [47] valid_score_dist = torch.log_softmax(torch.tensor( [6., 5.]), dim=0) min_length = 5 eos_idx = 2 lengths = torch.randint(0, 30, (batch_sz,)) samp = GreedySearch( 0, 1, 2, batch_sz, min_length, False, set(), False, 30, 1., 1) samp.initialize(torch.zeros(1), lengths) all_attns = [] for i in range(min_length + 4): word_probs = torch.full( (batch_sz, n_words), -float('inf')) # "best" prediction is eos - that should be blocked word_probs[0, eos_idx] = valid_score_dist[0] # include at least one prediction OTHER than EOS # that is greater than -1e20 word_probs[0, _non_eos_idxs[0]] = valid_score_dist[1] word_probs[1:, _non_eos_idxs[0] + i] = 0 attns = torch.randn(1, batch_sz, 53) all_attns.append(attns) samp.advance(word_probs, attns) if i < min_length: self.assertTrue( samp.topk_scores[0].allclose(valid_score_dist[1])) self.assertTrue( samp.topk_scores[1:].eq(0).all()) elif i == min_length: # now batch 0 has ended and no others have self.assertTrue(samp.is_finished[0, :].eq(1).all()) self.assertTrue(samp.is_finished[1:, 1:].eq(0).all()) else: # i > min_length break def test_returns_correct_scores_deterministic(self): for batch_sz in [1, 13]: for temp in [1., 3.]: n_words = 100 _non_eos_idxs = [47, 51, 13, 88, 99] valid_score_dist_1 = torch.log_softmax(torch.tensor( [6., 5., 4., 3., 2., 1.]), dim=0) valid_score_dist_2 = torch.log_softmax(torch.tensor( [6., 1.]), dim=0) eos_idx = 2 lengths = torch.randint(0, 30, (batch_sz,)) samp = GreedySearch( 0, 1, 2, batch_sz, 0, False, set(), False, 30, temp, 1) samp.initialize(torch.zeros(1), lengths) # initial step i = 0 word_probs = torch.full( (batch_sz, n_words), -float('inf')) # batch 0 dies on step 0 word_probs[0, eos_idx] = valid_score_dist_1[0] # include at least one prediction OTHER than EOS # that is greater than -1e20 word_probs[0, _non_eos_idxs] = valid_score_dist_1[1:] word_probs[1:, _non_eos_idxs[0] + i] = 0 attns = torch.randn(1, batch_sz, 53) samp.advance(word_probs, attns) self.assertTrue(samp.is_finished[0].eq(1).all()) samp.update_finished() self.assertEqual( samp.scores[0], [valid_score_dist_1[0] / temp]) if batch_sz == 1: self.assertTrue(samp.done) continue else: self.assertFalse(samp.done) # step 2 i = 1 word_probs = torch.full( (batch_sz - 1, n_words), -float('inf')) # (old) batch 8 dies on step 1 word_probs[7, eos_idx] = valid_score_dist_2[0] word_probs[0:7, _non_eos_idxs[:2]] = valid_score_dist_2 word_probs[8:, _non_eos_idxs[:2]] = valid_score_dist_2 attns = torch.randn(1, batch_sz, 53) samp.advance(word_probs, attns) self.assertTrue(samp.is_finished[7].eq(1).all()) samp.update_finished() self.assertEqual( samp.scores[8], [valid_score_dist_2[0] / temp]) # step 3 i = 2 word_probs = torch.full( (batch_sz - 2, n_words), -float('inf')) # everything dies word_probs[:, eos_idx] = 0 attns = torch.randn(1, batch_sz, 53) samp.advance(word_probs, attns) self.assertTrue(samp.is_finished.eq(1).all()) samp.update_finished() for b in range(batch_sz): if b != 0 and b != 8: self.assertEqual(samp.scores[b], [0]) self.assertTrue(samp.done) def test_returns_correct_scores_non_deterministic(self): for batch_sz in [1, 13]: for temp in [1., 3.]: n_words = 100 _non_eos_idxs = [47, 51, 13, 88, 99] valid_score_dist_1 = torch.log_softmax(torch.tensor( [6., 5., 4., 3., 2., 1.]), dim=0) valid_score_dist_2 = torch.log_softmax(torch.tensor( [6., 1.]), dim=0) eos_idx = 2 lengths = torch.randint(0, 30, (batch_sz,)) samp = GreedySearch( 0, 1, 2, batch_sz, 0, False, set(), False, 30, temp, 2) samp.initialize(torch.zeros(1), lengths) # initial step i = 0 for _ in range(100): word_probs = torch.full( (batch_sz, n_words), -float('inf')) # batch 0 dies on step 0 word_probs[0, eos_idx] = valid_score_dist_1[0] # include at least one prediction OTHER than EOS # that is greater than -1e20 word_probs[0, _non_eos_idxs] = valid_score_dist_1[1:] word_probs[1:, _non_eos_idxs[0] + i] = 0 attns = torch.randn(1, batch_sz, 53) samp.advance(word_probs, attns) if samp.is_finished[0].eq(1).all(): break else: self.fail("Batch 0 never ended (very unlikely but maybe " "due to stochasticisty. If so, please increase " "the range of the for-loop.") samp.update_finished() self.assertEqual( samp.scores[0], [valid_score_dist_1[0] / temp]) if batch_sz == 1: self.assertTrue(samp.done) continue else: self.assertFalse(samp.done) # step 2 i = 1 for _ in range(100): word_probs = torch.full( (batch_sz - 1, n_words), -float('inf')) # (old) batch 8 dies on step 1 word_probs[7, eos_idx] = valid_score_dist_2[0] word_probs[0:7, _non_eos_idxs[:2]] = valid_score_dist_2 word_probs[8:, _non_eos_idxs[:2]] = valid_score_dist_2 attns = torch.randn(1, batch_sz, 53) samp.advance(word_probs, attns) if samp.is_finished[7].eq(1).all(): break else: self.fail("Batch 8 never ended (very unlikely but maybe " "due to stochasticisty. If so, please increase " "the range of the for-loop.") samp.update_finished() self.assertEqual( samp.scores[8], [valid_score_dist_2[0] / temp]) # step 3 i = 2 for _ in range(250): word_probs = torch.full( (samp.alive_seq.shape[0], n_words), -float('inf')) # everything dies word_probs[:, eos_idx] = 0 attns = torch.randn(1, batch_sz, 53) samp.advance(word_probs, attns) if samp.is_finished.any(): samp.update_finished() if samp.is_finished.eq(1).all(): break else: self.fail("All batches never ended (very unlikely but " "maybe due to stochasticisty. If so, please " "increase the range of the for-loop.") for b in range(batch_sz): if b != 0 and b != 8: self.assertEqual(samp.scores[b], [0]) self.assertTrue(samp.done)
9,200
41.400922
78
py
data-to-text-hierarchical
data-to-text-hierarchical-master/onmt/tests/test_models.py
import copy import unittest import math import torch import onmt import onmt.inputters import onmt.opts from onmt.model_builder import build_embeddings, \ build_encoder, build_decoder from onmt.encoders.image_encoder import ImageEncoder from onmt.encoders.audio_encoder import AudioEncoder from onmt.utils.parse import ArgumentParser parser = ArgumentParser(description='train.py') onmt.opts.model_opts(parser) onmt.opts.train_opts(parser) # -data option is required, but not used in this test, so dummy. opt = parser.parse_known_args(['-data', 'dummy'])[0] class TestModel(unittest.TestCase): def __init__(self, *args, **kwargs): super(TestModel, self).__init__(*args, **kwargs) self.opt = opt def get_field(self): src = onmt.inputters.get_fields("text", 0, 0)["src"] src.base_field.build_vocab([]) return src def get_batch(self, source_l=3, bsize=1): # len x batch x nfeat test_src = torch.ones(source_l, bsize, 1).long() test_tgt = torch.ones(source_l, bsize, 1).long() test_length = torch.ones(bsize).fill_(source_l).long() return test_src, test_tgt, test_length def get_batch_image(self, tgt_l=3, bsize=1, h=15, w=17): # batch x c x h x w test_src = torch.ones(bsize, 3, h, w).float() test_tgt = torch.ones(tgt_l, bsize, 1).long() test_length = None return test_src, test_tgt, test_length def get_batch_audio(self, tgt_l=7, bsize=3, sample_rate=5500, window_size=0.03, t=37): # batch x 1 x nfft x t nfft = int(math.floor((sample_rate * window_size) / 2) + 1) test_src = torch.ones(bsize, 1, nfft, t).float() test_tgt = torch.ones(tgt_l, bsize, 1).long() test_length = torch.ones(bsize).long().fill_(tgt_l) return test_src, test_tgt, test_length def embeddings_forward(self, opt, source_l=3, bsize=1): ''' Tests if the embeddings works as expected args: opt: set of options source_l: Length of generated input sentence bsize: Batchsize of generated input ''' word_field = self.get_field() emb = build_embeddings(opt, word_field) test_src, _, __ = self.get_batch(source_l=source_l, bsize=bsize) if opt.decoder_type == 'transformer': input = torch.cat([test_src, test_src], 0) res = emb(input) compare_to = torch.zeros(source_l * 2, bsize, opt.src_word_vec_size) else: res = emb(test_src) compare_to = torch.zeros(source_l, bsize, opt.src_word_vec_size) self.assertEqual(res.size(), compare_to.size()) def encoder_forward(self, opt, source_l=3, bsize=1): ''' Tests if the encoder works as expected args: opt: set of options source_l: Length of generated input sentence bsize: Batchsize of generated input ''' if opt.rnn_size > 0: opt.enc_rnn_size = opt.rnn_size word_field = self.get_field() embeddings = build_embeddings(opt, word_field) enc = build_encoder(opt, embeddings) test_src, test_tgt, test_length = self.get_batch(source_l=source_l, bsize=bsize) hidden_t, outputs, test_length = enc(test_src, test_length) # Initialize vectors to compare size with test_hid = torch.zeros(self.opt.enc_layers, bsize, opt.enc_rnn_size) test_out = torch.zeros(source_l, bsize, opt.dec_rnn_size) # Ensure correct sizes and types self.assertEqual(test_hid.size(), hidden_t[0].size(), hidden_t[1].size()) self.assertEqual(test_out.size(), outputs.size()) self.assertEqual(type(outputs), torch.Tensor) def nmtmodel_forward(self, opt, source_l=3, bsize=1): """ Creates a nmtmodel with a custom opt function. Forwards a testbatch and checks output size. Args: opt: Namespace with options source_l: length of input sequence bsize: batchsize """ if opt.rnn_size > 0: opt.enc_rnn_size = opt.rnn_size opt.dec_rnn_size = opt.rnn_size word_field = self.get_field() embeddings = build_embeddings(opt, word_field) enc = build_encoder(opt, embeddings) embeddings = build_embeddings(opt, word_field, for_encoder=False) dec = build_decoder(opt, embeddings) model = onmt.models.model.NMTModel(enc, dec) test_src, test_tgt, test_length = self.get_batch(source_l=source_l, bsize=bsize) outputs, attn = model(test_src, test_tgt, test_length) outputsize = torch.zeros(source_l - 1, bsize, opt.dec_rnn_size) # Make sure that output has the correct size and type self.assertEqual(outputs.size(), outputsize.size()) self.assertEqual(type(outputs), torch.Tensor) def imagemodel_forward(self, opt, tgt_l=2, bsize=1, h=15, w=17): """ Creates an image-to-text nmtmodel with a custom opt function. Forwards a testbatch and checks output size. Args: opt: Namespace with options source_l: length of input sequence bsize: batchsize """ if opt.encoder_type == 'transformer' or opt.encoder_type == 'cnn': return word_field = self.get_field() enc = ImageEncoder( opt.enc_layers, opt.brnn, opt.enc_rnn_size, opt.dropout) embeddings = build_embeddings(opt, word_field, for_encoder=False) dec = build_decoder(opt, embeddings) model = onmt.models.model.NMTModel(enc, dec) test_src, test_tgt, test_length = self.get_batch_image( h=h, w=w, bsize=bsize, tgt_l=tgt_l) outputs, attn = model(test_src, test_tgt, test_length) outputsize = torch.zeros(tgt_l - 1, bsize, opt.dec_rnn_size) # Make sure that output has the correct size and type self.assertEqual(outputs.size(), outputsize.size()) self.assertEqual(type(outputs), torch.Tensor) def audiomodel_forward(self, opt, tgt_l=7, bsize=3, t=37): """ Creates a speech-to-text nmtmodel with a custom opt function. Forwards a testbatch and checks output size. Args: opt: Namespace with options source_l: length of input sequence bsize: batchsize """ if opt.encoder_type == 'transformer' or opt.encoder_type == 'cnn': return if opt.rnn_type == 'SRU': return word_field = self.get_field() enc = AudioEncoder(opt.rnn_type, opt.enc_layers, opt.dec_layers, opt.brnn, opt.enc_rnn_size, opt.dec_rnn_size, opt.audio_enc_pooling, opt.dropout, opt.sample_rate, opt.window_size) embeddings = build_embeddings(opt, word_field, for_encoder=False) dec = build_decoder(opt, embeddings) model = onmt.models.model.NMTModel(enc, dec) test_src, test_tgt, test_length = self.get_batch_audio( bsize=bsize, sample_rate=opt.sample_rate, window_size=opt.window_size, t=t, tgt_l=tgt_l) outputs, attn = model(test_src, test_tgt, test_length) outputsize = torch.zeros(tgt_l - 1, bsize, opt.dec_rnn_size) # Make sure that output has the correct size and type self.assertEqual(outputs.size(), outputsize.size()) self.assertEqual(type(outputs), torch.Tensor) def _add_test(param_setting, methodname): """ Adds a Test to TestModel according to settings Args: param_setting: list of tuples of (param, setting) methodname: name of the method that gets called """ def test_method(self): opt = copy.deepcopy(self.opt) if param_setting: for param, setting in param_setting: setattr(opt, param, setting) ArgumentParser.update_model_opts(opt) getattr(self, methodname)(opt) if param_setting: name = 'test_' + methodname + "_" + "_".join( str(param_setting).split()) else: name = 'test_' + methodname + '_standard' setattr(TestModel, name, test_method) test_method.__name__ = name ''' TEST PARAMETERS ''' opt.brnn = False test_embeddings = [[], [('decoder_type', 'transformer')] ] for p in test_embeddings: _add_test(p, 'embeddings_forward') tests_encoder = [[], [('encoder_type', 'mean')], # [('encoder_type', 'transformer'), # ('word_vec_size', 16), ('rnn_size', 16)], [] ] for p in tests_encoder: _add_test(p, 'encoder_forward') tests_nmtmodel = [[('rnn_type', 'GRU')], [('layers', 10)], [('input_feed', 0)], [('decoder_type', 'transformer'), ('encoder_type', 'transformer'), ('src_word_vec_size', 16), ('tgt_word_vec_size', 16), ('rnn_size', 16)], [('decoder_type', 'transformer'), ('encoder_type', 'transformer'), ('src_word_vec_size', 16), ('tgt_word_vec_size', 16), ('rnn_size', 16), ('position_encoding', True)], [('coverage_attn', True)], [('copy_attn', True)], [('global_attention', 'mlp')], [('context_gate', 'both')], [('context_gate', 'target')], [('context_gate', 'source')], [('encoder_type', "brnn"), ('brnn_merge', 'sum')], [('encoder_type', "brnn")], [('decoder_type', 'cnn'), ('encoder_type', 'cnn')], [('encoder_type', 'rnn'), ('global_attention', None)], [('encoder_type', 'rnn'), ('global_attention', None), ('copy_attn', True), ('copy_attn_type', 'general')], [('encoder_type', 'rnn'), ('global_attention', 'mlp'), ('copy_attn', True), ('copy_attn_type', 'general')], [], ] if onmt.models.sru.check_sru_requirement(): # """ Only do SRU test if requirment is safisfied. """ # SRU doesn't support input_feed. tests_nmtmodel.append([('rnn_type', 'SRU'), ('input_feed', 0)]) for p in tests_nmtmodel: _add_test(p, 'nmtmodel_forward') for p in tests_nmtmodel: _add_test(p, 'imagemodel_forward') for p in tests_nmtmodel: p.append(('sample_rate', 5500)) p.append(('window_size', 0.03)) # when reasonable, set audio_enc_pooling to 2 for arg, val in p: if arg == "layers" and int(val) > 2: # Need lengths >= audio_enc_pooling**n_layers. # That condition is unrealistic for large n_layers, # so leave audio_enc_pooling at 1. break else: p.append(('audio_enc_pooling', '2')) _add_test(p, 'audiomodel_forward')
11,557
34.563077
76
py
data-to-text-hierarchical
data-to-text-hierarchical-master/onmt/tests/test_image_dataset.py
import unittest from onmt.inputters.image_dataset import ImageDataReader import os import shutil import cv2 import numpy as np import torch class TestImageDataReader(unittest.TestCase): # this test touches the file system, so it could be considered an # integration test _THIS_DIR = os.path.dirname(os.path.abspath(__file__)) _IMG_DATA_DIRNAME = "test_image_data" _IMG_DATA_DIR = os.path.join(_THIS_DIR, _IMG_DATA_DIRNAME) _IMG_DATA_FMT = "test_img_{:d}.png" _IMG_DATA_PATH_FMT = os.path.join(_IMG_DATA_DIR, _IMG_DATA_FMT) _IMG_LIST_DIR = "test_image_filenames" # file to hold full paths to image data _IMG_LIST_PATHS_FNAME = "test_files.txt" _IMG_LIST_PATHS_PATH = os.path.join( _IMG_LIST_DIR, _IMG_LIST_PATHS_FNAME) # file to hold image paths relative to _IMG_DATA_DIR (i.e. file names) _IMG_LIST_FNAMES_FNAME = "test_fnames.txt" _IMG_LIST_FNAMES_PATH = os.path.join( _IMG_LIST_DIR, _IMG_LIST_FNAMES_FNAME) # it's ok if non-image files co-exist with image files in the data dir _JUNK_FILE = os.path.join( _IMG_DATA_DIR, "this_is_junk.txt") _N_EXAMPLES = 20 _N_CHANNELS = 3 @classmethod def setUpClass(cls): if not os.path.exists(cls._IMG_DATA_DIR): os.makedirs(cls._IMG_DATA_DIR) if not os.path.exists(cls._IMG_LIST_DIR): os.makedirs(cls._IMG_LIST_DIR) with open(cls._JUNK_FILE, "w") as f: f.write("this is some garbage\nShould have no impact.") with open(cls._IMG_LIST_PATHS_PATH, "w") as f_list_fnames, \ open(cls._IMG_LIST_FNAMES_PATH, "w") as f_list_paths: cls.n_rows = torch.randint(30, 314, (cls._N_EXAMPLES,)) cls.n_cols = torch.randint(30, 314, (cls._N_EXAMPLES,)) for i in range(cls._N_EXAMPLES): img = np.random.randint( 0, 255, (cls.n_rows[i], cls.n_cols[i], cls._N_CHANNELS)) f_path = cls._IMG_DATA_PATH_FMT.format(i) cv2.imwrite(f_path, img) f_name_short = cls._IMG_DATA_FMT.format(i) f_list_fnames.write(f_name_short + "\n") f_list_paths.write(f_path + "\n") @classmethod def tearDownClass(cls): shutil.rmtree(cls._IMG_DATA_DIR) shutil.rmtree(cls._IMG_LIST_DIR) def test_read_from_dir_and_data_file_containing_filenames(self): rdr = ImageDataReader(channel_size=self._N_CHANNELS) i = 0 # initialize since there's a sanity check on i for i, img in enumerate(rdr.read( self._IMG_LIST_FNAMES_PATH, "src", self._IMG_DATA_DIR)): self.assertEqual( img["src"].shape, (self._N_CHANNELS, self.n_rows[i], self.n_cols[i])) self.assertEqual(img["src_path"], self._IMG_DATA_PATH_FMT.format(i)) self.assertGreater(i, 0, "No image data was read.") def test_read_from_dir_and_data_file_containing_paths(self): rdr = ImageDataReader(channel_size=self._N_CHANNELS) i = 0 # initialize since there's a sanity check on i for i, img in enumerate(rdr.read( self._IMG_LIST_PATHS_PATH, "src", self._IMG_DATA_DIR)): self.assertEqual( img["src"].shape, (self._N_CHANNELS, self.n_rows[i], self.n_cols[i])) self.assertEqual(img["src_path"], self._IMG_DATA_FMT.format(i)) self.assertGreater(i, 0, "No image data was read.") class TestImageDataReader1Channel(TestImageDataReader): _N_CHANNELS = 1
3,641
38.16129
76
py
data-to-text-hierarchical
data-to-text-hierarchical-master/onmt/tests/test_audio_dataset.py
# -*- coding: utf-8 -*- import unittest from onmt.inputters.audio_dataset import AudioSeqField, AudioDataReader import itertools import os import shutil import torch import torchaudio from onmt.tests.utils_for_tests import product_dict class TestAudioField(unittest.TestCase): INIT_CASES = list(product_dict( pad_index=[0, 32], batch_first=[False, True], include_lengths=[True, False])) PARAMS = list(product_dict( batch_size=[1, 17], max_len=[23], full_length_seq=[0, 5, 16], nfeats=[1, 5])) @classmethod def degenerate_case(cls, init_case, params): if params["batch_size"] < params["full_length_seq"]: return True return False @classmethod def pad_inputs(cls, params): lengths = torch.randint(1, params["max_len"], (params["batch_size"],)).tolist() lengths[params["full_length_seq"]] = params["max_len"] fake_input = [ torch.randn((params["nfeats"], lengths[b])) for b in range(params["batch_size"])] return fake_input, lengths @classmethod def numericalize_inputs(cls, init_case, params): bs = params["batch_size"] max_len = params["max_len"] lengths = torch.randint(1, max_len, (bs,)) lengths[params["full_length_seq"]] = max_len nfeats = params["nfeats"] fake_input = torch.full( (bs, 1, nfeats, max_len), init_case["pad_index"]) for b in range(bs): fake_input[b, :, :, :lengths[b]] = torch.randn( (1, nfeats, lengths[b])) if init_case["include_lengths"]: fake_input = (fake_input, lengths) return fake_input, lengths def test_pad_shape_and_lengths(self): for init_case, params in itertools.product( self.INIT_CASES, self.PARAMS): if not self.degenerate_case(init_case, params): field = AudioSeqField(**init_case) fake_input, lengths = self.pad_inputs(params) outp = field.pad(fake_input) if init_case["include_lengths"]: outp, _ = outp expected_shape = ( params["batch_size"], 1, params["nfeats"], params["max_len"]) self.assertEqual(outp.shape, expected_shape) def test_pad_returns_correct_lengths(self): for init_case, params in itertools.product( self.INIT_CASES, self.PARAMS): if not self.degenerate_case(init_case, params) and \ init_case["include_lengths"]: field = AudioSeqField(**init_case) fake_input, lengths = self.pad_inputs(params) _, outp_lengths = field.pad(fake_input) self.assertEqual(outp_lengths, lengths) def test_pad_pads_right_places_and_uses_correct_index(self): for init_case, params in itertools.product( self.INIT_CASES, self.PARAMS): if not self.degenerate_case(init_case, params): field = AudioSeqField(**init_case) fake_input, lengths = self.pad_inputs(params) outp = field.pad(fake_input) if init_case["include_lengths"]: outp, _ = outp for b in range(params["batch_size"]): for s in range(lengths[b], params["max_len"]): self.assertTrue( outp[b, :, :, s].allclose( torch.tensor(float(init_case["pad_index"])))) def test_numericalize_shape(self): for init_case, params in itertools.product( self.INIT_CASES, self.PARAMS): if not self.degenerate_case(init_case, params): field = AudioSeqField(**init_case) fake_input, lengths = self.numericalize_inputs( init_case, params) outp = field.numericalize(fake_input) if init_case["include_lengths"]: outp, _ = outp if init_case["batch_first"]: expected_shape = ( params["batch_size"], 1, params["nfeats"], params["max_len"]) else: expected_shape = ( params["max_len"], params["batch_size"], 1, params["nfeats"]) self.assertEqual(expected_shape, outp.shape, init_case.__str__()) def test_process_shape(self): # tests pad and numericalize integration for init_case, params in itertools.product( self.INIT_CASES, self.PARAMS): if not self.degenerate_case(init_case, params): field = AudioSeqField(**init_case) fake_input, lengths = self.pad_inputs(params) outp = field.process(fake_input) if init_case["include_lengths"]: outp, _ = outp if init_case["batch_first"]: expected_shape = ( params["batch_size"], 1, params["nfeats"], params["max_len"]) else: expected_shape = ( params["max_len"], params["batch_size"], 1, params["nfeats"]) self.assertEqual(expected_shape, outp.shape, init_case.__str__()) def test_process_lengths(self): # tests pad and numericalize integration for init_case, params in itertools.product( self.INIT_CASES, self.PARAMS): if not self.degenerate_case(init_case, params): if init_case["include_lengths"]: field = AudioSeqField(**init_case) fake_input, lengths = self.pad_inputs(params) lengths = torch.tensor(lengths, dtype=torch.int) _, outp_lengths = field.process(fake_input) self.assertTrue(outp_lengths.eq(lengths).all()) class TestAudioDataReader(unittest.TestCase): # this test touches the file system, so it could be considered an # integration test _THIS_DIR = os.path.dirname(os.path.abspath(__file__)) _AUDIO_DATA_DIRNAME = "test_audio_data" _AUDIO_DATA_DIR = os.path.join(_THIS_DIR, _AUDIO_DATA_DIRNAME) _AUDIO_DATA_FMT = "test_noise_{:d}.wav" _AUDIO_DATA_PATH_FMT = os.path.join(_AUDIO_DATA_DIR, _AUDIO_DATA_FMT) _AUDIO_LIST_DIR = "test_audio_filenames" # file to hold full paths to audio data _AUDIO_LIST_PATHS_FNAME = "test_files.txt" _AUDIO_LIST_PATHS_PATH = os.path.join( _AUDIO_LIST_DIR, _AUDIO_LIST_PATHS_FNAME) # file to hold audio paths relative to _AUDIO_DATA_DIR (i.e. file names) _AUDIO_LIST_FNAMES_FNAME = "test_fnames.txt" _AUDIO_LIST_FNAMES_PATH = os.path.join( _AUDIO_LIST_DIR, _AUDIO_LIST_FNAMES_FNAME) # it's ok if non-audio files co-exist with audio files in the data dir _JUNK_FILE = os.path.join( _AUDIO_DATA_DIR, "this_is_junk.txt") _N_EXAMPLES = 20 _SAMPLE_RATE = 48000 _N_CHANNELS = 2 @classmethod def setUpClass(cls): if not os.path.exists(cls._AUDIO_DATA_DIR): os.makedirs(cls._AUDIO_DATA_DIR) if not os.path.exists(cls._AUDIO_LIST_DIR): os.makedirs(cls._AUDIO_LIST_DIR) with open(cls._JUNK_FILE, "w") as f: f.write("this is some garbage\nShould have no impact.") with open(cls._AUDIO_LIST_PATHS_PATH, "w") as f_list_fnames, \ open(cls._AUDIO_LIST_FNAMES_PATH, "w") as f_list_paths: lengths = torch.randint(int(.5e5), int(1.5e6), (cls._N_EXAMPLES,)) for i in range(cls._N_EXAMPLES): # dividing gets the noise in [-1, 1] white_noise = torch.randn((cls._N_CHANNELS, lengths[i])) / 10 f_path = cls._AUDIO_DATA_PATH_FMT.format(i) torchaudio.save(f_path, white_noise, cls._SAMPLE_RATE) f_name_short = cls._AUDIO_DATA_FMT.format(i) f_list_fnames.write(f_name_short + "\n") f_list_paths.write(f_path + "\n") @classmethod def tearDownClass(cls): shutil.rmtree(cls._AUDIO_DATA_DIR) shutil.rmtree(cls._AUDIO_LIST_DIR) def test_read_from_dir_and_data_file_containing_filenames(self): rdr = AudioDataReader(self._SAMPLE_RATE, window="hamming", window_size=0.02, window_stride=0.01) i = 0 # initialize since there's a sanity check on i for i, aud in enumerate(rdr.read( self._AUDIO_LIST_FNAMES_PATH, "src", self._AUDIO_DATA_DIR)): self.assertEqual(aud["src"].shape[0], 481) self.assertEqual(aud["src_path"], self._AUDIO_DATA_PATH_FMT.format(i)) self.assertGreater(i, 0, "No audio data was read.") def test_read_from_dir_and_data_file_containing_paths(self): rdr = AudioDataReader(self._SAMPLE_RATE, window="hamming", window_size=0.02, window_stride=0.01) i = 0 # initialize since there's a sanity check on i for i, aud in enumerate(rdr.read( self._AUDIO_LIST_PATHS_PATH, "src", self._AUDIO_DATA_DIR)): self.assertEqual(aud["src"].shape[0], 481) self.assertEqual(aud["src_path"], self._AUDIO_DATA_FMT.format(i)) self.assertGreater(i, 0, "No audio data was read.")
9,704
41.565789
78
py
data-to-text-hierarchical
data-to-text-hierarchical-master/onmt/tests/test_structured_attention.py
import unittest from onmt.modules.structured_attention import MatrixTree import torch class TestStructuredAttention(unittest.TestCase): def test_matrix_tree_marg_pdfs_sum_to_1(self): dtree = MatrixTree() q = torch.rand(1, 5, 5) marg = dtree.forward(q) self.assertTrue( marg.sum(1).allclose(torch.tensor(1.0)))
361
24.857143
56
py
data-to-text-hierarchical
data-to-text-hierarchical-master/onmt/tests/test_embeddings.py
import unittest from onmt.modules.embeddings import Embeddings import itertools from copy import deepcopy import torch from onmt.tests.utils_for_tests import product_dict class TestEmbeddings(unittest.TestCase): INIT_CASES = list(product_dict( word_vec_size=[172], word_vocab_size=[319], word_padding_idx=[17], position_encoding=[False, True], feat_merge=["first", "concat", "sum", "mlp"], feat_vec_exponent=[-1, 1.1, 0.7], feat_vec_size=[0, 200], feat_padding_idx=[[], [29], [0, 1]], feat_vocab_sizes=[[], [39], [401, 39]], dropout=[0, 0.5], fix_word_vecs=[False, True] )) PARAMS = list(product_dict( batch_size=[1, 14], max_seq_len=[23] )) @classmethod def case_is_degenerate(cls, case): no_feats = len(case["feat_vocab_sizes"]) == 0 if case["feat_merge"] != "first" and no_feats: return True if case["feat_merge"] == "first" and not no_feats: return True if case["feat_merge"] == "concat" and case["feat_vec_exponent"] != -1: return True if no_feats and case["feat_vec_exponent"] != -1: return True if len(case["feat_vocab_sizes"]) != len(case["feat_padding_idx"]): return True if case["feat_vec_size"] == 0 and case["feat_vec_exponent"] <= 0: return True if case["feat_merge"] == "sum": if case["feat_vec_exponent"] != -1: return True if case["feat_vec_size"] != 0: return True if case["feat_vec_size"] != 0 and case["feat_vec_exponent"] != -1: return True return False @classmethod def cases(cls): for case in cls.INIT_CASES: if not cls.case_is_degenerate(case): yield case @classmethod def dummy_inputs(cls, params, init_case): max_seq_len = params["max_seq_len"] batch_size = params["batch_size"] fv_sizes = init_case["feat_vocab_sizes"] n_words = init_case["word_vocab_size"] voc_sizes = [n_words] + fv_sizes pad_idxs = [init_case["word_padding_idx"]] + \ init_case["feat_padding_idx"] lengths = torch.randint(0, max_seq_len, (batch_size,)) lengths[0] = max_seq_len inps = torch.empty((max_seq_len, batch_size, len(voc_sizes)), dtype=torch.long) for f, (voc_size, pad_idx) in enumerate(zip(voc_sizes, pad_idxs)): for b, len_ in enumerate(lengths): inps[:len_, b, f] = torch.randint(0, voc_size-1, (len_,)) inps[len_:, b, f] = pad_idx return inps @classmethod def expected_shape(cls, params, init_case): wvs = init_case["word_vec_size"] fvs = init_case["feat_vec_size"] nf = len(init_case["feat_vocab_sizes"]) size = wvs if init_case["feat_merge"] not in {"sum", "mlp"}: size += nf * fvs return params["max_seq_len"], params["batch_size"], size def test_embeddings_forward_shape(self): for params, init_case in itertools.product(self.PARAMS, self.cases()): emb = Embeddings(**init_case) dummy_in = self.dummy_inputs(params, init_case) res = emb(dummy_in) expected_shape = self.expected_shape(params, init_case) self.assertEqual(res.shape, expected_shape, init_case.__str__()) def test_embeddings_trainable_params(self): for params, init_case in itertools.product(self.PARAMS, self.cases()): emb = Embeddings(**init_case) trainable_params = {n: p for n, p in emb.named_parameters() if p.requires_grad} # first check there's nothing unexpectedly not trainable for key in emb.state_dict(): if key not in trainable_params: if key.endswith("emb_luts.0.weight") and \ init_case["fix_word_vecs"]: # ok: word embeddings shouldn't be trainable # if word vecs are fixed continue if key.endswith(".pe.pe"): # ok: positional encodings shouldn't be trainable assert init_case["position_encoding"] continue else: self.fail("Param {:s} is unexpectedly not " "trainable.".format(key)) # then check nothing unexpectedly trainable if init_case["fix_word_vecs"]: self.assertFalse( any(trainable_param.endswith("emb_luts.0.weight") for trainable_param in trainable_params), "Word embedding is trainable but word vecs are fixed.") if init_case["position_encoding"]: self.assertFalse( any(trainable_p.endswith(".pe.pe") for trainable_p in trainable_params), "Positional encoding is trainable.") def test_embeddings_trainable_params_update(self): for params, init_case in itertools.product(self.PARAMS, self.cases()): emb = Embeddings(**init_case) trainable_params = {n: p for n, p in emb.named_parameters() if p.requires_grad} if len(trainable_params) > 0: old_weights = deepcopy(trainable_params) dummy_in = self.dummy_inputs(params, init_case) res = emb(dummy_in) pretend_loss = res.sum() pretend_loss.backward() dummy_optim = torch.optim.SGD(trainable_params.values(), 1) dummy_optim.step() for param_name in old_weights.keys(): self.assertTrue( trainable_params[param_name] .ne(old_weights[param_name]).any(), param_name + " " + init_case.__str__())
6,207
40.66443
78
py
data-to-text-hierarchical
data-to-text-hierarchical-master/onmt/tests/test_attention.py
""" Here come the tests for attention types and their compatibility """ import unittest import torch from torch.autograd import Variable import onmt class TestAttention(unittest.TestCase): def test_masked_global_attention(self): source_lengths = torch.IntTensor([7, 3, 5, 2]) # illegal_weights_mask = torch.ByteTensor([ # [0, 0, 0, 0, 0, 0, 0], # [0, 0, 0, 1, 1, 1, 1], # [0, 0, 0, 0, 0, 1, 1], # [0, 0, 1, 1, 1, 1, 1]]) batch_size = source_lengths.size(0) dim = 20 memory_bank = Variable(torch.randn(batch_size, source_lengths.max(), dim)) hidden = Variable(torch.randn(batch_size, dim)) attn = onmt.modules.GlobalAttention(dim) _, alignments = attn(hidden, memory_bank, memory_lengths=source_lengths) # TODO: fix for pytorch 0.3 # illegal_weights = alignments.masked_select(illegal_weights_mask) # self.assertEqual(0.0, illegal_weights.data.sum())
1,072
28
74
py
data-to-text-hierarchical
data-to-text-hierarchical-master/onmt/encoders/hierarchical_transformer.py
from onmt.modules.self_attention import MultiHeadSelfAttention from onmt.encoders.encoder import EncoderBase from onmt.utils.misc import nwise, aeq, sequence_mask import torch, copy import onmt class ContainsNaN(Exception): pass def _check_for_nan(tensor, msg=''): if (tensor!=tensor).any(): raise ContainsNaN(msg) class FeedForward(torch.nn.Module): def __init__(self, input_size, hidden_size, dropout): super().__init__() self.linear1 = torch.nn.Linear(input_size, hidden_size) self.linear2 = torch.nn.Linear(hidden_size, input_size) self.dropout = torch.nn.Dropout(dropout) self.norm = torch.nn.LayerNorm(input_size) def forward(self, src): ret = self.linear1(self.norm(src)) ret = self.linear2(self.dropout(torch.nn.functional.relu(ret))) return src + self.dropout(ret) # residual connetion def update_dropout(self, dropout): self.dropout.p = dropout class TransformerEncoderLayer(torch.nn.Module): r"""TransformerEncoderLayer is made up of self-attn and feedforward network. This standard encoder layer is based on the paper "Attention Is All You Need". Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N Gomez, Lukasz Kaiser, and Illia Polosukhin. 2017. Attention is all you need. In Advances in Neural Information Processing Systems, pages 6000–6010. Users may modify or implement in a different way during application. Args: d_model: the number of expected features in the input (required). nhead: the number of heads in the multiheadattention models (required). dim_feedforward: the dimension of the feedforward network model (default=2048). dropout: the dropout value (default=0.1). Examples:: >>> encoder_layer = nn.TransformerEncoderLayer(d_model, nhead) """ def __init__(self, input_size, heads, dim_feedforward=2048, glu_depth=-1, dropout=0.1): super(TransformerEncoderLayer, self).__init__() self.self_attn = MultiHeadSelfAttention(input_size, heads, dropout=dropout, glu_depth=glu_depth) self.norm = torch.nn.LayerNorm(input_size, dim_feedforward, dropout) self.dropout = torch.nn.Dropout(dropout) self.feedforward = FeedForward(input_size, dim_feedforward, dropout) def forward(self, src, src_mask=None): """Pass the input through the layer. Args: src: the sequence to the encoder layer (required). src_mask: the mask for the src sequence (optional). """ src = src + self.dropout(self.self_attn(self.norm(src), attn_mask=src_mask)[0]) return self.feedforward(src) def update_dropout(self, dropout): self.feedforward.update_dropout(dropout) self.dropout.p = dropout class TransformerEncoder(torch.nn.Module): """TransformerEncoder is a stack of N transformer encoder layers It is heavily inspired by pytorch's. Args: encoder_layer: an instance of the TransformerEncoderLayer class (required). num_layers: the number of sub-encoder-layers in the encoder (required). norm: the layer normalization component (optional). """ def __init__(self, hidden_size, heads=8, num_layers=6, glu_depth=-1, dim_feedforward=2048, dropout=0.1): super().__init__() self.layers = torch.nn.ModuleList([ TransformerEncoderLayer(input_size=hidden_size, heads=heads, dim_feedforward=dim_feedforward, glu_depth=glu_depth, dropout=dropout) for _ in range(num_layers) ]) self.final_norm = torch.nn.LayerNorm(hidden_size) def forward(self, src, mask=None): r"""Pass the input through the all layers in turn. Args: src: the sequence to encode (required). src_mask: the mask for the src sequence (optional). """ for encoder_layer in self.layers: src = encoder_layer(src, mask) return self.final_norm(src) def update_dropout(self, dropout): for layer in self.layers: layer.update_dropout(dropout) def block_eye(n, size): """ Create a block_diagonal matrix of n blocks, where each block is torch.eye(size) """ m1 = torch.ones(n, size, 1, size) m2 = torch.eye(n).view(n, 1, n, 1) return (m1*m2).view(n*size, n*size).to(torch.uint8) def build_pad_mask(source, ent_size, pad_idx): """ [seq_len, n_ents, ent_size] To be used in attention mechanism in decoder """ mask = source[:, :, 0] mask = (mask.transpose(0, 1) .squeeze() .contiguous() .view(source.size(1), -1, ent_size) .eq(pad_idx)) mask[:, :, 0] = 1 # we also mask the <ent> token return mask def build_chunk_mask(lengths, ent_size): """ [bsz, n_ents, n_ents] Filled with -inf where self-attention shouldn't attend, a zeros elsewhere. """ ones = lengths // ent_size ones = sequence_mask(ones).unsqueeze(1).repeat(1, ones.max(), 1).to(lengths.device) mask = torch.full(ones.shape, float('-inf')).to(lengths.device) mask.masked_fill_(ones, 0) return mask class HierarchicalTransformerEncoder(EncoderBase): """ Two encoders, one on the unit level and one on the chunk level """ def __init__(self, embeddings, units_layers=2, chunks_layers=2, units_heads=2, chunks_heads=2, dim_feedforward=1000, units_glu_depth=-1, chunks_glu_depth=-1, dropout=.5): super().__init__() self.embeddings = embeddings self.ent_size = onmt.ENT_SIZE self.unit_encoder = TransformerEncoder(hidden_size=embeddings.embedding_size, heads=units_heads, num_layers=units_layers, dim_feedforward=dim_feedforward, glu_depth=units_glu_depth, dropout=dropout) self.chunk_encoder = TransformerEncoder(hidden_size=embeddings.embedding_size, heads=chunks_heads, num_layers=chunks_layers, dim_feedforward=dim_feedforward, glu_depth=chunks_glu_depth, dropout=dropout) @classmethod def from_opt(cls, opt, embeddings): """Alternate constructor.""" dropout = opt.dropout[0] if type(opt.dropout) is list else opt.dropout if isinstance(opt.enc_layers, int) and opt.enc_layers > 0: print('opt.enc_layers is specified, over-riding units_layers and chunks_layers') opt.units_layers = opt.enc_layers opt.chunks_layers = opt.enc_layers if isinstance(opt.heads, int) and opt.enc_layers > 0: print('opt.heads is specified, over-riding units_heads and chunks_heads') opt.units_heads = opt.heads opt.chunks_heads = opt.heads if isinstance(opt.glu_depth, int) and opt.glu_depth > 0: print('opt.glu_depth is specified, over-riding units_glu_depth and chunks_glu_depth') opt.units_glu_depth = opt.glu_depth opt.chunks_glu_depth = opt.glu_depth return cls( embeddings=embeddings, units_layers=opt.units_layers, chunks_layers=opt.chunks_layers, units_heads=opt.units_heads, chunks_heads=opt.chunks_heads, dim_feedforward=opt.transformer_ff, units_glu_depth=opt.units_glu_depth, chunks_glu_depth=opt.chunks_glu_depth, dropout=dropout ) def forward(self, src, lengths=None): """ See :func:`EncoderBase.forward()` src (tensor) [seq_len, bs, 2] 2 <-- (value, type) """ self._check_args(src, lengths) seq_len, bsz, _ = src.shape n_ents = seq_len // self.ent_size # sanity check assert seq_len % n_ents == 0 assert seq_len == lengths.max() # We build the masks for self attention and decoding eye = block_eye(n_ents, self.ent_size).to(src.device) self_attn_mask = torch.full((seq_len, seq_len), float('-inf')).to(src.device) self_attn_mask.masked_fill_(eye.to(src.device), 0) unit_mask = build_pad_mask(src, self.ent_size, self.embeddings.word_padding_idx).to(src.device) chunk_mask = build_chunk_mask(lengths, self.ent_size).to(src.device) # embs [seq_len, bs, hidden_size] embs, pos_embs = self.embeddings(src) _check_for_nan(embs, 'after embedding layer') _check_for_nan(pos_embs, 'after embedding layer') # units [seq_len, bs, hidden_size] units = self.unit_encoder(embs, mask=self_attn_mask) # chunks & units_tokens [n_units, bs, hidden_size] units_tokens = units[range(0, seq_len, self.ent_size), :, :] chunks = self.chunk_encoder(units_tokens, mask=chunk_mask) # memory bank every thing we want to pass to the decoder # all tensors should have dim(1) be the batch size memory_bank = ( chunks, units, pos_embs, unit_mask.transpose(0, 1), chunk_mask[:, 0, :].unsqueeze(0).eq(float('-inf')) ) # We average the units representation to give a final encoding # and be inline with the onmt framework encoder_final = chunks.mean(dim=0).unsqueeze(0) # encoder_final = (encoder_final, encoder_final) return encoder_final, memory_bank, lengths def update_dropout(self, dropout): self.unit_encoder.update_dropout(dropout) self.chunk_encoder.update_dropout(dropout)
10,534
39.833333
103
py
data-to-text-hierarchical
data-to-text-hierarchical-master/onmt/encoders/mean_encoder.py
"""Define a minimal encoder.""" from onmt.encoders.encoder import EncoderBase from onmt.utils.misc import sequence_mask import torch class MeanEncoder(EncoderBase): """A trivial non-recurrent encoder. Simply applies mean pooling. Args: num_layers (int): number of replicated layers embeddings (onmt.modules.Embeddings): embedding module to use """ def __init__(self, num_layers, embeddings): super(MeanEncoder, self).__init__() self.num_layers = num_layers self.embeddings = embeddings @classmethod def from_opt(cls, opt, embeddings): """Alternate constructor.""" return cls( opt.enc_layers, embeddings) def forward(self, src, lengths=None): """See :func:`EncoderBase.forward()`""" self._check_args(src, lengths) emb = self.embeddings(src) _, batch, emb_dim = emb.size() if lengths is not None: # we avoid padding while mean pooling mask = sequence_mask(lengths).float() mask = mask / lengths.unsqueeze(1).float() mean = torch.bmm(mask.unsqueeze(1), emb.transpose(0, 1)).squeeze(1) else: mean = emb.mean(0) mean = mean.expand(self.num_layers, batch, emb_dim) memory_bank = emb encoder_final = (mean, mean) return encoder_final, memory_bank, lengths
1,404
29.543478
79
py
data-to-text-hierarchical
data-to-text-hierarchical-master/onmt/encoders/image_encoder.py
"""Image Encoder.""" import torch.nn as nn import torch.nn.functional as F import torch from onmt.encoders.encoder import EncoderBase class ImageEncoder(EncoderBase): """A simple encoder CNN -> RNN for image src. Args: num_layers (int): number of encoder layers. bidirectional (bool): bidirectional encoder. rnn_size (int): size of hidden states of the rnn. dropout (float): dropout probablity. """ def __init__(self, num_layers, bidirectional, rnn_size, dropout, image_chanel_size=3): super(ImageEncoder, self).__init__() self.num_layers = num_layers self.num_directions = 2 if bidirectional else 1 self.hidden_size = rnn_size self.layer1 = nn.Conv2d(image_chanel_size, 64, kernel_size=(3, 3), padding=(1, 1), stride=(1, 1)) self.layer2 = nn.Conv2d(64, 128, kernel_size=(3, 3), padding=(1, 1), stride=(1, 1)) self.layer3 = nn.Conv2d(128, 256, kernel_size=(3, 3), padding=(1, 1), stride=(1, 1)) self.layer4 = nn.Conv2d(256, 256, kernel_size=(3, 3), padding=(1, 1), stride=(1, 1)) self.layer5 = nn.Conv2d(256, 512, kernel_size=(3, 3), padding=(1, 1), stride=(1, 1)) self.layer6 = nn.Conv2d(512, 512, kernel_size=(3, 3), padding=(1, 1), stride=(1, 1)) self.batch_norm1 = nn.BatchNorm2d(256) self.batch_norm2 = nn.BatchNorm2d(512) self.batch_norm3 = nn.BatchNorm2d(512) src_size = 512 dropout = dropout[0] if type(dropout) is list else dropout self.rnn = nn.LSTM(src_size, int(rnn_size / self.num_directions), num_layers=num_layers, dropout=dropout, bidirectional=bidirectional) self.pos_lut = nn.Embedding(1000, src_size) @classmethod def from_opt(cls, opt, embeddings=None): """Alternate constructor.""" if embeddings is not None: raise ValueError("Cannot use embeddings with ImageEncoder.") # why is the model_opt.__dict__ check necessary? if "image_channel_size" not in opt.__dict__: image_channel_size = 3 else: image_channel_size = opt.image_channel_size return cls( opt.enc_layers, opt.brnn, opt.enc_rnn_size, opt.dropout[0] if type(opt.dropout) is list else opt.dropout, image_channel_size ) def load_pretrained_vectors(self, opt): """Pass in needed options only when modify function definition.""" pass def forward(self, src, lengths=None): """See :func:`onmt.encoders.encoder.EncoderBase.forward()`""" batch_size = src.size(0) # (batch_size, 64, imgH, imgW) # layer 1 src = F.relu(self.layer1(src[:, :, :, :] - 0.5), True) # (batch_size, 64, imgH/2, imgW/2) src = F.max_pool2d(src, kernel_size=(2, 2), stride=(2, 2)) # (batch_size, 128, imgH/2, imgW/2) # layer 2 src = F.relu(self.layer2(src), True) # (batch_size, 128, imgH/2/2, imgW/2/2) src = F.max_pool2d(src, kernel_size=(2, 2), stride=(2, 2)) # (batch_size, 256, imgH/2/2, imgW/2/2) # layer 3 # batch norm 1 src = F.relu(self.batch_norm1(self.layer3(src)), True) # (batch_size, 256, imgH/2/2, imgW/2/2) # layer4 src = F.relu(self.layer4(src), True) # (batch_size, 256, imgH/2/2/2, imgW/2/2) src = F.max_pool2d(src, kernel_size=(1, 2), stride=(1, 2)) # (batch_size, 512, imgH/2/2/2, imgW/2/2) # layer 5 # batch norm 2 src = F.relu(self.batch_norm2(self.layer5(src)), True) # (batch_size, 512, imgH/2/2/2, imgW/2/2/2) src = F.max_pool2d(src, kernel_size=(2, 1), stride=(2, 1)) # (batch_size, 512, imgH/2/2/2, imgW/2/2/2) src = F.relu(self.batch_norm3(self.layer6(src)), True) # # (batch_size, 512, H, W) all_outputs = [] for row in range(src.size(2)): inp = src[:, :, row, :].transpose(0, 2) \ .transpose(1, 2) row_vec = torch.Tensor(batch_size).type_as(inp.data) \ .long().fill_(row) pos_emb = self.pos_lut(row_vec) with_pos = torch.cat( (pos_emb.view(1, pos_emb.size(0), pos_emb.size(1)), inp), 0) outputs, hidden_t = self.rnn(with_pos) all_outputs.append(outputs) out = torch.cat(all_outputs, 0) return hidden_t, out, lengths def update_dropout(self, dropout): self.rnn.dropout = dropout
4,839
35.666667
76
py
data-to-text-hierarchical
data-to-text-hierarchical-master/onmt/encoders/rnn_encoder.py
"""Define RNN-based encoders.""" import torch.nn as nn import torch.nn.functional as F from torch.nn.utils.rnn import pack_padded_sequence as pack from torch.nn.utils.rnn import pad_packed_sequence as unpack from onmt.encoders.encoder import EncoderBase from onmt.utils.rnn_factory import rnn_factory class RNNEncoder(EncoderBase): """ A generic recurrent neural network encoder. Args: rnn_type (str): style of recurrent unit to use, one of [RNN, LSTM, GRU, SRU] bidirectional (bool) : use a bidirectional RNN num_layers (int) : number of stacked layers hidden_size (int) : hidden size of each layer dropout (float) : dropout value for :class:`torch.nn.Dropout` embeddings (onmt.modules.Embeddings): embedding module to use """ def __init__(self, rnn_type, bidirectional, num_layers, hidden_size, dropout=0.0, embeddings=None, use_bridge=False): super(RNNEncoder, self).__init__() assert embeddings is not None num_directions = 2 if bidirectional else 1 assert hidden_size % num_directions == 0 hidden_size = hidden_size // num_directions self.embeddings = embeddings self.rnn, self.no_pack_padded_seq = \ rnn_factory(rnn_type, input_size=embeddings.embedding_size, hidden_size=hidden_size, num_layers=num_layers, dropout=dropout, bidirectional=bidirectional) # Initialize the bridge layer self.use_bridge = use_bridge if self.use_bridge: self._initialize_bridge(rnn_type, hidden_size, num_layers) @classmethod def from_opt(cls, opt, embeddings): """Alternate constructor.""" return cls( opt.rnn_type, opt.brnn, opt.enc_layers, opt.enc_rnn_size, opt.dropout[0] if type(opt.dropout) is list else opt.dropout, embeddings, opt.bridge) def forward(self, src, lengths=None): """See :func:`EncoderBase.forward()`""" self._check_args(src, lengths) emb = self.embeddings(src) # s_len, batch, emb_dim = emb.size() packed_emb = emb if lengths is not None and not self.no_pack_padded_seq: # Lengths data is wrapped inside a Tensor. lengths_list = lengths.view(-1).tolist() packed_emb = pack(emb, lengths_list) memory_bank, encoder_final = self.rnn(packed_emb) if lengths is not None and not self.no_pack_padded_seq: memory_bank = unpack(memory_bank)[0] if self.use_bridge: encoder_final = self._bridge(encoder_final) return encoder_final, memory_bank, lengths def _initialize_bridge(self, rnn_type, hidden_size, num_layers): # LSTM has hidden and cell state, other only one number_of_states = 2 if rnn_type == "LSTM" else 1 # Total number of states self.total_hidden_dim = hidden_size * num_layers # Build a linear layer for each self.bridge = nn.ModuleList([nn.Linear(self.total_hidden_dim, self.total_hidden_dim, bias=True) for _ in range(number_of_states)]) def _bridge(self, hidden): """Forward hidden state through bridge.""" def bottle_hidden(linear, states): """ Transform from 3D to 2D, apply linear and return initial size """ size = states.size() result = linear(states.view(-1, self.total_hidden_dim)) return F.relu(result).view(size) if isinstance(hidden, tuple): # LSTM outs = tuple([bottle_hidden(layer, hidden[ix]) for ix, layer in enumerate(self.bridge)]) else: outs = bottle_hidden(self.bridge[0], hidden) return outs def update_dropout(self, dropout): self.rnn.dropout = dropout
4,274
34.92437
73
py
data-to-text-hierarchical
data-to-text-hierarchical-master/onmt/encoders/audio_encoder.py
"""Audio encoder""" import math import torch.nn as nn from torch.nn.utils.rnn import pack_padded_sequence as pack from torch.nn.utils.rnn import pad_packed_sequence as unpack from onmt.utils.rnn_factory import rnn_factory from onmt.encoders.encoder import EncoderBase class AudioEncoder(EncoderBase): """A simple encoder CNN -> RNN for audio input. Args: rnn_type (str): Type of RNN (e.g. GRU, LSTM, etc). enc_layers (int): Number of encoder layers. dec_layers (int): Number of decoder layers. brnn (bool): Bidirectional encoder. enc_rnn_size (int): Size of hidden states of the rnn. dec_rnn_size (int): Size of the decoder hidden states. enc_pooling (str): A comma separated list either of length 1 or of length ``enc_layers`` specifying the pooling amount. dropout (float): dropout probablity. sample_rate (float): input spec window_size (int): input spec """ def __init__(self, rnn_type, enc_layers, dec_layers, brnn, enc_rnn_size, dec_rnn_size, enc_pooling, dropout, sample_rate, window_size): super(AudioEncoder, self).__init__() self.enc_layers = enc_layers self.rnn_type = rnn_type self.dec_layers = dec_layers num_directions = 2 if brnn else 1 self.num_directions = num_directions assert enc_rnn_size % num_directions == 0 enc_rnn_size_real = enc_rnn_size // num_directions assert dec_rnn_size % num_directions == 0 self.dec_rnn_size = dec_rnn_size dec_rnn_size_real = dec_rnn_size // num_directions self.dec_rnn_size_real = dec_rnn_size_real self.dec_rnn_size = dec_rnn_size input_size = int(math.floor((sample_rate * window_size) / 2) + 1) enc_pooling = enc_pooling.split(',') assert len(enc_pooling) == enc_layers or len(enc_pooling) == 1 if len(enc_pooling) == 1: enc_pooling = enc_pooling * enc_layers enc_pooling = [int(p) for p in enc_pooling] self.enc_pooling = enc_pooling if type(dropout) is not list: dropout = [dropout] if max(dropout) > 0: self.dropout = nn.Dropout(dropout[0]) else: self.dropout = None self.W = nn.Linear(enc_rnn_size, dec_rnn_size, bias=False) self.batchnorm_0 = nn.BatchNorm1d(enc_rnn_size, affine=True) self.rnn_0, self.no_pack_padded_seq = \ rnn_factory(rnn_type, input_size=input_size, hidden_size=enc_rnn_size_real, num_layers=1, dropout=dropout[0], bidirectional=brnn) self.pool_0 = nn.MaxPool1d(enc_pooling[0]) for l in range(enc_layers - 1): batchnorm = nn.BatchNorm1d(enc_rnn_size, affine=True) rnn, _ = \ rnn_factory(rnn_type, input_size=enc_rnn_size, hidden_size=enc_rnn_size_real, num_layers=1, dropout=dropout[0], bidirectional=brnn) setattr(self, 'rnn_%d' % (l + 1), rnn) setattr(self, 'pool_%d' % (l + 1), nn.MaxPool1d(enc_pooling[l + 1])) setattr(self, 'batchnorm_%d' % (l + 1), batchnorm) @classmethod def from_opt(cls, opt, embeddings=None): """Alternate constructor.""" if embeddings is not None: raise ValueError("Cannot use embeddings with AudioEncoder.") return cls( opt.rnn_type, opt.enc_layers, opt.dec_layers, opt.brnn, opt.enc_rnn_size, opt.dec_rnn_size, opt.audio_enc_pooling, opt.dropout, opt.sample_rate, opt.window_size) def forward(self, src, lengths=None): """See :func:`onmt.encoders.encoder.EncoderBase.forward()`""" batch_size, _, nfft, t = src.size() src = src.transpose(0, 1).transpose(0, 3).contiguous() \ .view(t, batch_size, nfft) orig_lengths = lengths lengths = lengths.view(-1).tolist() for l in range(self.enc_layers): rnn = getattr(self, 'rnn_%d' % l) pool = getattr(self, 'pool_%d' % l) batchnorm = getattr(self, 'batchnorm_%d' % l) stride = self.enc_pooling[l] packed_emb = pack(src, lengths) memory_bank, tmp = rnn(packed_emb) memory_bank = unpack(memory_bank)[0] t, _, _ = memory_bank.size() memory_bank = memory_bank.transpose(0, 2) memory_bank = pool(memory_bank) lengths = [int(math.floor((length - stride) / stride + 1)) for length in lengths] memory_bank = memory_bank.transpose(0, 2) src = memory_bank t, _, num_feat = src.size() src = batchnorm(src.contiguous().view(-1, num_feat)) src = src.view(t, -1, num_feat) if self.dropout and l + 1 != self.enc_layers: src = self.dropout(src) memory_bank = memory_bank.contiguous().view(-1, memory_bank.size(2)) memory_bank = self.W(memory_bank).view(-1, batch_size, self.dec_rnn_size) state = memory_bank.new_full((self.dec_layers * self.num_directions, batch_size, self.dec_rnn_size_real), 0) if self.rnn_type == 'LSTM': # The encoder hidden is (layers*directions) x batch x dim. encoder_final = (state, state) else: encoder_final = state return encoder_final, memory_bank, orig_lengths.new_tensor(lengths) def update_dropout(self, dropout): self.dropout.p = dropout for i in range(self.enc_layers - 1): getattr(self, 'rnn_%d' % i).dropout = dropout
6,055
40.197279
77
py
data-to-text-hierarchical
data-to-text-hierarchical-master/onmt/encoders/encoder.py
"""Base class for encoders and generic multi encoders.""" import torch.nn as nn from onmt.utils.misc import aeq class EncoderBase(nn.Module): """ Base encoder class. Specifies the interface used by different encoder types and required by :class:`onmt.Models.NMTModel`. .. mermaid:: graph BT A[Input] subgraph RNN C[Pos 1] D[Pos 2] E[Pos N] end F[Memory_Bank] G[Final] A-->C A-->D A-->E C-->F D-->F E-->F E-->G """ @classmethod def from_opt(cls, opt, embeddings=None): raise NotImplementedError def _check_args(self, src, lengths=None, hidden=None): n_batch = src.size(1) if lengths is not None: n_batch_, = lengths.size() aeq(n_batch, n_batch_) def forward(self, src, lengths=None): """ Args: src (LongTensor): padded sequences of sparse indices ``(src_len, batch, nfeat)`` lengths (LongTensor): length of each sequence ``(batch,)`` Returns: (FloatTensor, FloatTensor): * final encoder state, used to initialize decoder * memory bank for attention, ``(src_len, batch, hidden)`` """ raise NotImplementedError
1,383
22.457627
79
py
data-to-text-hierarchical
data-to-text-hierarchical-master/onmt/encoders/transformer.py
""" Implementation of "Attention is All You Need" """ import torch.nn as nn from onmt.encoders.encoder import EncoderBase from onmt.modules import MultiHeadedAttention from onmt.modules.position_ffn import PositionwiseFeedForward from onmt.utils.misc import sequence_mask class TransformerEncoderLayer(nn.Module): """ A single layer of the transformer encoder. Args: d_model (int): the dimension of keys/values/queries in MultiHeadedAttention, also the input size of the first-layer of the PositionwiseFeedForward. heads (int): the number of head for MultiHeadedAttention. d_ff (int): the second-layer of the PositionwiseFeedForward. dropout (float): dropout probability(0-1.0). """ def __init__(self, d_model, heads, d_ff, dropout, attention_dropout, max_relative_positions=0): super(TransformerEncoderLayer, self).__init__() self.self_attn = MultiHeadedAttention( heads, d_model, dropout=attention_dropout, max_relative_positions=max_relative_positions) self.feed_forward = PositionwiseFeedForward(d_model, d_ff, dropout) self.layer_norm = nn.LayerNorm(d_model, eps=1e-6) self.dropout = nn.Dropout(dropout) def forward(self, inputs, mask): """ Args: inputs (FloatTensor): ``(batch_size, src_len, model_dim)`` mask (LongTensor): ``(batch_size, 1, src_len)`` Returns: (FloatTensor): * outputs ``(batch_size, src_len, model_dim)`` """ input_norm = self.layer_norm(inputs) context, _ = self.self_attn(input_norm, input_norm, input_norm, mask=mask, attn_type="self") out = self.dropout(context) + inputs return self.feed_forward(out) def update_dropout(self, dropout, attention_dropout): self.self_attn.update_dropout(attention_dropout) self.feed_forward.update_dropout(dropout) self.dropout.p = dropout class TransformerEncoder(EncoderBase): """The Transformer encoder from "Attention is All You Need" :cite:`DBLP:journals/corr/VaswaniSPUJGKP17` .. mermaid:: graph BT A[input] B[multi-head self-attn] C[feed forward] O[output] A --> B B --> C C --> O Args: num_layers (int): number of encoder layers d_model (int): size of the model heads (int): number of heads d_ff (int): size of the inner FF layer dropout (float): dropout parameters embeddings (onmt.modules.Embeddings): embeddings to use, should have positional encodings Returns: (torch.FloatTensor, torch.FloatTensor): * embeddings ``(src_len, batch_size, model_dim)`` * memory_bank ``(src_len, batch_size, model_dim)`` """ def __init__(self, num_layers, d_model, heads, d_ff, dropout, attention_dropout, embeddings, max_relative_positions): super(TransformerEncoder, self).__init__() self.embeddings = embeddings self.transformer = nn.ModuleList( [TransformerEncoderLayer( d_model, heads, d_ff, dropout, attention_dropout, max_relative_positions=max_relative_positions) for i in range(num_layers)]) self.layer_norm = nn.LayerNorm(d_model, eps=1e-6) @classmethod def from_opt(cls, opt, embeddings): """Alternate constructor.""" return cls( opt.enc_layers, opt.enc_rnn_size, opt.heads, opt.transformer_ff, opt.dropout[0] if type(opt.dropout) is list else opt.dropout, opt.attention_dropout[0] if type(opt.attention_dropout) is list else opt.attention_dropout, embeddings, opt.max_relative_positions) def forward(self, src, lengths=None): """See :func:`EncoderBase.forward()`""" self._check_args(src, lengths) emb = self.embeddings(src) out = emb.transpose(0, 1).contiguous() mask = ~sequence_mask(lengths).unsqueeze(1) # Run the forward pass of every layer of the tranformer. for layer in self.transformer: out = layer(out, mask) out = self.layer_norm(out) return emb, out.transpose(0, 1).contiguous(), lengths def update_dropout(self, dropout, attention_dropout): self.embeddings.update_dropout(dropout) for layer in self.transformer: layer.update_dropout(dropout, attention_dropout)
4,661
33.279412
75
py
data-to-text-hierarchical
data-to-text-hierarchical-master/onmt/encoders/cnn_encoder.py
""" Implementation of "Convolutional Sequence to Sequence Learning" """ import torch.nn as nn from onmt.encoders.encoder import EncoderBase from onmt.utils.cnn_factory import shape_transform, StackedCNN SCALE_WEIGHT = 0.5 ** 0.5 class CNNEncoder(EncoderBase): """Encoder based on "Convolutional Sequence to Sequence Learning" :cite:`DBLP:journals/corr/GehringAGYD17`. """ def __init__(self, num_layers, hidden_size, cnn_kernel_width, dropout, embeddings): super(CNNEncoder, self).__init__() self.embeddings = embeddings input_size = embeddings.embedding_size self.linear = nn.Linear(input_size, hidden_size) self.cnn = StackedCNN(num_layers, hidden_size, cnn_kernel_width, dropout) @classmethod def from_opt(cls, opt, embeddings): """Alternate constructor.""" return cls( opt.enc_layers, opt.enc_rnn_size, opt.cnn_kernel_width, opt.dropout[0] if type(opt.dropout) is list else opt.dropout, embeddings) def forward(self, input, lengths=None, hidden=None): """See :class:`onmt.modules.EncoderBase.forward()`""" self._check_args(input, lengths, hidden) emb = self.embeddings(input) # s_len, batch, emb_dim = emb.size() emb = emb.transpose(0, 1).contiguous() emb_reshape = emb.view(emb.size(0) * emb.size(1), -1) emb_remap = self.linear(emb_reshape) emb_remap = emb_remap.view(emb.size(0), emb.size(1), -1) emb_remap = shape_transform(emb_remap) out = self.cnn(emb_remap) return emb_remap.squeeze(3).transpose(0, 1).contiguous(), \ out.squeeze(3).transpose(0, 1).contiguous(), lengths def update_dropout(self, dropout): self.cnn.dropout.p = dropout
1,860
32.232143
73
py
data-to-text-hierarchical
data-to-text-hierarchical-master/onmt/translate/translation_server.py
#!/usr/bin/env python """REST Translation server.""" from __future__ import print_function import codecs import sys import os import time import json import threading import re import traceback import importlib import torch import onmt.opts from onmt.utils.logging import init_logger from onmt.utils.misc import set_random_seed from onmt.utils.misc import check_model_config from onmt.utils.alignment import to_word_align from onmt.utils.parse import ArgumentParser from onmt.translate.translator import build_translator def critical(func): """Decorator for critical section (mutually exclusive code)""" def wrapper(server_model, *args, **kwargs): if sys.version_info[0] == 3: if not server_model.running_lock.acquire(True, 120): raise ServerModelError("Model %d running lock timeout" % server_model.model_id) else: # semaphore doesn't have a timeout arg in Python 2.7 server_model.running_lock.acquire(True) try: o = func(server_model, *args, **kwargs) except (Exception, RuntimeError): server_model.running_lock.release() raise server_model.running_lock.release() return o return wrapper class Timer: def __init__(self, start=False): self.stime = -1 self.prev = -1 self.times = {} if start: self.start() def start(self): self.stime = time.time() self.prev = self.stime self.times = {} def tick(self, name=None, tot=False): t = time.time() if not tot: elapsed = t - self.prev else: elapsed = t - self.stime self.prev = t if name is not None: self.times[name] = elapsed return elapsed class ServerModelError(Exception): pass class TranslationServer(object): def __init__(self): self.models = {} self.next_id = 0 def start(self, config_file): """Read the config file and pre-/load the models.""" self.config_file = config_file with open(self.config_file) as f: self.confs = json.load(f) self.models_root = self.confs.get('models_root', './available_models') for i, conf in enumerate(self.confs["models"]): if "models" not in conf: if "model" in conf: # backwards compatibility for confs conf["models"] = [conf["model"]] else: raise ValueError("""Incorrect config file: missing 'models' parameter for model #%d""" % i) check_model_config(conf, self.models_root) kwargs = {'timeout': conf.get('timeout', None), 'load': conf.get('load', None), 'preprocess_opt': conf.get('preprocess', None), 'tokenizer_opt': conf.get('tokenizer', None), 'postprocess_opt': conf.get('postprocess', None), 'on_timeout': conf.get('on_timeout', None), 'model_root': conf.get('model_root', self.models_root) } kwargs = {k: v for (k, v) in kwargs.items() if v is not None} model_id = conf.get("id", None) opt = conf["opt"] opt["models"] = conf["models"] self.preload_model(opt, model_id=model_id, **kwargs) def clone_model(self, model_id, opt, timeout=-1): """Clone a model `model_id`. Different options may be passed. If `opt` is None, it will use the same set of options """ if model_id in self.models: if opt is None: opt = self.models[model_id].user_opt opt["models"] = self.models[model_id].opt.models return self.load_model(opt, timeout) else: raise ServerModelError("No such model '%s'" % str(model_id)) def load_model(self, opt, model_id=None, **model_kwargs): """Load a model given a set of options """ model_id = self.preload_model(opt, model_id=model_id, **model_kwargs) load_time = self.models[model_id].load_time return model_id, load_time def preload_model(self, opt, model_id=None, **model_kwargs): """Preloading the model: updating internal datastructure It will effectively load the model if `load` is set """ if model_id is not None: if model_id in self.models.keys(): raise ValueError("Model ID %d already exists" % model_id) else: model_id = self.next_id while model_id in self.models.keys(): model_id += 1 self.next_id = model_id + 1 print("Pre-loading model %d" % model_id) model = ServerModel(opt, model_id, **model_kwargs) self.models[model_id] = model return model_id def run(self, inputs): """Translate `inputs` We keep the same format as the Lua version i.e. ``[{"id": model_id, "src": "sequence to translate"},{ ...}]`` We use inputs[0]["id"] as the model id """ model_id = inputs[0].get("id", 0) if model_id in self.models and self.models[model_id] is not None: return self.models[model_id].run(inputs) else: print("Error No such model '%s'" % str(model_id)) raise ServerModelError("No such model '%s'" % str(model_id)) def unload_model(self, model_id): """Manually unload a model. It will free the memory and cancel the timer """ if model_id in self.models and self.models[model_id] is not None: self.models[model_id].unload() else: raise ServerModelError("No such model '%s'" % str(model_id)) def list_models(self): """Return the list of available models """ models = [] for _, model in self.models.items(): models += [model.to_dict()] return models class ServerModel(object): """Wrap a model with server functionality. Args: opt (dict): Options for the Translator model_id (int): Model ID preprocess_opt (list): Options for preprocess processus or None (extend for CJK) tokenizer_opt (dict): Options for the tokenizer or None postprocess_opt (list): Options for postprocess processus or None (extend for CJK) load (bool): whether to load the model during :func:`__init__()` timeout (int): Seconds before running :func:`do_timeout()` Negative values means no timeout on_timeout (str): Options are ["to_cpu", "unload"]. Set what to do on timeout (see :func:`do_timeout()`.) model_root (str): Path to the model directory it must contain the model and tokenizer file """ def __init__(self, opt, model_id, preprocess_opt=None, tokenizer_opt=None, postprocess_opt=None, load=False, timeout=-1, on_timeout="to_cpu", model_root="./"): self.model_root = model_root self.opt = self.parse_opt(opt) self.model_id = model_id self.preprocess_opt = preprocess_opt self.tokenizer_opt = tokenizer_opt self.postprocess_opt = postprocess_opt self.timeout = timeout self.on_timeout = on_timeout self.unload_timer = None self.user_opt = opt self.tokenizer = None if len(self.opt.log_file) > 0: log_file = os.path.join(model_root, self.opt.log_file) else: log_file = None self.logger = init_logger(log_file=log_file, log_file_level=self.opt.log_file_level) self.loading_lock = threading.Event() self.loading_lock.set() self.running_lock = threading.Semaphore(value=1) set_random_seed(self.opt.seed, self.opt.cuda) if load: self.load() def parse_opt(self, opt): """Parse the option set passed by the user using `onmt.opts` Args: opt (dict): Options passed by the user Returns: opt (argparse.Namespace): full set of options for the Translator """ prec_argv = sys.argv sys.argv = sys.argv[:1] parser = ArgumentParser() onmt.opts.translate_opts(parser) models = opt['models'] if not isinstance(models, (list, tuple)): models = [models] opt['models'] = [os.path.join(self.model_root, model) for model in models] opt['src'] = "dummy_src" for (k, v) in opt.items(): if k == 'models': sys.argv += ['-model'] sys.argv += [str(model) for model in v] elif type(v) == bool: sys.argv += ['-%s' % k] else: sys.argv += ['-%s' % k, str(v)] opt = parser.parse_args() ArgumentParser.validate_translate_opts(opt) opt.cuda = opt.gpu > -1 sys.argv = prec_argv return opt @property def loaded(self): return hasattr(self, 'translator') def load(self): self.loading_lock.clear() timer = Timer() self.logger.info("Loading model %d" % self.model_id) timer.start() try: self.translator = build_translator(self.opt, report_score=False, out_file=codecs.open( os.devnull, "w", "utf-8")) except RuntimeError as e: raise ServerModelError("Runtime Error: %s" % str(e)) timer.tick("model_loading") if self.preprocess_opt is not None: self.logger.info("Loading preprocessor") self.preprocessor = [] for function_path in self.preprocess_opt: function = get_function_by_path(function_path) self.preprocessor.append(function) if self.tokenizer_opt is not None: self.logger.info("Loading tokenizer") if "type" not in self.tokenizer_opt: raise ValueError( "Missing mandatory tokenizer option 'type'") if self.tokenizer_opt['type'] == 'sentencepiece': if "model" not in self.tokenizer_opt: raise ValueError( "Missing mandatory tokenizer option 'model'") import sentencepiece as spm sp = spm.SentencePieceProcessor() model_path = os.path.join(self.model_root, self.tokenizer_opt['model']) sp.Load(model_path) self.tokenizer = sp elif self.tokenizer_opt['type'] == 'pyonmttok': if "params" not in self.tokenizer_opt: raise ValueError( "Missing mandatory tokenizer option 'params'") import pyonmttok if self.tokenizer_opt["mode"] is not None: mode = self.tokenizer_opt["mode"] else: mode = None # load can be called multiple times: modify copy tokenizer_params = dict(self.tokenizer_opt["params"]) for key, value in self.tokenizer_opt["params"].items(): if key.endswith("path"): tokenizer_params[key] = os.path.join( self.model_root, value) tokenizer = pyonmttok.Tokenizer(mode, **tokenizer_params) self.tokenizer = tokenizer else: raise ValueError("Invalid value for tokenizer type") if self.postprocess_opt is not None: self.logger.info("Loading postprocessor") self.postprocessor = [] for function_path in self.postprocess_opt: function = get_function_by_path(function_path) self.postprocessor.append(function) self.load_time = timer.tick() self.reset_unload_timer() self.loading_lock.set() @critical def run(self, inputs): """Translate `inputs` using this model Args: inputs (List[dict[str, str]]): [{"src": "..."},{"src": ...}] Returns: result (list): translations times (dict): containing times """ self.stop_unload_timer() timer = Timer() timer.start() self.logger.info("Running translation using %d" % self.model_id) if not self.loading_lock.is_set(): self.logger.info( "Model #%d is being loaded by another thread, waiting" % self.model_id) if not self.loading_lock.wait(timeout=30): raise ServerModelError("Model %d loading timeout" % self.model_id) else: if not self.loaded: self.load() timer.tick(name="load") elif self.opt.cuda: self.to_gpu() timer.tick(name="to_gpu") texts = [] head_spaces = [] tail_spaces = [] sslength = [] for i, inp in enumerate(inputs): src = inp['src'] if src.strip() == "": head_spaces.append(src) texts.append("") tail_spaces.append("") else: whitespaces_before, whitespaces_after = "", "" match_before = re.search(r'^\s+', src) match_after = re.search(r'\s+$', src) if match_before is not None: whitespaces_before = match_before.group(0) if match_after is not None: whitespaces_after = match_after.group(0) head_spaces.append(whitespaces_before) preprocessed_src = self.maybe_preprocess(src.strip()) tok = self.maybe_tokenize(preprocessed_src) texts.append(tok) sslength.append(len(tok.split())) tail_spaces.append(whitespaces_after) empty_indices = [i for i, x in enumerate(texts) if x == ""] texts_to_translate = [x for x in texts if x != ""] scores = [] predictions = [] if len(texts_to_translate) > 0: try: scores, predictions = self.translator.translate( texts_to_translate, batch_size=len(texts_to_translate) if self.opt.batch_size == 0 else self.opt.batch_size) except (RuntimeError, Exception) as e: err = "Error: %s" % str(e) self.logger.error(err) self.logger.error("repr(text_to_translate): " + repr(texts_to_translate)) self.logger.error("model: #%s" % self.model_id) self.logger.error("model opt: " + str(self.opt.__dict__)) self.logger.error(traceback.format_exc()) raise ServerModelError(err) timer.tick(name="translation") self.logger.info("""Using model #%d\t%d inputs \ttranslation time: %f""" % (self.model_id, len(texts), timer.times['translation'])) self.reset_unload_timer() # NOTE: translator returns lists of `n_best` list def flatten_list(_list): return sum(_list, []) tiled_texts = [t for t in texts_to_translate for _ in range(self.opt.n_best)] results = flatten_list(predictions) scores = [score_tensor.item() for score_tensor in flatten_list(scores)] results = [self.maybe_detokenize_with_align(result, src) for result, src in zip(results, tiled_texts)] aligns = [align for _, align in results] results = [self.maybe_postprocess(seq) for seq, _ in results] # build back results with empty texts for i in empty_indices: j = i * self.opt.n_best results = results[:j] + [""] * self.opt.n_best + results[j:] aligns = aligns[:j] + [None] * self.opt.n_best + aligns[j:] scores = scores[:j] + [0] * self.opt.n_best + scores[j:] head_spaces = [h for h in head_spaces for i in range(self.opt.n_best)] tail_spaces = [h for h in tail_spaces for i in range(self.opt.n_best)] results = ["".join(items) for items in zip(head_spaces, results, tail_spaces)] self.logger.info("Translation Results: %d", len(results)) return results, scores, self.opt.n_best, timer.times, aligns def do_timeout(self): """Timeout function that frees GPU memory. Moves the model to CPU or unloads it; depending on attr`self.on_timemout` value """ if self.on_timeout == "unload": self.logger.info("Timeout: unloading model %d" % self.model_id) self.unload() if self.on_timeout == "to_cpu": self.logger.info("Timeout: sending model %d to CPU" % self.model_id) self.to_cpu() @critical def unload(self): self.logger.info("Unloading model %d" % self.model_id) del self.translator if self.opt.cuda: torch.cuda.empty_cache() self.unload_timer = None def stop_unload_timer(self): if self.unload_timer is not None: self.unload_timer.cancel() def reset_unload_timer(self): if self.timeout < 0: return self.stop_unload_timer() self.unload_timer = threading.Timer(self.timeout, self.do_timeout) self.unload_timer.start() def to_dict(self): hide_opt = ["models", "src"] d = {"model_id": self.model_id, "opt": {k: self.user_opt[k] for k in self.user_opt.keys() if k not in hide_opt}, "models": self.user_opt["models"], "loaded": self.loaded, "timeout": self.timeout, } if self.tokenizer_opt is not None: d["tokenizer"] = self.tokenizer_opt return d @critical def to_cpu(self): """Move the model to CPU and clear CUDA cache.""" self.translator.model.cpu() if self.opt.cuda: torch.cuda.empty_cache() def to_gpu(self): """Move the model to GPU.""" torch.cuda.set_device(self.opt.gpu) self.translator.model.cuda() def maybe_preprocess(self, sequence): """Preprocess the sequence (or not) """ if self.preprocess_opt is not None: return self.preprocess(sequence) return sequence def preprocess(self, sequence): """Preprocess a single sequence. Args: sequence (str): The sequence to preprocess. Returns: sequence (str): The preprocessed sequence. """ if self.preprocessor is None: raise ValueError("No preprocessor loaded") for function in self.preprocessor: sequence = function(sequence) return sequence def maybe_tokenize(self, sequence): """Tokenize the sequence (or not). Same args/returns as `tokenize` """ if self.tokenizer_opt is not None: return self.tokenize(sequence) return sequence def tokenize(self, sequence): """Tokenize a single sequence. Args: sequence (str): The sequence to tokenize. Returns: tok (str): The tokenized sequence. """ if self.tokenizer is None: raise ValueError("No tokenizer loaded") if self.tokenizer_opt["type"] == "sentencepiece": tok = self.tokenizer.EncodeAsPieces(sequence) tok = " ".join(tok) elif self.tokenizer_opt["type"] == "pyonmttok": tok, _ = self.tokenizer.tokenize(sequence) tok = " ".join(tok) return tok @property def tokenizer_marker(self): marker = None tokenizer_type = self.tokenizer_opt.get('type', None) if tokenizer_type == "pyonmttok": params = self.tokenizer_opt.get('params', None) if params is not None: if params.get("joiner_annotate", None) is not None: marker = 'joiner' elif params.get("spacer_annotate", None) is not None: marker = 'spacer' elif tokenizer_type == "sentencepiece": marker = 'spacer' return marker def maybe_detokenize_with_align(self, sequence, src): """De-tokenize (or not) the sequence (with alignment). Args: sequence (str): The sequence to detokenize, possible with alignment seperate by ` ||| `. Returns: sequence (str): The detokenized sequence. align (str): The alignment correspand to detokenized src/tgt sorted or None if no alignment in output. """ align = None if self.opt.report_align: # output contain alignment sequence, align = sequence.split(' ||| ') align = self.maybe_convert_align(src, sequence, align) sequence = self.maybe_detokenize(sequence) return (sequence, align) def maybe_detokenize(self, sequence): """De-tokenize the sequence (or not) Same args/returns as :func:`tokenize()` """ if self.tokenizer_opt is not None and ''.join(sequence.split()) != '': return self.detokenize(sequence) return sequence def detokenize(self, sequence): """Detokenize a single sequence Same args/returns as :func:`tokenize()` """ if self.tokenizer is None: raise ValueError("No tokenizer loaded") if self.tokenizer_opt["type"] == "sentencepiece": detok = self.tokenizer.DecodePieces(sequence.split()) elif self.tokenizer_opt["type"] == "pyonmttok": detok = self.tokenizer.detokenize(sequence.split()) return detok def maybe_convert_align(self, src, tgt, align): """Convert alignment to match detokenized src/tgt (or not). Args: src (str): The tokenized source sequence. tgt (str): The tokenized target sequence. align (str): The alignment correspand to src/tgt pair. Returns: align (str): The alignment correspand to detokenized src/tgt. """ if self.tokenizer_marker is not None and ''.join(tgt.split()) != '': return to_word_align(src, tgt, align, mode=self.tokenizer_marker) return align def maybe_postprocess(self, sequence): """Postprocess the sequence (or not) """ if self.postprocess_opt is not None: return self.postprocess(sequence) return sequence def postprocess(self, sequence): """Preprocess a single sequence. Args: sequence (str): The sequence to process. Returns: sequence (str): The postprocessed sequence. """ if self.postprocessor is None: raise ValueError("No postprocessor loaded") for function in self.postprocessor: sequence = function(sequence) return sequence def get_function_by_path(path, args=[], kwargs={}): module_name = ".".join(path.split(".")[:-1]) function_name = path.split(".")[-1] try: module = importlib.import_module(module_name) except ValueError as e: print("Cannot import module '%s'" % module_name) raise e function = getattr(module, function_name) return function
24,269
33.72103
79
py
data-to-text-hierarchical
data-to-text-hierarchical-master/onmt/translate/decode_strategy.py
import torch class DecodeStrategy(object): """Base class for generation strategies. Args: pad (int): Magic integer in output vocab. bos (int): Magic integer in output vocab. eos (int): Magic integer in output vocab. batch_size (int): Current batch size. parallel_paths (int): Decoding strategies like beam search use parallel paths. Each batch is repeated ``parallel_paths`` times in relevant state tensors. min_length (int): Shortest acceptable generation, not counting begin-of-sentence or end-of-sentence. max_length (int): Longest acceptable sequence, not counting begin-of-sentence (presumably there has been no EOS yet if max_length is used as a cutoff). block_ngram_repeat (int): Block beams where ``block_ngram_repeat``-grams repeat. exclusion_tokens (set[int]): If a gram contains any of these tokens, it may repeat. return_attention (bool): Whether to work with attention too. If this is true, it is assumed that the decoder is attentional. Attributes: pad (int): See above. bos (int): See above. eos (int): See above. predictions (list[list[LongTensor]]): For each batch, holds a list of beam prediction sequences. scores (list[list[FloatTensor]]): For each batch, holds a list of scores. attention (list[list[FloatTensor or list[]]]): For each batch, holds a list of attention sequence tensors (or empty lists) having shape ``(step, inp_seq_len)`` where ``inp_seq_len`` is the length of the sample (not the max length of all inp seqs). alive_seq (LongTensor): Shape ``(B x parallel_paths, step)``. This sequence grows in the ``step`` axis on each call to :func:`advance()`. is_finished (ByteTensor or NoneType): Shape ``(B, parallel_paths)``. Initialized to ``None``. alive_attn (FloatTensor or NoneType): If tensor, shape is ``(step, B x parallel_paths, inp_seq_len)``, where ``inp_seq_len`` is the (max) length of the input sequence. min_length (int): See above. max_length (int): See above. block_ngram_repeat (int): See above. exclusion_tokens (set[int]): See above. return_attention (bool): See above. done (bool): See above. """ def __init__(self, pad, bos, eos, dot, batch_size, parallel_paths, min_length, block_ngram_repeat, exclusion_tokens, return_attention, max_length): # magic indices self.pad = pad self.bos = bos self.eos = eos self.dot = dot self.batch_size = batch_size self.parallel_paths = parallel_paths # result caching self.predictions = [[] for _ in range(batch_size)] self.scores = [[] for _ in range(batch_size)] self.attention = [[] for _ in range(batch_size)] self.alive_attn = None self.min_length = min_length self.max_length = max_length self.block_ngram_repeat = block_ngram_repeat n_paths = batch_size * parallel_paths self.forbidden_tokens = [dict() for _ in range(n_paths)] self.exclusion_tokens = exclusion_tokens self.return_attention = return_attention self.done = False def initialize(self, memory_bank, src_lengths, src_map=None, device=None): """DecodeStrategy subclasses should override :func:`initialize()`. `initialize` should be called before all actions. used to prepare necessary ingredients for decode. """ if device is None: device = torch.device('cpu') self.alive_seq = torch.full( [self.batch_size * self.parallel_paths, 1], self.bos, dtype=torch.long, device=device) self.is_finished = torch.zeros( [self.batch_size, self.parallel_paths], dtype=torch.uint8, device=device) return None, memory_bank, src_lengths, src_map def __len__(self): return self.alive_seq.shape[1] def ensure_min_length(self, log_probs): """ We set the proba of generating <eos> to 0 if the sequence is not long enough. We make sure that the sequence cannot end until the last token generated is self.dot. we get the idx of all rows where the last token is not self.dot we set self.eos to -1e20 for those idx """ if len(self) <= self.min_length: log_probs[:, self.eos] = -1e20 if self.dot is not None: idx = self.alive_seq[:, -1].ne(self.dot).nonzero().squeeze(1) log_probs[idx, self.eos] = -1e20 def ensure_max_length(self): # add one to account for BOS. Don't account for EOS because hitting # this implies it hasn't been found. if len(self) == self.max_length + 1: self.is_finished.fill_(1) def block_ngram_repeats(self, log_probs): """ We prevent the beam from going in any direction that would repeat any ngram of size <block_ngram_repeat> more thant once. The way we do it: we maintain a list of all ngrams of size <block_ngram_repeat> that is updated each time the beam advances, and manually put any token that would lead to a repeated ngram to 0. This improves on the previous version's complexity: - previous version's complexity: batch_size * beam_size * len(self) - current version's complexity: batch_size * beam_size This improves on the previous version's accuracy; - Previous version blocks the whole beam, whereas here we only block specific tokens. - Before the translation would fail when all beams contained repeated ngrams. This is sure to never happen here. """ # we don't block nothing if the user doesn't want it if self.block_ngram_repeat <= 0: return # we can't block nothing beam's too short if len(self) < self.block_ngram_repeat: return n = self.block_ngram_repeat - 1 for path_idx in range(self.alive_seq.shape[0]): # we check paths one by one current_ngram = tuple(self.alive_seq[path_idx, -n:].tolist()) forbidden_tokens = self.forbidden_tokens[path_idx].get( current_ngram, None) if forbidden_tokens is not None: log_probs[path_idx, list(forbidden_tokens)] = -10e20 def maybe_update_forbidden_tokens(self): """We complete and reorder the list of forbidden_tokens""" # we don't forbid nothing if the user doesn't want it if self.block_ngram_repeat <= 0: return # we can't forbid nothing if beam's too short if len(self) < self.block_ngram_repeat: return n = self.block_ngram_repeat forbidden_tokens = list() for path_idx, seq in zip(self.select_indices, self.alive_seq): # Reordering forbidden_tokens following beam selection # We rebuild a dict to ensure we get the value and not the pointer # Please note here that there is a mistake in the code and we should # use deepcopy instead of dict. However, at the time of writting the # paper I did not catch it and therefore I leave it as is here. forbidden_tokens.append( dict(self.forbidden_tokens[path_idx])) # Grabing the newly selected tokens and associated ngram current_ngram = tuple(seq[-n:].tolist()) # skip the blocking if any token in current_ngram is excluded if set(current_ngram) & self.exclusion_tokens: continue forbidden_tokens[-1].setdefault(current_ngram[:-1], set()) forbidden_tokens[-1][current_ngram[:-1]].add(current_ngram[-1]) self.forbidden_tokens = forbidden_tokens def advance(self, log_probs, attn): """DecodeStrategy subclasses should override :func:`advance()`. Advance is used to update ``self.alive_seq``, ``self.is_finished``, and, when appropriate, ``self.alive_attn``. """ raise NotImplementedError() def update_finished(self): """DecodeStrategy subclasses should override :func:`update_finished()`. ``update_finished`` is used to update ``self.predictions``, ``self.scores``, and other "output" attributes. """ raise NotImplementedError()
8,761
38.647059
80
py
data-to-text-hierarchical
data-to-text-hierarchical-master/onmt/translate/translation.py
""" Translation main class """ from __future__ import unicode_literals, print_function import torch from onmt.inputters.text_dataset import TextMultiField from onmt.utils.alignment import build_align_pharaoh class TranslationBuilder(object): """ Build a word-based translation from the batch output of translator and the underlying dictionaries. Replacement based on "Addressing the Rare Word Problem in Neural Machine Translation" :cite:`Luong2015b` Args: data (onmt.inputters.Dataset): Data. fields (List[Tuple[str, torchtext.data.Field]]): data fields n_best (int): number of translations produced replace_unk (bool): replace unknown words using attention has_tgt (bool): will the batch have gold targets """ def __init__(self, data, fields, n_best=1, replace_unk=False, has_tgt=False, phrase_table=""): self.data = data self.fields = fields self._has_text_src = isinstance( dict(self.fields)["src"], TextMultiField) self.n_best = n_best self.replace_unk = replace_unk self.phrase_table = phrase_table self.has_tgt = has_tgt def _build_target_tokens(self, src, src_vocab, src_raw, pred, attn): """Be carefull that this function has been hardcoded to translate RotoWire and therefore splits token at '_'. Remove Line49 to have a generic function""" tgt_field = dict(self.fields)["tgt"].base_field vocab = tgt_field.vocab tokens = [] for tok in pred: if tok < len(vocab): to_extend = vocab.itos[tok] else: to_extend = src_vocab.itos[tok - len(vocab)] to_extend = to_extend.split('_') #to_extend = [to_extend] tokens.extend(to_extend) if tokens[-1] == tgt_field.eos_token: tokens = tokens[:-1] break if self.replace_unk and attn is not None and src is not None: for i in range(len(tokens)): if tokens[i] == tgt_field.unk_token: _, max_index = attn[i][:len(src_raw)].max(0) tokens[i] = src_raw[max_index.item()] if self.phrase_table != "": with open(self.phrase_table, "r") as f: for line in f: if line.startswith(src_raw[max_index.item()]): tokens[i] = line.split('|||')[1].strip() return tokens def from_batch(self, translation_batch): batch = translation_batch["batch"] assert(len(translation_batch["gold_score"]) == len(translation_batch["predictions"])) batch_size = batch.batch_size preds, pred_score, attn, align, gold_score, indices = list(zip( *sorted(zip(translation_batch["predictions"], translation_batch["scores"], translation_batch["attention"], translation_batch["alignment"], translation_batch["gold_score"], batch.indices.data), key=lambda x: x[-1]))) if not any(align): # when align is a empty nested list align = [None] * batch_size # Sorting inds, perm = torch.sort(batch.indices) if self._has_text_src: src = batch.src[0][:, :, 0].index_select(1, perm) else: src = None tgt = batch.tgt[:, :, 0].index_select(1, perm) \ if self.has_tgt else None translations = [] for b in range(batch_size): if self._has_text_src: src_vocab = self.data.src_vocabs[inds[b]] \ if self.data.src_vocabs else None src_raw = self.data.examples[inds[b]].src[0] else: src_vocab = None src_raw = None pred_sents = [self._build_target_tokens( src[:, b] if src is not None else None, src_vocab, src_raw, preds[b][n], attn[b][n]) for n in range(self.n_best)] gold_sent = None if tgt is not None: gold_sent = self._build_target_tokens( src[:, b] if src is not None else None, src_vocab, src_raw, tgt[1:, b] if tgt is not None else None, None) translation = Translation( src[:, b] if src is not None else None, src_raw, pred_sents, attn[b], pred_score[b], gold_sent, gold_score[b], align[b] ) translations.append(translation) return translations class Translation(object): """Container for a translated sentence. Attributes: src (LongTensor): Source word IDs. src_raw (List[str]): Raw source words. pred_sents (List[List[str]]): Words from the n-best translations. pred_scores (List[List[float]]): Log-probs of n-best translations. attns (List[FloatTensor]) : Attention distribution for each translation. gold_sent (List[str]): Words from gold translation. gold_score (List[float]): Log-prob of gold translation. word_aligns (List[FloatTensor]): Words Alignment distribution for each translation. """ __slots__ = ["src", "src_raw", "pred_sents", "attns", "pred_scores", "gold_sent", "gold_score", "word_aligns"] def __init__(self, src, src_raw, pred_sents, attn, pred_scores, tgt_sent, gold_score, word_aligns): self.src = src self.src_raw = src_raw self.pred_sents = pred_sents self.attns = attn self.pred_scores = pred_scores self.gold_sent = tgt_sent self.gold_score = gold_score self.word_aligns = word_aligns def log(self, sent_number): """ Log translation. """ msg = ['\nSENT {}: {}\n'.format(sent_number, self.src_raw)] best_pred = self.pred_sents[0] best_score = self.pred_scores[0] pred_sent = ' '.join(best_pred) msg.append('PRED {}: {}\n'.format(sent_number, pred_sent)) msg.append("PRED SCORE: {:.4f}\n".format(best_score)) if self.word_aligns is not None: pred_align = self.word_aligns[0] pred_align_pharaoh = build_align_pharaoh(pred_align) pred_align_sent = ' '.join(pred_align_pharaoh) msg.append("ALIGN: {}\n".format(pred_align_sent)) if self.gold_sent is not None: tgt_sent = ' '.join(self.gold_sent) msg.append('GOLD {}: {}\n'.format(sent_number, tgt_sent)) msg.append(("GOLD SCORE: {:.4f}\n".format(self.gold_score))) if len(self.pred_sents) > 1: msg.append('\nBEST HYP:\n') for score, sent in zip(self.pred_scores, self.pred_sents): msg.append("[{:.4f}] {}\n".format(score, sent)) return "".join(msg)
7,226
37.854839
86
py
data-to-text-hierarchical
data-to-text-hierarchical-master/onmt/translate/greedy_search.py
import torch from onmt.translate.decode_strategy import DecodeStrategy def sample_with_temperature(logits, sampling_temp, keep_topk): """Select next tokens randomly from the top k possible next tokens. Samples from a categorical distribution over the ``keep_topk`` words using the category probabilities ``logits / sampling_temp``. Args: logits (FloatTensor): Shaped ``(batch_size, vocab_size)``. These can be logits (``(-inf, inf)``) or log-probs (``(-inf, 0]``). (The distribution actually uses the log-probabilities ``logits - logits.logsumexp(-1)``, which equals the logits if they are log-probabilities summing to 1.) sampling_temp (float): Used to scale down logits. The higher the value, the more likely it is that a non-max word will be sampled. keep_topk (int): This many words could potentially be chosen. The other logits are set to have probability 0. Returns: (LongTensor, FloatTensor): * topk_ids: Shaped ``(batch_size, 1)``. These are the sampled word indices in the output vocab. * topk_scores: Shaped ``(batch_size, 1)``. These are essentially ``(logits / sampling_temp)[topk_ids]``. """ if sampling_temp == 0.0 or keep_topk == 1: # For temp=0.0, take the argmax to avoid divide-by-zero errors. # keep_topk=1 is also equivalent to argmax. topk_scores, topk_ids = logits.topk(1, dim=-1) if sampling_temp > 0: topk_scores /= sampling_temp else: logits = torch.div(logits, sampling_temp) if keep_topk > 0: top_values, top_indices = torch.topk(logits, keep_topk, dim=1) kth_best = top_values[:, -1].view([-1, 1]) kth_best = kth_best.repeat([1, logits.shape[1]]).float() # Set all logits that are not in the top-k to -10000. # This puts the probabilities close to 0. ignore = torch.lt(logits, kth_best) logits = logits.masked_fill(ignore, -10000) dist = torch.distributions.Multinomial( logits=logits, total_count=1) topk_ids = torch.argmax(dist.sample(), dim=1, keepdim=True) topk_scores = logits.gather(dim=1, index=topk_ids) return topk_ids, topk_scores class GreedySearch(DecodeStrategy): """Select next tokens randomly from the top k possible next tokens. The ``scores`` attribute's lists are the score, after applying temperature, of the final prediction (either EOS or the final token in the event that ``max_length`` is reached) Args: pad (int): See base. bos (int): See base. eos (int): See base. batch_size (int): See base. min_length (int): See base. max_length (int): See base. block_ngram_repeat (int): See base. exclusion_tokens (set[int]): See base. return_attention (bool): See base. max_length (int): See base. sampling_temp (float): See :func:`~onmt.translate.greedy_search.sample_with_temperature()`. keep_topk (int): See :func:`~onmt.translate.greedy_search.sample_with_temperature()`. """ def __init__(self, pad, bos, eos, batch_size, min_length, block_ngram_repeat, exclusion_tokens, return_attention, max_length, sampling_temp, keep_topk): assert block_ngram_repeat == 0 super(GreedySearch, self).__init__( pad, bos, eos, batch_size, 1, min_length, block_ngram_repeat, exclusion_tokens, return_attention, max_length) self.sampling_temp = sampling_temp self.keep_topk = keep_topk self.topk_scores = None def initialize(self, memory_bank, src_lengths, src_map=None, device=None): """Initialize for decoding.""" fn_map_state = None if isinstance(memory_bank, tuple): mb_device = memory_bank[0].device else: mb_device = memory_bank.device if device is None: device = mb_device self.memory_lengths = src_lengths super(GreedySearch, self).initialize( memory_bank, src_lengths, src_map, device) self.select_indices = torch.arange( self.batch_size, dtype=torch.long, device=device) self.original_batch_idx = torch.arange( self.batch_size, dtype=torch.long, device=device) return fn_map_state, memory_bank, self.memory_lengths, src_map @property def current_predictions(self): return self.alive_seq[:, -1] @property def batch_offset(self): return self.select_indices def advance(self, log_probs, attn): """Select next tokens randomly from the top k possible next tokens. Args: log_probs (FloatTensor): Shaped ``(batch_size, vocab_size)``. These can be logits (``(-inf, inf)``) or log-probs (``(-inf, 0]``). (The distribution actually uses the log-probabilities ``logits - logits.logsumexp(-1)``, which equals the logits if they are log-probabilities summing to 1.) attn (FloatTensor): Shaped ``(1, B, inp_seq_len)``. """ self.ensure_min_length(log_probs) self.block_ngram_repeats(log_probs) topk_ids, self.topk_scores = sample_with_temperature( log_probs, self.sampling_temp, self.keep_topk) self.is_finished = topk_ids.eq(self.eos) self.alive_seq = torch.cat([self.alive_seq, topk_ids], -1) if self.return_attention: if self.alive_attn is None: self.alive_attn = attn else: self.alive_attn = torch.cat([self.alive_attn, attn], 0) self.ensure_max_length() def update_finished(self): """Finalize scores and predictions.""" # shape: (sum(~ self.is_finished), 1) finished_batches = self.is_finished.view(-1).nonzero() for b in finished_batches.view(-1): b_orig = self.original_batch_idx[b] self.scores[b_orig].append(self.topk_scores[b, 0]) self.predictions[b_orig].append(self.alive_seq[b, 1:]) self.attention[b_orig].append( self.alive_attn[:, b, :self.memory_lengths[b]] if self.alive_attn is not None else []) self.done = self.is_finished.all() if self.done: return is_alive = ~self.is_finished.view(-1) self.alive_seq = self.alive_seq[is_alive] if self.alive_attn is not None: self.alive_attn = self.alive_attn[:, is_alive] self.select_indices = is_alive.nonzero().view(-1) self.original_batch_idx = self.original_batch_idx[is_alive]
6,843
39.258824
79
py
data-to-text-hierarchical
data-to-text-hierarchical-master/onmt/translate/beam_search.py
import torch from onmt.translate import penalties from onmt.translate.decode_strategy import DecodeStrategy from onmt.utils.misc import tile import warnings class BeamSearch(DecodeStrategy): """Generation beam search. Note that the attributes list is not exhaustive. Rather, it highlights tensors to document their shape. (Since the state variables' "batch" size decreases as beams finish, we denote this axis with a B rather than ``batch_size``). Args: beam_size (int): Number of beams to use (see base ``parallel_paths``). batch_size (int): See base. pad (int): See base. bos (int): See base. eos (int): See base. n_best (int): Don't stop until at least this many beams have reached EOS. global_scorer (onmt.translate.GNMTGlobalScorer): Scorer instance. min_length (int): See base. max_length (int): See base. return_attention (bool): See base. block_ngram_repeat (int): See base. exclusion_tokens (set[int]): See base. Attributes: top_beam_finished (ByteTensor): Shape ``(B,)``. _batch_offset (LongTensor): Shape ``(B,)``. _beam_offset (LongTensor): Shape ``(batch_size x beam_size,)``. alive_seq (LongTensor): See base. topk_log_probs (FloatTensor): Shape ``(B x beam_size,)``. These are the scores used for the topk operation. memory_lengths (LongTensor): Lengths of encodings. Used for masking attentions. select_indices (LongTensor or NoneType): Shape ``(B x beam_size,)``. This is just a flat view of the ``_batch_index``. topk_scores (FloatTensor): Shape ``(B, beam_size)``. These are the scores a sequence will receive if it finishes. topk_ids (LongTensor): Shape ``(B, beam_size)``. These are the word indices of the topk predictions. _batch_index (LongTensor): Shape ``(B, beam_size)``. _prev_penalty (FloatTensor or NoneType): Shape ``(B, beam_size)``. Initialized to ``None``. _coverage (FloatTensor or NoneType): Shape ``(1, B x beam_size, inp_seq_len)``. hypotheses (list[list[Tuple[Tensor]]]): Contains a tuple of score (float), sequence (long), and attention (float or None). """ def __init__(self, beam_size, batch_size, pad, bos, eos, dot, n_best, global_scorer, min_length, max_length, return_attention, block_ngram_repeat, exclusion_tokens, stepwise_penalty, ratio): super(BeamSearch, self).__init__( pad, bos, eos, dot, batch_size, beam_size, min_length, block_ngram_repeat, exclusion_tokens, return_attention, max_length) # beam parameters self.global_scorer = global_scorer self.beam_size = beam_size self.n_best = n_best self.ratio = ratio # result caching self.hypotheses = [[] for _ in range(batch_size)] # beam state self.top_beam_finished = torch.zeros([batch_size], dtype=torch.uint8) # BoolTensor was introduced in pytorch 1.2 try: self.top_beam_finished = self.top_beam_finished.bool() except AttributeError: pass self._batch_offset = torch.arange(batch_size, dtype=torch.long) self.select_indices = None self.done = False # "global state" of the old beam self._prev_penalty = None self._coverage = None self._stepwise_cov_pen = ( stepwise_penalty and self.global_scorer.has_cov_pen) self._vanilla_cov_pen = ( not stepwise_penalty and self.global_scorer.has_cov_pen) self._cov_pen = self.global_scorer.has_cov_pen def initialize(self, memory_bank, src_lengths, src_map=None, device=None): """Initialize for decoding. Repeat src objects `beam_size` times. """ def fn_map_state(state, dim): return tile(state, self.beam_size, dim=dim) if isinstance(memory_bank, tuple): memory_bank = tuple(tile(x, self.beam_size, dim=1) for x in memory_bank) mb_device = memory_bank[0].device else: memory_bank = tile(memory_bank, self.beam_size, dim=1) mb_device = memory_bank.device if src_map is not None: src_map = tile(src_map, self.beam_size, dim=1) if device is None: device = mb_device self.memory_lengths = tile(src_lengths, self.beam_size) super(BeamSearch, self).initialize( memory_bank, self.memory_lengths, src_map, device) self.best_scores = torch.full( [self.batch_size], -1e10, dtype=torch.float, device=device) self._beam_offset = torch.arange( 0, self.batch_size * self.beam_size, step=self.beam_size, dtype=torch.long, device=device) self.topk_log_probs = torch.tensor( [0.0] + [float("-inf")] * (self.beam_size - 1), device=device ).repeat(self.batch_size) # buffers for the topk scores and 'backpointer' self.topk_scores = torch.empty((self.batch_size, self.beam_size), dtype=torch.float, device=device) self.topk_ids = torch.empty((self.batch_size, self.beam_size), dtype=torch.long, device=device) self._batch_index = torch.empty([self.batch_size, self.beam_size], dtype=torch.long, device=device) return fn_map_state, memory_bank, self.memory_lengths, src_map @property def current_predictions(self): return self.alive_seq[:, -1] @property def current_backptr(self): # for testing return self.select_indices.view(self.batch_size, self.beam_size)\ .fmod(self.beam_size) @property def batch_offset(self): return self._batch_offset def advance(self, log_probs, attn, attn_key): vocab_size = log_probs.size(-1) # using integer division to get an integer _B without casting _B = log_probs.shape[0] // self.beam_size if self._stepwise_cov_pen and self._prev_penalty is not None: self.topk_log_probs += self._prev_penalty self.topk_log_probs -= self.global_scorer.cov_penalty( self._coverage + attn, self.global_scorer.beta).view( _B, self.beam_size) # force the output to be longer than self.min_length step = len(self) self.ensure_min_length(log_probs) # Multiply probs by the beam probability. log_probs += self.topk_log_probs.view(_B * self.beam_size, 1) # if the sequence ends now, then the penalty is the current # length + 1, to include the EOS token length_penalty = self.global_scorer.length_penalty( step + 1, alpha=self.global_scorer.alpha) curr_scores = log_probs / length_penalty # Avoid any direction that would repeat unwanted ngrams self.block_ngram_repeats(curr_scores) # Flatten probs into a list of possibilities. curr_scores = curr_scores.reshape(_B, self.beam_size * vocab_size) torch.topk(curr_scores, self.beam_size, dim=-1, out=(self.topk_scores, self.topk_ids)) # Recover log probs. # Length penalty is just a scalar. It doesn't matter if it's applied # before or after the topk. torch.mul(self.topk_scores, length_penalty, out=self.topk_log_probs) # Resolve beam origin and map to batch index flat representation. torch.div(self.topk_ids, vocab_size, out=self._batch_index) self._batch_index += self._beam_offset[:_B].unsqueeze(1) self.select_indices = self._batch_index.view(_B * self.beam_size) self.topk_ids.fmod_(vocab_size) # resolve true word ids # Append last prediction. self.alive_seq = torch.cat( [self.alive_seq.index_select(0, self.select_indices), self.topk_ids.view(_B * self.beam_size, 1)], -1) self.maybe_update_forbidden_tokens() if self.return_attention or self._cov_pen: assert attn_key is not None # we reorder the current attention dict current_attn = {key: value.index_select(1, self.select_indices) for key, value in attn.items()} if step == 1: self.alive_attn = current_attn # update global state (step == 1) if self._cov_pen: # coverage penalty assert attn_key is not None self._prev_penalty = torch.zeros_like(self.topk_log_probs) self._coverage = current_attn[attn_key] else: # we reorder all attentions from the start self.alive_attn = {key: value.index_select(1, self.select_indices) for key, value in self.alive_attn.items()} # We cat previous attentions with current self.alive_attn = { key: torch.cat([self.alive_attn[key], current_attn[key]], 0) for key in list(current_attn) } # update global state (step > 1) if self._cov_pen: self._coverage = self._coverage.index_select( 1, self.select_indices) self._coverage += current_attn[key_attn] self._prev_penalty = self.global_scorer.cov_penalty( self._coverage, beta=self.global_scorer.beta).view( _B, self.beam_size) if self._vanilla_cov_pen: # shape: (batch_size x beam_size, 1) cov_penalty = self.global_scorer.cov_penalty( self._coverage, beta=self.global_scorer.beta) self.topk_scores -= cov_penalty.view(_B, self.beam_size).float() self.is_finished = self.topk_ids.eq(self.eos) self.ensure_max_length() def update_finished(self): # Penalize beams that finished. _B_old = self.topk_log_probs.shape[0] step = self.alive_seq.shape[-1] # 1 greater than the step in advance self.topk_log_probs.masked_fill_(self.is_finished, -1e10) # on real data (newstest2017) with the pretrained transformer, # it's faster to not move this back to the original device self.is_finished = self.is_finished.to('cpu') self.top_beam_finished |= self.is_finished[:, 0].eq(1) predictions = self.alive_seq.view(_B_old, self.beam_size, step) if self.alive_attn is None: attention = None else: attention = { key: tensor.view(step - 1, _B_old, self.beam_size, tensor.size(-1)) for key, tensor in self.alive_attn.items() } non_finished_batch = [] for i in range(self.is_finished.size(0)): # Batch level b = self._batch_offset[i] finished_hyp = self.is_finished[i].nonzero().view(-1) # Store finished hypotheses for this batch. for j in finished_hyp: # Beam level: finished beam j in batch i if self.ratio > 0: s = self.topk_scores[i, j] / (step + 1) if self.best_scores[b] < s: self.best_scores[b] = s self.hypotheses[b].append(( self.topk_scores[i, j], predictions[i, j, 1:], # Ignore start_token. { key: tensor[:, i, j, :self._memory_lengths[i]] for key, tensor in attention.items() } if attention is not None else None)) # End condition is the top beam finished and we can return # n_best hypotheses. if self.ratio > 0: pred_len = self.memory_lengths[i] * self.ratio finish_flag = ((self.topk_scores[i, 0] / pred_len) <= self.best_scores[b]) or \ self.is_finished[i].all() else: finish_flag = self.top_beam_finished[i] != 0 if finish_flag and len(self.hypotheses[b]) >= self.n_best: best_hyp = sorted( self.hypotheses[b], key=lambda x: x[0], reverse=True) for n, (score, pred, attn) in enumerate(best_hyp): if n >= self.n_best: break self.scores[b].append(score) self.predictions[b].append(pred) # ``(batch, n_best,)`` self.attention[b].append( attn if attn is not None else []) else: non_finished_batch.append(i) non_finished = torch.tensor(non_finished_batch) # If all sentences are translated, no need to go further. if len(non_finished) == 0: self.done = True return _B_new = non_finished.shape[0] # Remove finished batches for the next step. self.top_beam_finished = self.top_beam_finished.index_select( 0, non_finished) self._batch_offset = self._batch_offset.index_select(0, non_finished) non_finished = non_finished.to(self.topk_ids.device) self.topk_log_probs = self.topk_log_probs.index_select(0, non_finished) self._batch_index = self._batch_index.index_select(0, non_finished) self.select_indices = self._batch_index.view(_B_new * self.beam_size) self.alive_seq = predictions.index_select(0, non_finished) \ .view(-1, self.alive_seq.size(-1)) self.topk_scores = self.topk_scores.index_select(0, non_finished) self.topk_ids = self.topk_ids.index_select(0, non_finished) if self.alive_attn is not None: self.alive_attn = { key: tensor.index_select(1, non_finished) \ .view(step - 1, _B_new * self.beam_size, tensor.size(-1)) for key, tensor in attention.items()} if self._cov_pen: self._coverage = self._coverage \ .view(1, _B_old, self.beam_size, inp_seq_len) \ .index_select(1, non_finished) \ .view(1, _B_new * self.beam_size, inp_seq_len) if self._stepwise_cov_pen: self._prev_penalty = self._prev_penalty.index_select( 0, non_finished) class GNMTGlobalScorer(object): """NMT re-ranking. Args: alpha (float): Length parameter. beta (float): Coverage parameter. length_penalty (str): Length penalty strategy. coverage_penalty (str): Coverage penalty strategy. Attributes: alpha (float): See above. beta (float): See above. length_penalty (callable): See :class:`penalties.PenaltyBuilder`. coverage_penalty (callable): See :class:`penalties.PenaltyBuilder`. has_cov_pen (bool): See :class:`penalties.PenaltyBuilder`. has_len_pen (bool): See :class:`penalties.PenaltyBuilder`. """ @classmethod def from_opt(cls, opt): return cls( opt.alpha, opt.beta, opt.length_penalty, opt.coverage_penalty) def __init__(self, alpha, beta, length_penalty, coverage_penalty): self._validate(alpha, beta, length_penalty, coverage_penalty) self.alpha = alpha self.beta = beta penalty_builder = penalties.PenaltyBuilder(coverage_penalty, length_penalty) self.has_cov_pen = penalty_builder.has_cov_pen # Term will be subtracted from probability self.cov_penalty = penalty_builder.coverage_penalty self.has_len_pen = penalty_builder.has_len_pen # Probability will be divided by this self.length_penalty = penalty_builder.length_penalty @classmethod def _validate(cls, alpha, beta, length_penalty, coverage_penalty): # these warnings indicate that either the alpha/beta # forces a penalty to be a no-op, or a penalty is a no-op but # the alpha/beta would suggest otherwise. if length_penalty is None or length_penalty == "none": if alpha != 0: warnings.warn("Non-default `alpha` with no length penalty. " "`alpha` has no effect.") else: # using some length penalty if length_penalty == "wu" and alpha == 0.: warnings.warn("Using length penalty Wu with alpha==0 " "is equivalent to using length penalty none.") if coverage_penalty is None or coverage_penalty == "none": if beta != 0: warnings.warn("Non-default `beta` with no coverage penalty. " "`beta` has no effect.") else: # using some coverage penalty if beta == 0.: warnings.warn("Non-default coverage penalty with beta==0 " "is equivalent to using coverage penalty none.")
17,605
43.015
83
py
data-to-text-hierarchical
data-to-text-hierarchical-master/onmt/translate/penalties.py
from __future__ import division import torch class PenaltyBuilder(object): """Returns the Length and Coverage Penalty function for Beam Search. Args: length_pen (str): option name of length pen cov_pen (str): option name of cov pen Attributes: has_cov_pen (bool): Whether coverage penalty is None (applying it is a no-op). Note that the converse isn't true. Setting beta to 0 should force coverage length to be a no-op. has_len_pen (bool): Whether length penalty is None (applying it is a no-op). Note that the converse isn't true. Setting alpha to 1 should force length penalty to be a no-op. coverage_penalty (callable[[FloatTensor, float], FloatTensor]): Calculates the coverage penalty. length_penalty (callable[[int, float], float]): Calculates the length penalty. """ def __init__(self, cov_pen, length_pen): self.has_cov_pen = not self._pen_is_none(cov_pen) self.coverage_penalty = self._coverage_penalty(cov_pen) self.has_len_pen = not self._pen_is_none(length_pen) self.length_penalty = self._length_penalty(length_pen) @staticmethod def _pen_is_none(pen): return pen == "none" or pen is None def _coverage_penalty(self, cov_pen): if cov_pen == "wu": return self.coverage_wu elif cov_pen == "summary": return self.coverage_summary elif self._pen_is_none(cov_pen): return self.coverage_none else: raise NotImplementedError("No '{:s}' coverage penalty.".format( cov_pen)) def _length_penalty(self, length_pen): if length_pen == "wu": return self.length_wu elif length_pen == "avg": return self.length_average elif self._pen_is_none(length_pen): return self.length_none else: raise NotImplementedError("No '{:s}' length penalty.".format( length_pen)) # Below are all the different penalty terms implemented so far. # Subtract coverage penalty from topk log probs. # Divide topk log probs by length penalty. def coverage_wu(self, cov, beta=0.): """GNMT coverage re-ranking score. See "Google's Neural Machine Translation System" :cite:`wu2016google`. ``cov`` is expected to be sized ``(*, seq_len)``, where ``*`` is probably ``batch_size x beam_size`` but could be several dimensions like ``(batch_size, beam_size)``. If ``cov`` is attention, then the ``seq_len`` axis probably sums to (almost) 1. """ penalty = -torch.min(cov, cov.clone().fill_(1.0)).log().sum(-1) return beta * penalty def coverage_summary(self, cov, beta=0.): """Our summary penalty.""" penalty = torch.max(cov, cov.clone().fill_(1.0)).sum(-1) penalty -= cov.size(-1) return beta * penalty def coverage_none(self, cov, beta=0.): """Returns zero as penalty""" none = torch.zeros((1,), device=cov.device, dtype=torch.float) if cov.dim() == 3: none = none.unsqueeze(0) return none def length_wu(self, cur_len, alpha=0.): """GNMT length re-ranking score. See "Google's Neural Machine Translation System" :cite:`wu2016google`. """ return ((5 + cur_len) / 6.0) ** alpha def length_average(self, cur_len, alpha=0.): """Returns the current sequence length.""" return cur_len def length_none(self, cur_len, alpha=0.): """Returns unmodified scores.""" return 1.0
3,710
35.029126
78
py
data-to-text-hierarchical
data-to-text-hierarchical-master/onmt/translate/translator.py
#!/usr/bin/env python """ Translator Class and builder """ from __future__ import print_function import codecs import os import time import numpy as np from itertools import count, zip_longest import torch import onmt.model_builder import onmt.inputters as inputters import onmt.decoders.ensemble from onmt.translate.beam_search import BeamSearch from onmt.translate.greedy_search import GreedySearch from onmt.utils.misc import tile, set_random_seed, report_matrix from onmt.utils.alignment import extract_alignment, build_align_pharaoh from onmt.modules.copy_generator import collapse_copy_scores def build_translator(opt, report_score=True, logger=None, out_file=None): if out_file is None: out_file = codecs.open(opt.output, 'w+', 'utf-8') load_test_model = onmt.decoders.ensemble.load_test_model \ if len(opt.models) > 1 else onmt.model_builder.load_test_model fields, model, model_opt = load_test_model(opt) scorer = onmt.translate.GNMTGlobalScorer.from_opt(opt) translator = Translator.from_opt( model, fields, opt, model_opt, global_scorer=scorer, out_file=out_file, report_align=opt.report_align, report_score=report_score, logger=logger ) return translator def _tally_parameters(model): enc = 0 dec = 0 for name, param in model.named_parameters(): if 'encoder' in name: enc += param.nelement() else: dec += param.nelement() return enc + dec, enc, dec def max_tok_len(new, count, sofar): """ In token batching scheme, the number of sequences is limited such that the total number of src/tgt tokens (including padding) in a batch <= batch_size """ # Maintains the longest src and tgt length in the current batch global max_src_in_batch # this is a hack # Reset current longest length at a new batch (count=1) if count == 1: max_src_in_batch = 0 # max_tgt_in_batch = 0 # Src: [<bos> w1 ... wN <eos>] max_src_in_batch = max(max_src_in_batch, len(new.src[0]) + 2) # Tgt: [w1 ... wM <eos>] src_elements = count * max_src_in_batch return src_elements class Translator(object): """Translate a batch of sentences with a saved model. Args: model (onmt.modules.NMTModel): NMT model to use for translation fields (dict[str, torchtext.data.Field]): A dict mapping each side to its list of name-Field pairs. src_reader (onmt.inputters.DataReaderBase): Source reader. tgt_reader (onmt.inputters.TextDataReader): Target reader. gpu (int): GPU device. Set to negative for no GPU. n_best (int): How many beams to wait for. min_length (int): See :class:`onmt.translate.decode_strategy.DecodeStrategy`. max_length (int): See :class:`onmt.translate.decode_strategy.DecodeStrategy`. beam_size (int): Number of beams. random_sampling_topk (int): See :class:`onmt.translate.greedy_search.GreedySearch`. random_sampling_temp (int): See :class:`onmt.translate.greedy_search.GreedySearch`. stepwise_penalty (bool): Whether coverage penalty is applied every step or not. dump_beam (bool): Debugging option. block_ngram_repeat (int): See :class:`onmt.translate.decode_strategy.DecodeStrategy`. ignore_when_blocking (set or frozenset): See :class:`onmt.translate.decode_strategy.DecodeStrategy`. replace_unk (bool): Replace unknown token. data_type (str): Source data type. verbose (bool): Print/log every translation. report_time (bool): Print/log total time/frequency. copy_attn (bool): Use copy attention. global_scorer (onmt.translate.GNMTGlobalScorer): Translation scoring/reranking object. out_file (TextIO or codecs.StreamReaderWriter): Output file. report_score (bool) : Whether to report scores logger (logging.Logger or NoneType): Logger. """ def __init__( self, model, fields, src_reader, tgt_reader, gpu=-1, n_best=1, min_length=0, max_length=100, ratio=0., beam_size=30, random_sampling_topk=1, random_sampling_temp=1, stepwise_penalty=None, dump_beam=False, block_ngram_repeat=0, ignore_when_blocking=frozenset(), replace_unk=False, phrase_table="", data_type="text", verbose=False, report_time=False, copy_attn=False, global_scorer=None, out_file=None, report_align=False, report_score=True, dump_attn=False, logger=None, seed=-1): self.model = model self.fields = fields tgt_field = dict(self.fields)["tgt"].base_field self._tgt_vocab = tgt_field.vocab self._tgt_eos_idx = self._tgt_vocab.stoi[tgt_field.eos_token] self._tgt_pad_idx = self._tgt_vocab.stoi[tgt_field.pad_token] self._tgt_bos_idx = self._tgt_vocab.stoi[tgt_field.init_token] self._tgt_unk_idx = self._tgt_vocab.stoi[tgt_field.unk_token] self._tgt_dot_idx = self._tgt_vocab.stoi['.'] # custom rule self._tgt_vocab_len = len(self._tgt_vocab) self._gpu = gpu self._use_cuda = gpu > -1 self._dev = torch.device("cuda", self._gpu) \ if self._use_cuda else torch.device("cpu") self.n_best = n_best self.max_length = max_length self.beam_size = beam_size self.random_sampling_temp = random_sampling_temp self.sample_from_topk = random_sampling_topk self.min_length = min_length self.ratio = ratio self.stepwise_penalty = stepwise_penalty self.dump_beam = dump_beam self.dump_attn = dump_attn self.block_ngram_repeat = block_ngram_repeat self.ignore_when_blocking = ignore_when_blocking self._exclusion_idxs = { self._tgt_vocab.stoi[t] for t in self.ignore_when_blocking} self.src_reader = src_reader self.tgt_reader = tgt_reader self.replace_unk = replace_unk if self.replace_unk and not self.model.decoder.attentional: raise ValueError( "replace_unk requires an attentional decoder.") self.phrase_table = phrase_table self.data_type = data_type self.verbose = verbose self.report_time = report_time self.copy_attn = copy_attn self.global_scorer = global_scorer if self.global_scorer.has_cov_pen and \ not self.model.decoder.attentional: raise ValueError( "Coverage penalty requires an attentional decoder.") self.out_file = out_file self.report_align = report_align self.report_score = report_score self.logger = logger self.use_filter_pred = False self._filter_pred = None # for debugging self.beam_trace = self.dump_beam != "" self.beam_accum = None if self.beam_trace: self.beam_accum = { "predicted_ids": [], "beam_parent_ids": [], "scores": [], "log_probs": []} set_random_seed(seed, self._use_cuda) # Logging construction of translator n_params, enc, dec = _tally_parameters(model) self._log(f'Built a translator with {n_params} parameters') self._log(f'Important params are:') self._log(f' -beam size:{self.beam_size}') self._log(f' -blk:{self.block_ngram_repeat}') self._log(f' -dumping attention at: {self.dump_attn}') @classmethod def from_opt( cls, model, fields, opt, model_opt, global_scorer=None, out_file=None, report_align=False, report_score=True, logger=None): """Alternate constructor. Args: model (onmt.modules.NMTModel): See :func:`__init__()`. fields (dict[str, torchtext.data.Field]): See :func:`__init__()`. opt (argparse.Namespace): Command line options model_opt (argparse.Namespace): Command line options saved with the model checkpoint. global_scorer (onmt.translate.GNMTGlobalScorer): See :func:`__init__()`.. out_file (TextIO or codecs.StreamReaderWriter): See :func:`__init__()`. report_align (bool) : See :func:`__init__()`. report_score (bool) : See :func:`__init__()`. logger (logging.Logger or NoneType): See :func:`__init__()`. """ src_reader = inputters.str2reader[opt.data_type].from_opt(opt) tgt_reader = inputters.str2reader["text"].from_opt(opt) return cls( model, fields, src_reader, tgt_reader, gpu=opt.gpu, n_best=opt.n_best, min_length=opt.min_length, max_length=opt.max_length, ratio=opt.ratio, beam_size=opt.beam_size, random_sampling_topk=opt.random_sampling_topk, random_sampling_temp=opt.random_sampling_temp, stepwise_penalty=opt.stepwise_penalty, dump_beam=opt.dump_beam, block_ngram_repeat=opt.block_ngram_repeat, ignore_when_blocking=set(opt.ignore_when_blocking), replace_unk=opt.replace_unk, phrase_table=opt.phrase_table, data_type=opt.data_type, verbose=opt.verbose, report_time=opt.report_time, copy_attn=model_opt.copy_attn, global_scorer=global_scorer, out_file=out_file, report_align=report_align, report_score=report_score, dump_attn=opt.dump_attn, logger=logger, seed=opt.seed) def _log(self, msg): if self.logger: self.logger.info(msg) else: print(msg) def _gold_score(self, batch, memory_bank, src_lengths, src_vocabs, use_src_map, enc_states, batch_size, src): if "tgt" in batch.__dict__: gs = self._score_target( batch, memory_bank, src_lengths, src_vocabs, batch.src_map if use_src_map else None) self.model.decoder.init_state(src, memory_bank, enc_states) else: gs = [0] * batch_size return gs def translate( self, src, tgt=None, src_dir=None, batch_size=None, batch_type="sents", attn_debug=False, align_debug=False, phrase_table=""): """Translate content of ``src`` and get gold scores from ``tgt``. Args: src: See :func:`self.src_reader.read()`. tgt: See :func:`self.tgt_reader.read()`. src_dir: See :func:`self.src_reader.read()` (only relevant for certain types of data). batch_size (int): size of examples per mini-batch attn_debug (bool): enables the attention logging align_debug (bool): enables the word alignment logging Returns: (`list`, `list`) * all_scores is a list of `batch_size` lists of `n_best` scores * all_predictions is a list of `batch_size` lists of `n_best` predictions """ if batch_size is None: raise ValueError("batch_size must be set") src_data = {"reader": self.src_reader, "data": src, "dir": src_dir} tgt_data = {"reader": self.tgt_reader, "data": tgt, "dir": None} _readers, _data, _dir = inputters.Dataset.config( [('src', src_data), ('tgt', tgt_data)]) data = inputters.Dataset( self.fields, readers=_readers, data=_data, dirs=_dir, sort_key=inputters.str2sortkey[self.data_type], filter_pred=self._filter_pred ) data_iter = inputters.OrderedIterator( dataset=data, device=self._dev, batch_size=batch_size, batch_size_fn=max_tok_len if batch_type == "tokens" else None, train=False, sort=False, sort_within_batch=True, shuffle=False ) xlation_builder = onmt.translate.TranslationBuilder( data, self.fields, self.n_best, self.replace_unk, tgt, self.phrase_table ) # Statistics counter = count(1) pred_score_total, pred_words_total = 0, 0 gold_score_total, gold_words_total = 0, 0 all_scores = [] all_predictions = [] start_time = time.time() for _iter, batch in enumerate(data_iter, 1): if _iter == 1: n_params, enc, dec = _tally_parameters(self.model) self._log(f'Built a translator with {n_params} parameters') self._log(f'Important params are:') self._log(f' -beam size:{self.beam_size}') self._log(f' -blk:{self.block_ngram_repeat}') self._log(f' -dumping attention at: {self.dump_attn}') self._log(f'Translating batch {_iter} (batch_size is {batch_size})') return_attention = True if attn_debug or self.dump_attn else False batch_data = self.translate_batch( batch, data.src_vocabs, return_attention ) translations = xlation_builder.from_batch(batch_data) if self.dump_attn: # save the attention for this batch in this folder torch.save(batch_data["attention"], os.path.join(self.dump_attn, f'batch_{_iter}')) for trans in translations: all_scores += [trans.pred_scores[:self.n_best]] pred_score_total += trans.pred_scores[0] pred_words_total += len(trans.pred_sents[0]) if tgt is not None: gold_score_total += trans.gold_score gold_words_total += len(trans.gold_sent) + 1 n_best_preds = [" ".join(pred) for pred in trans.pred_sents[:self.n_best]] if self.report_align: align_pharaohs = [build_align_pharaoh(align) for align in trans.word_aligns[:self.n_best]] n_best_preds_align = [" ".join(align) for align in align_pharaohs] n_best_preds = [pred + " ||| " + align for pred, align in zip( n_best_preds, n_best_preds_align)] all_predictions += [n_best_preds] self.out_file.write('\n'.join(n_best_preds) + '\n') self.out_file.flush() if self.verbose: sent_number = next(counter) output = trans.log(sent_number) if self.logger: self.logger.info(output) else: os.write(1, output.encode('utf-8')) if attn_debug: preds = trans.pred_sents[0] preds.append('</s>') attns = trans.attns[0].tolist() if self.data_type == 'text': srcs = trans.src_raw else: srcs = [str(item) for item in range(len(attns[0]))] output = report_matrix(srcs, preds, attns) if self.logger: self.logger.info(output) else: os.write(1, output.encode('utf-8')) if align_debug: if trans.gold_sent is not None: tgts = trans.gold_sent else: tgts = trans.pred_sents[0] align = trans.word_aligns[0].tolist() if self.data_type == 'text': srcs = trans.src_raw else: srcs = [str(item) for item in range(len(align[0]))] output = report_matrix(srcs, tgts, align) if self.logger: self.logger.info(output) else: os.write(1, output.encode('utf-8')) end_time = time.time() if self.report_score: msg = self._report_score('PRED', pred_score_total, pred_words_total) self._log(msg) if tgt is not None: msg = self._report_score('GOLD', gold_score_total, gold_words_total) self._log(msg) if self.report_time: total_time = end_time - start_time self._log("Total translation time (s): %f" % total_time) self._log("Average translation time (s): %f" % ( total_time / len(all_predictions))) self._log("Tokens per second: %f" % ( pred_words_total / total_time)) if self.dump_beam: import json json.dump(self.translator.beam_accum, codecs.open(self.dump_beam, 'w', 'utf-8')) return all_scores, all_predictions def _align_pad_prediction(self, predictions, bos, pad): """ Padding predictions in batch and add BOS. Args: predictions (List[List[Tensor]]): `(batch, n_best,)`, for each src sequence contain n_best tgt predictions all of which ended with eos id. bos (int): bos index to be used. pad (int): pad index to be used. Return: batched_nbest_predict (torch.LongTensor): `(batch, n_best, tgt_l)` """ dtype, device = predictions[0][0].dtype, predictions[0][0].device flatten_tgt = [best.tolist() for bests in predictions for best in bests] paded_tgt = torch.tensor( list(zip_longest(*flatten_tgt, fillvalue=pad)), dtype=dtype, device=device).T bos_tensor = torch.full([paded_tgt.size(0), 1], bos, dtype=dtype, device=device) full_tgt = torch.cat((bos_tensor, paded_tgt), dim=-1) batched_nbest_predict = full_tgt.view( len(predictions), -1, full_tgt.size(-1)) # (batch, n_best, tgt_l) return batched_nbest_predict def _align_forward(self, batch, predictions): """ For a batch of input and its prediction, return a list of batch predict alignment src indice Tensor in size ``(batch, n_best,)``. """ # (0) add BOS and padding to tgt prediction if hasattr(batch, 'tgt'): batch_tgt_idxs = batch.tgt.transpose(1, 2).transpose(0, 2) else: batch_tgt_idxs = self._align_pad_prediction( predictions, bos=self._tgt_bos_idx, pad=self._tgt_pad_idx) tgt_mask = (batch_tgt_idxs.eq(self._tgt_pad_idx) | batch_tgt_idxs.eq(self._tgt_eos_idx) | batch_tgt_idxs.eq(self._tgt_bos_idx)) n_best = batch_tgt_idxs.size(1) # (1) Encoder forward. src, enc_states, memory_bank, src_lengths = self._run_encoder(batch) # (2) Repeat src objects `n_best` times. # We use batch_size x n_best, get ``(src_len, batch * n_best, nfeat)`` src = tile(src, n_best, dim=1) enc_states = tile(enc_states, n_best, dim=1) if isinstance(memory_bank, tuple): memory_bank = tuple(tile(x, n_best, dim=1) for x in memory_bank) else: memory_bank = tile(memory_bank, n_best, dim=1) src_lengths = tile(src_lengths, n_best) # ``(batch * n_best,)`` # (3) Init decoder with n_best src, self.model.decoder.init_state(src, memory_bank, enc_states) # reshape tgt to ``(len, batch * n_best, nfeat)`` tgt = batch_tgt_idxs.view(-1, batch_tgt_idxs.size(-1)).T.unsqueeze(-1) dec_in = tgt[:-1] # exclude last target from inputs _, attns = self.model.decoder( dec_in, memory_bank, memory_lengths=src_lengths, with_align=True) alignment_attn = attns["align"] # ``(B, tgt_len-1, src_len)`` # masked_select align_tgt_mask = tgt_mask.view(-1, tgt_mask.size(-1)) prediction_mask = align_tgt_mask[:, 1:] # exclude bos to match pred # get aligned src id for each prediction's valid tgt tokens alignement = extract_alignment( alignment_attn, prediction_mask, src_lengths, n_best) return alignement def translate_batch(self, batch, src_vocabs, attn_debug): """Translate a batch of sentences.""" with torch.no_grad(): if self.beam_size == 1: decode_strategy = GreedySearch( pad=self._tgt_pad_idx, bos=self._tgt_bos_idx, eos=self._tgt_eos_idx, batch_size=batch.batch_size, min_length=self.min_length, max_length=self.max_length, block_ngram_repeat=self.block_ngram_repeat, exclusion_tokens=self._exclusion_idxs, return_attention=attn_debug or self.replace_unk, sampling_temp=self.random_sampling_temp, keep_topk=self.sample_from_topk) else: # TODO: support these blacklisted features assert not self.dump_beam decode_strategy = BeamSearch( self.beam_size, batch_size=batch.batch_size, pad=self._tgt_pad_idx, bos=self._tgt_bos_idx, eos=self._tgt_eos_idx, dot=self._tgt_dot_idx, n_best=self.n_best, global_scorer=self.global_scorer, min_length=self.min_length, max_length=self.max_length, return_attention=attn_debug or self.replace_unk, block_ngram_repeat=self.block_ngram_repeat, exclusion_tokens=self._exclusion_idxs, stepwise_penalty=self.stepwise_penalty, ratio=self.ratio) return self._translate_batch_with_strategy(batch, src_vocabs, decode_strategy) def _run_encoder(self, batch): src, src_lengths = batch.src if isinstance(batch.src, tuple) \ else (batch.src, None) enc_states, memory_bank, src_lengths = self.model.encoder( src, src_lengths) if src_lengths is None: assert not isinstance(memory_bank, tuple), \ 'Ensemble decoding only supported for text data' src_lengths = torch.Tensor(batch.batch_size) \ .type_as(memory_bank) \ .long() \ .fill_(memory_bank.size(0)) return src, enc_states, memory_bank, src_lengths def _decode_and_generate( self, decoder_in, memory_bank, batch, src_vocabs, memory_lengths, src_map=None, step=None, batch_offset=None): if self.copy_attn: # Turn any copied words into UNKs. decoder_in = decoder_in.masked_fill( decoder_in.gt(self._tgt_vocab_len - 1), self._tgt_unk_idx ) # Decoder forward, takes [tgt_len, batch, nfeats] as input # and [src_len, batch, hidden] as memory_bank # in case of inference tgt_len = 1, batch = beam times batch_size # in case of Gold Scoring tgt_len = actual length, batch = 1 batch dec_out, dec_attn = self.model.decoder( decoder_in, memory_bank, memory_lengths=memory_lengths, step=step ) # Generator forward. if not self.copy_attn: if "std" in dec_attn: attn = dec_attn["std"] attn_key = 'std' else: attn = None attn_key = None log_probs = self.model.generator(dec_out.squeeze(0)) # returns [(batch_size x beam_size) , vocab ] when 1 step # or [ tgt_len, batch_size, vocab ] when full sentence else: attn = dec_attn["copy"] attn_key = 'copy' scores = self.model.generator(dec_out.view(-1, dec_out.size(2)), attn.view(-1, attn.size(2)), src_map) # here we have scores [tgt_lenxbatch, vocab] or [beamxbatch, vocab] if batch_offset is None: scores = scores.view(-1, batch.batch_size, scores.size(-1)) scores = scores.transpose(0, 1).contiguous() else: scores = scores.view(-1, self.beam_size, scores.size(-1)) scores = collapse_copy_scores( scores, batch, self._tgt_vocab, src_vocabs, batch_dim=0, batch_offset=batch_offset ) scores = scores.view(decoder_in.size(0), -1, scores.size(-1)) log_probs = scores.squeeze(0).log() # returns [(batch_size x beam_size) , vocab ] when 1 step # or [ tgt_len, batch_size, vocab ] when full sentence return log_probs, attn, attn_key def _translate_batch_with_strategy( self, batch, src_vocabs, decode_strategy): """Translate a batch of sentences step by step using cache. Args: batch: a batch of sentences, yield by data iterator. src_vocabs (list): list of torchtext.data.Vocab if can_copy. decode_strategy (DecodeStrategy): A decode strategy to use for generate translation step by step. Returns: results (dict): The translation results. """ # (0) Prep the components of the search. use_src_map = self.copy_attn parallel_paths = decode_strategy.parallel_paths # beam_size batch_size = batch.batch_size # (1) Run the encoder on the src. src, enc_states, memory_bank, src_lengths = self._run_encoder(batch) self.model.decoder.init_state(src, memory_bank, enc_states) results = { "predictions": None, "scores": None, "attention": None, "batch": batch, "gold_score": self._gold_score( batch, memory_bank, src_lengths, src_vocabs, use_src_map, enc_states, batch_size, src)} # (2) prep decode_strategy. Possibly repeat src objects. src_map = batch.src_map if use_src_map else None fn_map_state, memory_bank, memory_lengths, src_map = \ decode_strategy.initialize(memory_bank, src_lengths, src_map) if fn_map_state is not None: self.model.decoder.map_state(fn_map_state) # (3) Begin decoding step by step: for step in range(decode_strategy.max_length): decoder_input = decode_strategy.current_predictions.view(1, -1, 1) log_probs, attn, attn_key = self._decode_and_generate( decoder_input, memory_bank, batch, src_vocabs, memory_lengths=memory_lengths, src_map=src_map, step=step, batch_offset=decode_strategy.batch_offset) decode_strategy.advance(log_probs, attn, attn_key) any_finished = decode_strategy.is_finished.any() if any_finished: decode_strategy.update_finished() if decode_strategy.done: break select_indices = decode_strategy.select_indices if any_finished: # Reorder states. if isinstance(memory_bank, tuple): memory_bank = tuple(x.index_select(1, select_indices) for x in memory_bank) else: memory_bank = memory_bank.index_select(1, select_indices) memory_lengths = memory_lengths.index_select(0, select_indices) if src_map is not None: src_map = src_map.index_select(1, select_indices) if parallel_paths > 1 or any_finished: self.model.decoder.map_state( lambda state, dim: state.index_select(dim, select_indices)) results["scores"] = decode_strategy.scores results["predictions"] = decode_strategy.predictions results["attention"] = decode_strategy.attention if self.report_align: results["alignment"] = self._align_forward( batch, decode_strategy.predictions) else: results["alignment"] = [[] for _ in range(batch_size)] return results def _score_target(self, batch, memory_bank, src_lengths, src_vocabs, src_map): tgt = batch.tgt tgt_in = tgt[:-1] log_probs, attn = self._decode_and_generate( tgt_in, memory_bank, batch, src_vocabs, memory_lengths=src_lengths, src_map=src_map) log_probs[:, :, self._tgt_pad_idx] = 0 gold = tgt[1:] gold_scores = log_probs.gather(2, gold) gold_scores = gold_scores.sum(dim=0).view(-1) return gold_scores def _report_score(self, name, score_total, words_total): if words_total == 0: msg = "%s No words predicted" % (name,) else: avg_score = score_total / words_total ppl = np.exp(-score_total.item() / words_total) msg = ("%s AVG SCORE: %.4f, %s PPL: %.4f" % ( name, avg_score, name, ppl)) return msg
30,859
38.262087
99
py
data-to-text-hierarchical
data-to-text-hierarchical-master/onmt/utils/rnn_factory.py
""" RNN tools """ import torch.nn as nn import onmt.models def rnn_factory(rnn_type, **kwargs): """ rnn factory, Use pytorch version when available. """ no_pack_padded_seq = False if rnn_type == "SRU": # SRU doesn't support PackedSequence. no_pack_padded_seq = True rnn = onmt.models.sru.SRU(**kwargs) else: rnn = getattr(nn, rnn_type)(**kwargs) return rnn, no_pack_padded_seq
432
23.055556
60
py
data-to-text-hierarchical
data-to-text-hierarchical-master/onmt/utils/optimizers.py
""" Optimizers class """ import torch import torch.optim as optim from torch.nn.utils import clip_grad_norm_ import operator import functools from copy import copy from math import sqrt import types import importlib from onmt.utils.misc import fn_args def build_torch_optimizer(model, opt): """Builds the PyTorch optimizer. We use the default parameters for Adam that are suggested by the original paper https://arxiv.org/pdf/1412.6980.pdf These values are also used by other established implementations, e.g. https://www.tensorflow.org/api_docs/python/tf/train/AdamOptimizer https://keras.io/optimizers/ Recently there are slightly different values used in the paper "Attention is all you need" https://arxiv.org/pdf/1706.03762.pdf, particularly the value beta2=0.98 was used there however, beta2=0.999 is still arguably the more established value, so we use that here as well Args: model: The model to optimize. opt. The dictionary of options. Returns: A ``torch.optim.Optimizer`` instance. """ params = [p for p in model.parameters() if p.requires_grad] betas = [opt.adam_beta1, opt.adam_beta2] if opt.optim == 'sgd': optimizer = optim.SGD(params, lr=opt.learning_rate) elif opt.optim == 'adagrad': optimizer = optim.Adagrad( params, lr=opt.learning_rate, initial_accumulator_value=opt.adagrad_accumulator_init) elif opt.optim == 'adadelta': optimizer = optim.Adadelta(params, lr=opt.learning_rate) elif opt.optim == 'adafactor': optimizer = AdaFactor( params, non_constant_decay=True, enable_factorization=True, weight_decay=0) elif opt.optim == 'adam': optimizer = optim.Adam( params, lr=opt.learning_rate, betas=betas, eps=1e-9) elif opt.optim == 'sparseadam': dense = [] sparse = [] for name, param in model.named_parameters(): if not param.requires_grad: continue # TODO: Find a better way to check for sparse gradients. if 'embed' in name: sparse.append(param) else: dense.append(param) optimizer = MultipleOptimizer( [optim.Adam( dense, lr=opt.learning_rate, betas=betas, eps=1e-8), optim.SparseAdam( sparse, lr=opt.learning_rate, betas=betas, eps=1e-8)]) elif opt.optim == 'fusedadam': # we use here a FusedAdam() copy of an old Apex repo optimizer = FusedAdam( params, lr=opt.learning_rate, betas=betas) else: raise ValueError('Invalid optimizer type: ' + opt.optim) if opt.model_dtype == 'fp16': import apex if opt.optim != 'fusedadam': # In this case use the new AMP API from apex loss_scale = "dynamic" if opt.loss_scale == 0 else opt.loss_scale model, optimizer = apex.amp.initialize( [model, model.generator], optimizer, opt_level=opt.apex_opt_level, loss_scale=loss_scale, keep_batchnorm_fp32=None) else: # In this case use the old FusedAdam with FP16_optimizer wrapper static_loss_scale = opt.loss_scale dynamic_loss_scale = opt.loss_scale == 0 optimizer = apex.optimizers.FP16_Optimizer( optimizer, static_loss_scale=static_loss_scale, dynamic_loss_scale=dynamic_loss_scale) return optimizer def make_learning_rate_decay_fn(opt): """Returns the learning decay function from options.""" if opt.decay_method == 'noam': return functools.partial( noam_decay, warmup_steps=opt.warmup_steps, model_size=opt.rnn_size) elif opt.decay_method == 'noamwd': return functools.partial( noamwd_decay, warmup_steps=opt.warmup_steps, model_size=opt.rnn_size, rate=opt.learning_rate_decay, decay_steps=opt.decay_steps, start_step=opt.start_decay_steps) elif opt.decay_method == 'rsqrt': return functools.partial( rsqrt_decay, warmup_steps=opt.warmup_steps) elif opt.start_decay_steps is not None: return functools.partial( exponential_decay, rate=opt.learning_rate_decay, decay_steps=opt.decay_steps, start_step=opt.start_decay_steps) def noam_decay(step, warmup_steps, model_size): """Learning rate schedule described in https://arxiv.org/pdf/1706.03762.pdf. """ return ( model_size ** (-0.5) * min(step ** (-0.5), step * warmup_steps**(-1.5))) def noamwd_decay(step, warmup_steps, model_size, rate, decay_steps, start_step=0): """Learning rate schedule optimized for huge batches """ return ( model_size ** (-0.5) * min(step ** (-0.5), step * warmup_steps**(-1.5)) * rate ** (max(step - start_step + decay_steps, 0) // decay_steps)) def exponential_decay(step, rate, decay_steps, start_step=0): """A standard exponential decay, scaling the learning rate by :obj:`rate` every :obj:`decay_steps` steps. """ return rate ** (max(step - start_step + decay_steps, 0) // decay_steps) def rsqrt_decay(step, warmup_steps): """Decay based on the reciprocal of the step square root.""" return 1.0 / sqrt(max(step, warmup_steps)) class MultipleOptimizer(object): """ Implement multiple optimizers needed for sparse adam """ def __init__(self, op): """ ? """ self.optimizers = op @property def param_groups(self): param_groups = [] for optimizer in self.optimizers: param_groups.extend(optimizer.param_groups) return param_groups def zero_grad(self): """ ? """ for op in self.optimizers: op.zero_grad() def step(self): """ ? """ for op in self.optimizers: op.step() @property def state(self): """ ? """ return {k: v for op in self.optimizers for k, v in op.state.items()} def state_dict(self): """ ? """ return [op.state_dict() for op in self.optimizers] def load_state_dict(self, state_dicts): """ ? """ assert len(state_dicts) == len(self.optimizers) for i in range(len(state_dicts)): self.optimizers[i].load_state_dict(state_dicts[i]) class Optimizer(object): """ Controller class for optimization. Mostly a thin wrapper for `optim`, but also useful for implementing rate scheduling beyond what is currently available. Also implements necessary methods for training RNNs such as grad manipulations. """ def __init__(self, optimizer, learning_rate, learning_rate_decay_fn=None, max_grad_norm=None): """Initializes the controller. Args: optimizer: A ``torch.optim.Optimizer`` instance. learning_rate: The initial learning rate. learning_rate_decay_fn: An optional callable taking the current step as argument and return a learning rate scaling factor. max_grad_norm: Clip gradients to this global norm. """ self._optimizer = optimizer self._learning_rate = learning_rate self._learning_rate_decay_fn = learning_rate_decay_fn self._max_grad_norm = max_grad_norm or 0 self._training_step = 1 self._decay_step = 1 self._fp16 = None @classmethod def from_opt(cls, model, opt, checkpoint=None): """Builds the optimizer from options. Args: cls: The ``Optimizer`` class to instantiate. model: The model to optimize. opt: The dict of user options. checkpoint: An optional checkpoint to load states from. Returns: An ``Optimizer`` instance. """ optim_opt = opt optim_state_dict = None if opt.train_from and checkpoint is not None: optim = checkpoint['optim'] ckpt_opt = checkpoint['opt'] ckpt_state_dict = {} if isinstance(optim, Optimizer): # Backward compatibility. ckpt_state_dict['training_step'] = optim._step + 1 ckpt_state_dict['decay_step'] = optim._step + 1 ckpt_state_dict['optimizer'] = optim.optimizer.state_dict() else: ckpt_state_dict = optim if opt.reset_optim == 'none': # Load everything from the checkpoint. optim_opt = ckpt_opt optim_state_dict = ckpt_state_dict elif opt.reset_optim == 'all': # Build everything from scratch. pass elif opt.reset_optim == 'states': # Reset optimizer, keep options. optim_opt = ckpt_opt optim_state_dict = ckpt_state_dict del optim_state_dict['optimizer'] elif opt.reset_optim == 'keep_states': # Reset options, keep optimizer. optim_state_dict = ckpt_state_dict optimizer = cls( build_torch_optimizer(model, optim_opt), optim_opt.learning_rate, learning_rate_decay_fn=make_learning_rate_decay_fn(optim_opt), max_grad_norm=optim_opt.max_grad_norm) if opt.model_dtype == "fp16": if opt.optim == "fusedadam": optimizer._fp16 = "legacy" else: optimizer._fp16 = "amp" if optim_state_dict: optimizer.load_state_dict(optim_state_dict) return optimizer @property def training_step(self): """The current training step.""" return self._training_step def learning_rate(self): """Returns the current learning rate.""" if self._learning_rate_decay_fn is None: return self._learning_rate scale = self._learning_rate_decay_fn(self._decay_step) return scale * self._learning_rate def state_dict(self): return { 'training_step': self._training_step, 'decay_step': self._decay_step, 'optimizer': self._optimizer.state_dict() } def load_state_dict(self, state_dict): self._training_step = state_dict['training_step'] # State can be partially restored. if 'decay_step' in state_dict: self._decay_step = state_dict['decay_step'] if 'optimizer' in state_dict: self._optimizer.load_state_dict(state_dict['optimizer']) def zero_grad(self): """Zero the gradients of optimized parameters.""" self._optimizer.zero_grad() def backward(self, loss): """Wrapper for backward pass. Some optimizer requires ownership of the backward pass.""" if self._fp16 == "amp": import apex with apex.amp.scale_loss(loss, self._optimizer) as scaled_loss: scaled_loss.backward() elif self._fp16 == "legacy": kwargs = {} if "update_master_grads" in fn_args(self._optimizer.backward): kwargs["update_master_grads"] = True self._optimizer.backward(loss, **kwargs) else: loss.backward() def step(self): """Update the model parameters based on current gradients. Optionally, will employ gradient modification or update learning rate. """ learning_rate = self.learning_rate() if self._fp16 == "legacy": if hasattr(self._optimizer, "update_master_grads"): self._optimizer.update_master_grads() if hasattr(self._optimizer, "clip_master_grads") and \ self._max_grad_norm > 0: self._optimizer.clip_master_grads(self._max_grad_norm) for group in self._optimizer.param_groups: group['lr'] = learning_rate if self._fp16 is None and self._max_grad_norm > 0: clip_grad_norm_(group['params'], self._max_grad_norm) self._optimizer.step() self._decay_step += 1 self._training_step += 1 # Code below is an implementation of https://arxiv.org/pdf/1804.04235.pdf # inspired but modified from https://github.com/DeadAt0m/adafactor-pytorch class AdaFactor(torch.optim.Optimizer): def __init__(self, params, lr=None, beta1=0.9, beta2=0.999, eps1=1e-30, eps2=1e-3, cliping_threshold=1, non_constant_decay=True, enable_factorization=True, ams_grad=True, weight_decay=0): enable_momentum = beta1 != 0 if non_constant_decay: ams_grad = False defaults = dict(lr=lr, beta1=beta1, beta2=beta2, eps1=eps1, eps2=eps2, cliping_threshold=cliping_threshold, weight_decay=weight_decay, ams_grad=ams_grad, enable_factorization=enable_factorization, enable_momentum=enable_momentum, non_constant_decay=non_constant_decay) super(AdaFactor, self).__init__(params, defaults) def __setstate__(self, state): super(AdaFactor, self).__setstate__(state) def _experimental_reshape(self, shape): temp_shape = shape[2:] if len(temp_shape) == 1: new_shape = (shape[0], shape[1]*shape[2]) else: tmp_div = len(temp_shape) // 2 + len(temp_shape) % 2 new_shape = (shape[0]*functools.reduce(operator.mul, temp_shape[tmp_div:], 1), shape[1]*functools.reduce(operator.mul, temp_shape[:tmp_div], 1)) return new_shape, copy(shape) def _check_shape(self, shape): ''' output1 - True - algorithm for matrix, False - vector; output2 - need reshape ''' if len(shape) > 2: return True, True elif len(shape) == 2: return True, False elif len(shape) == 2 and (shape[0] == 1 or shape[1] == 1): return False, False else: return False, False def _rms(self, x): return sqrt(torch.mean(x.pow(2))) def step(self, closure=None): loss = None if closure is not None: loss = closure() for group in self.param_groups: for p in group['params']: if p.grad is None: continue grad = p.grad.data if grad.is_sparse: raise RuntimeError('Adam does not support sparse \ gradients, use SparseAdam instead') is_matrix, is_need_reshape = self._check_shape(grad.size()) new_shape = p.data.size() if is_need_reshape and group['enable_factorization']: new_shape, old_shape = \ self._experimental_reshape(p.data.size()) grad = grad.view(new_shape) state = self.state[p] if len(state) == 0: state['step'] = 0 if group['enable_momentum']: state['exp_avg'] = torch.zeros(new_shape, dtype=torch.float32, device=p.grad.device) if is_matrix and group['enable_factorization']: state['exp_avg_sq_R'] = \ torch.zeros((1, new_shape[1]), dtype=torch.float32, device=p.grad.device) state['exp_avg_sq_C'] = \ torch.zeros((new_shape[0], 1), dtype=torch.float32, device=p.grad.device) else: state['exp_avg_sq'] = torch.zeros(new_shape, dtype=torch.float32, device=p.grad.device) if group['ams_grad']: state['exp_avg_sq_hat'] = \ torch.zeros(new_shape, dtype=torch.float32, device=p.grad.device) if group['enable_momentum']: exp_avg = state['exp_avg'] if is_matrix and group['enable_factorization']: exp_avg_sq_r = state['exp_avg_sq_R'] exp_avg_sq_c = state['exp_avg_sq_C'] else: exp_avg_sq = state['exp_avg_sq'] if group['ams_grad']: exp_avg_sq_hat = state['exp_avg_sq_hat'] state['step'] += 1 lr_t = group['lr'] lr_t *= max(group['eps2'], self._rms(p.data)) if group['enable_momentum']: if group['non_constant_decay']: beta1_t = group['beta1'] * \ (1 - group['beta1'] ** (state['step'] - 1)) \ / (1 - group['beta1'] ** state['step']) else: beta1_t = group['beta1'] exp_avg.mul_(beta1_t).add_(1 - beta1_t, grad) if group['non_constant_decay']: beta2_t = group['beta2'] * \ (1 - group['beta2'] ** (state['step'] - 1)) / \ (1 - group['beta2'] ** state['step']) else: beta2_t = group['beta2'] if is_matrix and group['enable_factorization']: exp_avg_sq_r.mul_(beta2_t). \ add_(1 - beta2_t, torch.sum(torch.mul(grad, grad). add_(group['eps1']), dim=0, keepdim=True)) exp_avg_sq_c.mul_(beta2_t). \ add_(1 - beta2_t, torch.sum(torch.mul(grad, grad). add_(group['eps1']), dim=1, keepdim=True)) v = torch.mul(exp_avg_sq_c, exp_avg_sq_r).div_(torch.sum(exp_avg_sq_r)) else: exp_avg_sq.mul_(beta2_t). \ addcmul_(1 - beta2_t, grad, grad). \ add_((1 - beta2_t)*group['eps1']) v = exp_avg_sq g = grad if group['enable_momentum']: g = torch.div(exp_avg, 1 - beta1_t ** state['step']) if group['ams_grad']: torch.max(exp_avg_sq_hat, v, out=exp_avg_sq_hat) v = exp_avg_sq_hat u = torch.div(g, (torch.div(v, 1 - beta2_t ** state['step'])).sqrt().add_(group['eps1'])) else: u = torch.div(g, v.sqrt()) u.div_(max(1, self._rms(u) / group['cliping_threshold'])) p.data.add_(-lr_t * (u.view(old_shape) if is_need_reshape and group['enable_factorization'] else u)) if group['weight_decay'] != 0: p.data.add_(-group['weight_decay'] * lr_t, p.data) return loss class FusedAdam(torch.optim.Optimizer): """Implements Adam algorithm. Currently GPU-only. Requires Apex to be installed via ``python setup.py install --cuda_ext --cpp_ext``. It has been proposed in `Adam: A Method for Stochastic Optimization`_. Arguments: params (iterable): iterable of parameters to optimize or dicts defining parameter groups. lr (float, optional): learning rate. (default: 1e-3) betas (Tuple[float, float], optional): coefficients used for computing running averages of gradient and its square. (default: (0.9, 0.999)) eps (float, optional): term added to the denominator to improve numerical stability. (default: 1e-8) weight_decay (float, optional): weight decay (L2 penalty) (default: 0) amsgrad (boolean, optional): whether to use the AMSGrad variant of this algorithm from the paper `On the Convergence of Adam and Beyond`_ (default: False) NOT SUPPORTED in FusedAdam! eps_inside_sqrt (boolean, optional): in the 'update parameters' step, adds eps to the bias-corrected second moment estimate before evaluating square root instead of adding it to the square root of second moment estimate as in the original paper. (default: False) .. _Adam: A Method for Stochastic Optimization: https://arxiv.org/abs/1412.6980 .. _On the Convergence of Adam and Beyond: https://openreview.net/forum?id=ryQu7f-RZ """ def __init__(self, params, lr=1e-3, bias_correction=True, betas=(0.9, 0.999), eps=1e-8, eps_inside_sqrt=False, weight_decay=0., max_grad_norm=0., amsgrad=False): global fused_adam_cuda fused_adam_cuda = importlib.import_module("fused_adam_cuda") if amsgrad: raise RuntimeError('AMSGrad variant not supported.') defaults = dict(lr=lr, bias_correction=bias_correction, betas=betas, eps=eps, weight_decay=weight_decay, max_grad_norm=max_grad_norm) super(FusedAdam, self).__init__(params, defaults) self.eps_mode = 0 if eps_inside_sqrt else 1 def step(self, closure=None, grads=None, output_params=None, scale=1., grad_norms=None): """Performs a single optimization step. Arguments: closure (callable, optional): A closure that reevaluates the model and returns the loss. grads (list of tensors, optional): weight gradient to use for the optimizer update. If gradients have type torch.half, parameters are expected to be in type torch.float. (default: None) output params (list of tensors, optional): A reduced precision copy of the updated weights written out in addition to the regular updated weights. Have to be of same type as gradients. (default: None) scale (float, optional): factor to divide gradient tensor values by before applying to weights. (default: 1) """ loss = None if closure is not None: loss = closure() if grads is None: grads_group = [None]*len(self.param_groups) # backward compatibility # assuming a list/generator of parameter means single group elif isinstance(grads, types.GeneratorType): grads_group = [grads] elif type(grads[0]) != list: grads_group = [grads] else: grads_group = grads if output_params is None: output_params_group = [None]*len(self.param_groups) elif isinstance(output_params, types.GeneratorType): output_params_group = [output_params] elif type(output_params[0]) != list: output_params_group = [output_params] else: output_params_group = output_params if grad_norms is None: grad_norms = [None]*len(self.param_groups) for group, grads_this_group, output_params_this_group, \ grad_norm in zip(self.param_groups, grads_group, output_params_group, grad_norms): if grads_this_group is None: grads_this_group = [None]*len(group['params']) if output_params_this_group is None: output_params_this_group = [None]*len(group['params']) # compute combined scale factor for this group combined_scale = scale if group['max_grad_norm'] > 0: # norm is in fact norm*scale clip = ((grad_norm / scale) + 1e-6) / group['max_grad_norm'] if clip > 1: combined_scale = clip * scale bias_correction = 1 if group['bias_correction'] else 0 for p, grad, output_param in zip(group['params'], grads_this_group, output_params_this_group): # note: p.grad should not ever be set for correct operation of # mixed precision optimizer that sometimes sends None gradients if p.grad is None and grad is None: continue if grad is None: grad = p.grad.data if grad.is_sparse: raise RuntimeError('FusedAdam does not support sparse \ gradients, please consider \ SparseAdam instead') state = self.state[p] # State initialization if len(state) == 0: state['step'] = 0 # Exponential moving average of gradient values state['exp_avg'] = torch.zeros_like(p.data) # Exponential moving average of squared gradient values state['exp_avg_sq'] = torch.zeros_like(p.data) exp_avg, exp_avg_sq = state['exp_avg'], state['exp_avg_sq'] beta1, beta2 = group['betas'] state['step'] += 1 out_p = torch.tensor([], dtype=torch.float) if output_param \ is None else output_param fused_adam_cuda.adam(p.data, out_p, exp_avg, exp_avg_sq, grad, group['lr'], beta1, beta2, group['eps'], combined_scale, state['step'], self.eps_mode, bias_correction, group['weight_decay']) return loss
27,188
38.461538
79
py
data-to-text-hierarchical
data-to-text-hierarchical-master/onmt/utils/loss.py
""" This includes: LossComputeBase and the standard NMTLossCompute, and sharded loss compute stuff. """ from __future__ import division import torch import torch.nn as nn import torch.nn.functional as F import onmt from onmt.modules.sparse_losses import SparsemaxLoss from onmt.modules.sparse_activations import LogSparsemax def build_loss_compute(model, tgt_field, opt, train=True): """ Returns a LossCompute subclass which wraps around an nn.Module subclass (such as nn.NLLLoss) which defines the loss criterion. The LossCompute object allows this loss to be computed in shards and passes the relevant data to a Statistics object which handles training/validation logging. Currently, the NMTLossCompute class handles all loss computation except for when using a copy mechanism. """ device = torch.device("cuda" if onmt.utils.misc.use_gpu(opt) else "cpu") padding_idx = tgt_field.vocab.stoi[tgt_field.pad_token] unk_idx = tgt_field.vocab.stoi[tgt_field.unk_token] if opt.lambda_coverage != 0: assert opt.coverage_attn, "--coverage_attn needs to be set in " \ "order to use --lambda_coverage != 0" if opt.copy_attn: criterion = onmt.modules.CopyGeneratorLoss( len(tgt_field.vocab), opt.copy_attn_force, unk_index=unk_idx, ignore_index=padding_idx ) elif opt.label_smoothing > 0 and train: criterion = LabelSmoothingLoss( opt.label_smoothing, len(tgt_field.vocab), ignore_index=padding_idx ) elif isinstance(model.generator[-1], LogSparsemax): criterion = SparsemaxLoss(ignore_index=padding_idx, reduction='sum') else: criterion = nn.NLLLoss(ignore_index=padding_idx, reduction='sum') # if the loss function operates on vectors of raw logits instead of # probabilities, only the first part of the generator needs to be # passed to the NMTLossCompute. At the moment, the only supported # loss function of this kind is the sparsemax loss. use_raw_logits = isinstance(criterion, SparsemaxLoss) loss_gen = model.generator[0] if use_raw_logits else model.generator if opt.copy_attn: compute = onmt.modules.CopyGeneratorLossCompute( criterion, loss_gen, tgt_field.vocab, opt.copy_loss_by_seqlength, lambda_coverage=opt.lambda_coverage ) else: compute = NMTLossCompute( criterion, loss_gen, lambda_coverage=opt.lambda_coverage, lambda_align=opt.lambda_align) compute.to(device) return compute class LossComputeBase(nn.Module): """ Class for managing efficient loss computation. Handles sharding next step predictions and accumulating multiple loss computations Users can implement their own loss computation strategy by making subclass of this one. Users need to implement the _compute_loss() and make_shard_state() methods. Args: generator (:obj:`nn.Module`) : module that maps the output of the decoder to a distribution over the target vocabulary. tgt_vocab (:obj:`Vocab`) : torchtext vocab object representing the target output normalzation (str): normalize by "sents" or "tokens" """ def __init__(self, criterion, generator): super(LossComputeBase, self).__init__() self.criterion = criterion self.generator = generator @property def padding_idx(self): return self.criterion.ignore_index def _make_shard_state(self, batch, output, range_, attns=None): """ Make shard state dictionary for shards() to return iterable shards for efficient loss computation. Subclass must define this method to match its own _compute_loss() interface. Args: batch: the current batch. output: the predict output from the model. range_: the range of examples for computing, the whole batch or a trunc of it? attns: the attns dictionary returned from the model. """ return NotImplementedError def _compute_loss(self, batch, output, target, **kwargs): """ Compute the loss. Subclass must define this method. Args: batch: the current batch. output: the predict output from the model. target: the validate target to compare output with. **kwargs(optional): additional info for computing loss. """ return NotImplementedError def __call__(self, batch, output, attns, normalization=1.0, shard_size=0, trunc_start=0, trunc_size=None): """Compute the forward loss, possibly in shards in which case this method also runs the backward pass and returns ``None`` as the loss value. Also supports truncated BPTT for long sequences by taking a range in the decoder output sequence to back propagate in. Range is from `(trunc_start, trunc_start + trunc_size)`. Note sharding is an exact efficiency trick to relieve memory required for the generation buffers. Truncation is an approximate efficiency trick to relieve the memory required in the RNN buffers. Args: batch (batch) : batch of labeled examples output (:obj:`FloatTensor`) : output of decoder model `[tgt_len x batch x hidden]` attns (dict) : dictionary of attention distributions `[tgt_len x batch x src_len]` normalization: Optional normalization factor. shard_size (int) : maximum number of examples in a shard trunc_start (int) : starting position of truncation window trunc_size (int) : length of truncation window Returns: A tuple with the loss and a :obj:`onmt.utils.Statistics` instance. """ if trunc_size is None: trunc_size = batch.tgt.size(0) - trunc_start trunc_range = (trunc_start, trunc_start + trunc_size) shard_state = self._make_shard_state(batch, output, trunc_range, attns) if shard_size == 0: loss, stats = self._compute_loss(batch, **shard_state) return loss / float(normalization), stats batch_stats = onmt.utils.Statistics() for shard in shards(shard_state, shard_size): loss, stats = self._compute_loss(batch, **shard) loss.div(float(normalization)).backward() batch_stats.update(stats) return None, batch_stats def _stats(self, loss, scores, target): """ Args: loss (:obj:`FloatTensor`): the loss computed by the loss criterion. scores (:obj:`FloatTensor`): a score for each possible output target (:obj:`FloatTensor`): true targets Returns: :obj:`onmt.utils.Statistics` : statistics for this batch. """ pred = scores.max(1)[1] non_padding = target.ne(self.padding_idx) num_correct = pred.eq(target).masked_select(non_padding).sum().item() num_non_padding = non_padding.sum().item() return onmt.utils.Statistics(loss.item(), num_non_padding, num_correct) def _bottle(self, _v): return _v.view(-1, _v.size(2)) def _unbottle(self, _v, batch_size): return _v.view(-1, batch_size, _v.size(1)) class LabelSmoothingLoss(nn.Module): """ With label smoothing, KL-divergence between q_{smoothed ground truth prob.}(w) and p_{prob. computed by model}(w) is minimized. """ def __init__(self, label_smoothing, tgt_vocab_size, ignore_index=-100): assert 0.0 < label_smoothing <= 1.0 self.ignore_index = ignore_index super(LabelSmoothingLoss, self).__init__() smoothing_value = label_smoothing / (tgt_vocab_size - 2) one_hot = torch.full((tgt_vocab_size,), smoothing_value) one_hot[self.ignore_index] = 0 self.register_buffer('one_hot', one_hot.unsqueeze(0)) self.confidence = 1.0 - label_smoothing def forward(self, output, target): """ output (FloatTensor): batch_size x n_classes target (LongTensor): batch_size """ model_prob = self.one_hot.repeat(target.size(0), 1) model_prob.scatter_(1, target.unsqueeze(1), self.confidence) model_prob.masked_fill_((target == self.ignore_index).unsqueeze(1), 0) return F.kl_div(output, model_prob, reduction='sum') class NMTLossCompute(LossComputeBase): """ Standard NMT Loss Computation. """ def __init__(self, criterion, generator, normalization="sents", lambda_coverage=0.0, lambda_align=0.0): super(NMTLossCompute, self).__init__(criterion, generator) self.lambda_coverage = lambda_coverage self.lambda_align = lambda_align def _make_shard_state(self, batch, output, range_, attns=None): shard_state = { "output": output, "target": batch.tgt[range_[0] + 1: range_[1], :, 0], } if self.lambda_coverage != 0.0: coverage = attns.get("coverage", None) std = attns.get("std", None) assert attns is not None assert std is not None, "lambda_coverage != 0.0 requires " \ "attention mechanism" assert coverage is not None, "lambda_coverage != 0.0 requires " \ "coverage attention" shard_state.update({ "std_attn": attns.get("std"), "coverage_attn": coverage }) if self.lambda_align != 0.0: # attn_align should be in (batch_size, pad_tgt_size, pad_src_size) attn_align = attns.get("align", None) # align_idx should be a Tensor in size([N, 3]), N is total number # of align src-tgt pair in current batch, each as # ['sent_N°_in_batch', 'tgt_id+1', 'src_id'] (check AlignField) align_idx = batch.align assert attns is not None assert attn_align is not None, "lambda_align != 0.0 requires " \ "alignement attention head" assert align_idx is not None, "lambda_align != 0.0 requires " \ "provide guided alignement" pad_tgt_size, batch_size, _ = batch.tgt.size() pad_src_size = batch.src[0].size(0) align_matrix_size = [batch_size, pad_tgt_size, pad_src_size] ref_align = onmt.utils.make_batch_align_matrix( align_idx, align_matrix_size, normalize=True) # NOTE: tgt-src ref alignement that in range_ of shard # (coherent with batch.tgt) shard_state.update({ "align_head": attn_align, "ref_align": ref_align[:, range_[0] + 1: range_[1], :] }) return shard_state def _compute_loss(self, batch, output, target, std_attn=None, coverage_attn=None, align_head=None, ref_align=None): bottled_output = self._bottle(output) scores = self.generator(bottled_output) gtruth = target.view(-1) loss = self.criterion(scores, gtruth) if self.lambda_coverage != 0.0: coverage_loss = self._compute_coverage_loss( std_attn=std_attn, coverage_attn=coverage_attn) loss += coverage_loss if self.lambda_align != 0.0: if align_head.dtype != loss.dtype: # Fix FP16 align_head = align_head.to(loss.dtype) if ref_align.dtype != loss.dtype: ref_align = ref_align.to(loss.dtype) align_loss = self._compute_alignement_loss( align_head=align_head, ref_align=ref_align) loss += align_loss stats = self._stats(loss.clone(), scores, gtruth) return loss, stats def _compute_coverage_loss(self, std_attn, coverage_attn): covloss = torch.min(std_attn, coverage_attn).sum() covloss *= self.lambda_coverage return covloss def _compute_alignement_loss(self, align_head, ref_align): """Compute loss between 2 partial alignment matrix.""" # align_head contains value in [0, 1) presenting attn prob, # 0 was resulted by the context attention src_pad_mask # So, the correspand position in ref_align should also be 0 # Therefore, clip align_head to > 1e-18 should be bias free. align_loss = -align_head.clamp(min=1e-18).log().mul(ref_align).sum() align_loss *= self.lambda_align return align_loss def filter_shard_state(state, shard_size=None): for k, v in state.items(): if shard_size is None: yield k, v if v is not None: v_split = [] if isinstance(v, torch.Tensor): for v_chunk in torch.split(v, shard_size): v_chunk = v_chunk.data.clone() v_chunk.requires_grad = v.requires_grad v_split.append(v_chunk) yield k, (v, v_split) def shards(state, shard_size, eval_only=False): """ Args: state: A dictionary which corresponds to the output of *LossCompute._make_shard_state(). The values for those keys are Tensor-like or None. shard_size: The maximum size of the shards yielded by the model. eval_only: If True, only yield the state, nothing else. Otherwise, yield shards. Yields: Each yielded shard is a dict. Side effect: After the last shard, this function does back-propagation. """ if eval_only: yield filter_shard_state(state) else: # non_none: the subdict of the state dictionary where the values # are not None. non_none = dict(filter_shard_state(state, shard_size)) # Now, the iteration: # state is a dictionary of sequences of tensor-like but we # want a sequence of dictionaries of tensors. # First, unzip the dictionary into a sequence of keys and a # sequence of tensor-like sequences. keys, values = zip(*((k, [v_chunk for v_chunk in v_split]) for k, (_, v_split) in non_none.items())) # Now, yield a dictionary for each shard. The keys are always # the same. values is a sequence of length #keys where each # element is a sequence of length #shards. We want to iterate # over the shards, not over the keys: therefore, the values need # to be re-zipped by shard and then each shard can be paired # with the keys. for shard_tensors in zip(*values): yield dict(zip(keys, shard_tensors)) # Assumed backprop'd variables = [] for k, (v, v_split) in non_none.items(): if isinstance(v, torch.Tensor) and state[k].requires_grad: variables.extend(zip(torch.split(state[k], shard_size), [v_chunk.grad for v_chunk in v_split])) inputs, grads = zip(*variables) torch.autograd.backward(inputs, grads)
15,317
39.099476
79
py
data-to-text-hierarchical
data-to-text-hierarchical-master/onmt/utils/misc.py
# -*- coding: utf-8 -*- import torch import random import inspect from itertools import islice, repeat import os def split_corpus(path, shard_size, default=None): """yield a `list` containing `shard_size` line of `path`, or repeatly generate `default` if `path` is None. """ if path is not None: return _split_corpus(path, shard_size) else: return repeat(default) def _split_corpus(path, shard_size): """Yield a `list` containing `shard_size` line of `path`. """ with open(path, "rb") as f: if shard_size <= 0: yield f.readlines() else: while True: shard = list(islice(f, shard_size)) if not shard: break yield shard def aeq(*args): """ Assert all arguments have the same value """ arguments = (arg for arg in args) first = next(arguments) assert all(arg == first for arg in arguments), \ "Not all arguments have the same value: " + str(args) def sequence_mask(lengths, max_len=None): """ Creates a boolean mask from sequence lengths. """ batch_size = lengths.numel() max_len = max_len or lengths.max() return (torch.arange(0, max_len, device=lengths.device) .type_as(lengths) .repeat(batch_size, 1) .lt(lengths.unsqueeze(1))) def tile(x, count, dim=0): """ Tiles x on dimension dim count times. """ perm = list(range(len(x.size()))) if dim != 0: perm[0], perm[dim] = perm[dim], perm[0] x = x.permute(perm).contiguous() out_size = list(x.size()) out_size[0] *= count batch = x.size(0) x = x.view(batch, -1) \ .transpose(0, 1) \ .repeat(count, 1) \ .transpose(0, 1) \ .contiguous() \ .view(*out_size) if dim != 0: x = x.permute(perm).contiguous() return x def use_gpu(opt): """ Creates a boolean if gpu used """ return (hasattr(opt, 'gpu_ranks') and len(opt.gpu_ranks) > 0) or \ (hasattr(opt, 'gpu') and opt.gpu > -1) def set_random_seed(seed, is_cuda): """Sets the random seed.""" if seed > 0: torch.manual_seed(seed) # this one is needed for torchtext random call (shuffled iterator) # in multi gpu it ensures datasets are read in the same order random.seed(seed) # some cudnn methods can be random even after fixing the seed # unless you tell it to be deterministic torch.backends.cudnn.deterministic = True if is_cuda and seed > 0: # These ensure same initialization in multi gpu mode torch.cuda.manual_seed(seed) def generate_relative_positions_matrix(length, max_relative_positions, cache=False): """Generate the clipped relative positions matrix for a given length and maximum relative positions""" if cache: distance_mat = torch.arange(-length+1, 1, 1).unsqueeze(0) else: range_vec = torch.arange(length) range_mat = range_vec.unsqueeze(-1).expand(-1, length).transpose(0, 1) distance_mat = range_mat - range_mat.transpose(0, 1) distance_mat_clipped = torch.clamp(distance_mat, min=-max_relative_positions, max=max_relative_positions) # Shift values to be >= 0 final_mat = distance_mat_clipped + max_relative_positions return final_mat def relative_matmul(x, z, transpose): """Helper function for relative positions attention.""" batch_size = x.shape[0] heads = x.shape[1] length = x.shape[2] x_t = x.permute(2, 0, 1, 3) x_t_r = x_t.reshape(length, heads * batch_size, -1) if transpose: z_t = z.transpose(1, 2) x_tz_matmul = torch.matmul(x_t_r, z_t) else: x_tz_matmul = torch.matmul(x_t_r, z) x_tz_matmul_r = x_tz_matmul.reshape(length, batch_size, heads, -1) x_tz_matmul_r_t = x_tz_matmul_r.permute(1, 2, 0, 3) return x_tz_matmul_r_t def fn_args(fun): """Returns the list of function arguments name.""" return inspect.getfullargspec(fun).args def nwise(iterable, n=2): iterables = tee(iterable, n) [next(iterables[i]) for i in range(n) for j in range(i)] return zip(*iterables) def report_matrix(row_label, column_label, matrix): header_format = "{:>10.10} " + "{:>10.7} " * len(row_label) row_format = "{:>10.10} " + "{:>10.7f} " * len(row_label) output = header_format.format("", *row_label) + '\n' for word, row in zip(column_label, matrix): max_index = row.index(max(row)) row_format = row_format.replace( "{:>10.7f} ", "{:*>10.7f} ", max_index + 1) row_format = row_format.replace( "{:*>10.7f} ", "{:>10.7f} ", max_index) output += row_format.format(word, *row) + '\n' row_format = "{:>10.10} " + "{:>10.7f} " * len(row_label) return output def check_model_config(model_config, root): # we need to check the model path + any tokenizer path for model in model_config["models"]: model_path = os.path.join(root, model) if not os.path.exists(model_path): raise FileNotFoundError( "{} from model {} does not exist".format( model_path, model_config["id"])) if "tokenizer" in model_config.keys(): if "params" in model_config["tokenizer"].keys(): for k, v in model_config["tokenizer"]["params"].items(): if k.endswith("path"): tok_path = os.path.join(root, v) if not os.path.exists(tok_path): raise FileNotFoundError( "{} from model {} does not exist".format( tok_path, model_config["id"]))
5,885
31.7
78
py
data-to-text-hierarchical
data-to-text-hierarchical-master/onmt/utils/report_manager.py
""" Report manager utility """ from __future__ import print_function import time from datetime import datetime import onmt from onmt.utils.logging import logger def build_report_manager(opt, gpu_rank): if opt.tensorboard and gpu_rank == 0: from torch.utils.tensorboard import SummaryWriter tensorboard_log_dir = opt.tensorboard_log_dir if not opt.train_from: tensorboard_log_dir += datetime.now().strftime("/%b-%d_%H-%M-%S") writer = SummaryWriter(tensorboard_log_dir, comment="Unmt") else: writer = None report_mgr = ReportMgr(opt.report_every, start_time=-1, tensorboard_writer=writer) return report_mgr class ReportMgrBase(object): """ Report Manager Base class Inherited classes should override: * `_report_training` * `_report_step` """ def __init__(self, report_every, start_time=-1.): """ Args: report_every(int): Report status every this many sentences start_time(float): manually set report start time. Negative values means that you will need to set it later or use `start()` """ self.report_every = report_every self.start_time = start_time def start(self): self.start_time = time.time() def log(self, *args, **kwargs): logger.info(*args, **kwargs) def report_training(self, step, num_steps, learning_rate, report_stats, multigpu=False): """ This is the user-defined batch-level traing progress report function. Args: step(int): current step count. num_steps(int): total number of batches. learning_rate(float): current learning rate. report_stats(Statistics): old Statistics instance. Returns: report_stats(Statistics): updated Statistics instance. """ if self.start_time < 0: raise ValueError("""ReportMgr needs to be started (set 'start_time' or use 'start()'""") if step % self.report_every == 0: if multigpu: report_stats = \ onmt.utils.Statistics.all_gather_stats(report_stats) self._report_training( step, num_steps, learning_rate, report_stats) return onmt.utils.Statistics() else: return report_stats def _report_training(self, *args, **kwargs): """ To be overridden """ raise NotImplementedError() def report_step(self, lr, step, train_stats=None, valid_stats=None): """ Report stats of a step Args: train_stats(Statistics): training stats valid_stats(Statistics): validation stats lr(float): current learning rate """ self._report_step( lr, step, train_stats=train_stats, valid_stats=valid_stats) def _report_step(self, *args, **kwargs): raise NotImplementedError() class ReportMgr(ReportMgrBase): def __init__(self, report_every, start_time=-1., tensorboard_writer=None): """ A report manager that writes statistics on standard output as well as (optionally) TensorBoard Args: report_every(int): Report status every this many sentences tensorboard_writer(:obj:`tensorboard.SummaryWriter`): The TensorBoard Summary writer to use or None """ super(ReportMgr, self).__init__(report_every, start_time) self.tensorboard_writer = tensorboard_writer def maybe_log_tensorboard(self, stats, prefix, learning_rate, step): if self.tensorboard_writer is not None: stats.log_tensorboard( prefix, self.tensorboard_writer, learning_rate, step) def _report_training(self, step, num_steps, learning_rate, report_stats): """ See base class method `ReportMgrBase.report_training`. """ report_stats.output(step, num_steps, learning_rate, self.start_time) self.maybe_log_tensorboard(report_stats, "progress", learning_rate, step) report_stats = onmt.utils.Statistics() return report_stats def _report_step(self, lr, step, train_stats=None, valid_stats=None): """ See base class method `ReportMgrBase.report_step`. """ if train_stats is not None: self.log('Train perplexity: %g' % train_stats.ppl()) self.log('Train accuracy: %g' % train_stats.accuracy()) self.maybe_log_tensorboard(train_stats, "train", lr, step) if valid_stats is not None: self.log('Validation perplexity: %g' % valid_stats.ppl()) self.log('Validation accuracy: %g' % valid_stats.accuracy()) self.maybe_log_tensorboard(valid_stats, "valid", lr, step)
5,323
33.128205
78
py
data-to-text-hierarchical
data-to-text-hierarchical-master/onmt/utils/cnn_factory.py
""" Implementation of "Convolutional Sequence to Sequence Learning" """ import torch import torch.nn as nn import torch.nn.init as init import onmt.modules SCALE_WEIGHT = 0.5 ** 0.5 def shape_transform(x): """ Tranform the size of the tensors to fit for conv input. """ return torch.unsqueeze(torch.transpose(x, 1, 2), 3) class GatedConv(nn.Module): """ Gated convolution for CNN class """ def __init__(self, input_size, width=3, dropout=0.2, nopad=False): super(GatedConv, self).__init__() self.conv = onmt.modules.WeightNormConv2d( input_size, 2 * input_size, kernel_size=(width, 1), stride=(1, 1), padding=(width // 2 * (1 - nopad), 0)) init.xavier_uniform_(self.conv.weight, gain=(4 * (1 - dropout))**0.5) self.dropout = nn.Dropout(dropout) def forward(self, x_var): x_var = self.dropout(x_var) x_var = self.conv(x_var) out, gate = x_var.split(int(x_var.size(1) / 2), 1) out = out * torch.sigmoid(gate) return out class StackedCNN(nn.Module): """ Stacked CNN class """ def __init__(self, num_layers, input_size, cnn_kernel_width=3, dropout=0.2): super(StackedCNN, self).__init__() self.dropout = dropout self.num_layers = num_layers self.layers = nn.ModuleList() for _ in range(num_layers): self.layers.append( GatedConv(input_size, cnn_kernel_width, dropout)) def forward(self, x): for conv in self.layers: x = x + conv(x) x *= SCALE_WEIGHT return x
1,620
28.472727
78
py
data-to-text-hierarchical
data-to-text-hierarchical-master/onmt/utils/statistics.py
""" Statistics calculation utility """ from __future__ import division import time import math import sys from onmt.utils.logging import logger class Statistics(object): """ Accumulator for loss statistics. Currently calculates: * accuracy * perplexity * elapsed time """ def __init__(self, loss=0, n_words=0, n_correct=0): self.loss = loss self.n_words = n_words self.n_correct = n_correct self.n_src_words = 0 self.start_time = time.time() @staticmethod def all_gather_stats(stat, max_size=4096): """ Gather a `Statistics` object accross multiple process/nodes Args: stat(:obj:Statistics): the statistics object to gather accross all processes/nodes max_size(int): max buffer size to use Returns: `Statistics`, the update stats object """ stats = Statistics.all_gather_stats_list([stat], max_size=max_size) return stats[0] @staticmethod def all_gather_stats_list(stat_list, max_size=4096): """ Gather a `Statistics` list accross all processes/nodes Args: stat_list(list([`Statistics`])): list of statistics objects to gather accross all processes/nodes max_size(int): max buffer size to use Returns: our_stats(list([`Statistics`])): list of updated stats """ from torch.distributed import get_rank from onmt.utils.distributed import all_gather_list # Get a list of world_size lists with len(stat_list) Statistics objects all_stats = all_gather_list(stat_list, max_size=max_size) our_rank = get_rank() our_stats = all_stats[our_rank] for other_rank, stats in enumerate(all_stats): if other_rank == our_rank: continue for i, stat in enumerate(stats): our_stats[i].update(stat, update_n_src_words=True) return our_stats def update(self, stat, update_n_src_words=False): """ Update statistics by suming values with another `Statistics` object Args: stat: another statistic object update_n_src_words(bool): whether to update (sum) `n_src_words` or not """ self.loss += stat.loss self.n_words += stat.n_words self.n_correct += stat.n_correct if update_n_src_words: self.n_src_words += stat.n_src_words def accuracy(self): """ compute accuracy """ return 100 * (self.n_correct / self.n_words) def xent(self): """ compute cross entropy """ return self.loss / self.n_words def ppl(self): """ compute perplexity """ return math.exp(min(self.loss / self.n_words, 100)) def elapsed_time(self): """ compute elapsed time """ return time.time() - self.start_time def output(self, step, num_steps, learning_rate, start): """Write out statistics to stdout. Args: step (int): current step n_batch (int): total batches start (int): start time of step. """ t = self.elapsed_time() step_fmt = "%2d" % step if num_steps > 0: step_fmt = "%s/%5d" % (step_fmt, num_steps) logger.info( ("Step %s; acc: %6.2f; ppl: %5.2f; xent: %4.2f; " + "lr: %7.5f; %3.0f/%3.0f tok/s; %6.0f sec") % (step_fmt, self.accuracy(), self.ppl(), self.xent(), learning_rate, self.n_src_words / (t + 1e-5), self.n_words / (t + 1e-5), time.time() - start)) sys.stdout.flush() def log_tensorboard(self, prefix, writer, learning_rate, step): """ display statistics to tensorboard """ t = self.elapsed_time() writer.add_scalar(prefix + "/xent", self.xent(), step) writer.add_scalar(prefix + "/ppl", self.ppl(), step) writer.add_scalar(prefix + "/accuracy", self.accuracy(), step) writer.add_scalar(prefix + "/tgtper", self.n_words / t, step) writer.add_scalar(prefix + "/lr", learning_rate, step)
4,291
30.328467
79
py
data-to-text-hierarchical
data-to-text-hierarchical-master/onmt/utils/distributed.py
""" Pytorch Distributed utils This piece of code was heavily inspired by the equivalent of Fairseq-py https://github.com/pytorch/fairseq """ from __future__ import print_function import math import pickle import torch.distributed from onmt.utils.logging import logger def is_master(opt, device_id): return opt.gpu_ranks[device_id] == 0 def multi_init(opt, device_id): dist_init_method = 'tcp://{master_ip}:{master_port}'.format( master_ip=opt.master_ip, master_port=opt.master_port) dist_world_size = opt.world_size torch.distributed.init_process_group( backend=opt.gpu_backend, init_method=dist_init_method, world_size=dist_world_size, rank=opt.gpu_ranks[device_id]) gpu_rank = torch.distributed.get_rank() if not is_master(opt, device_id): logger.disabled = True return gpu_rank def all_reduce_and_rescale_tensors(tensors, rescale_denom, buffer_size=10485760): """All-reduce and rescale tensors in chunks of the specified size. Args: tensors: list of Tensors to all-reduce rescale_denom: denominator for rescaling summed Tensors buffer_size: all-reduce chunk size in bytes """ # buffer size in bytes, determine equiv. # of elements based on data type buffer_t = tensors[0].new( math.ceil(buffer_size / tensors[0].element_size())).zero_() buffer = [] def all_reduce_buffer(): # copy tensors into buffer_t offset = 0 for t in buffer: numel = t.numel() buffer_t[offset:offset+numel].copy_(t.view(-1)) offset += numel # all-reduce and rescale torch.distributed.all_reduce(buffer_t[:offset]) buffer_t.div_(rescale_denom) # copy all-reduced buffer back into tensors offset = 0 for t in buffer: numel = t.numel() t.view(-1).copy_(buffer_t[offset:offset+numel]) offset += numel filled = 0 for t in tensors: sz = t.numel() * t.element_size() if sz > buffer_size: # tensor is bigger than buffer, all-reduce and rescale directly torch.distributed.all_reduce(t) t.div_(rescale_denom) elif filled + sz > buffer_size: # buffer is full, all-reduce and replace buffer with grad all_reduce_buffer() buffer = [t] filled = sz else: # add tensor to buffer buffer.append(t) filled += sz if len(buffer) > 0: all_reduce_buffer() def all_gather_list(data, max_size=4096): """Gathers arbitrary data from all nodes into a list.""" world_size = torch.distributed.get_world_size() if not hasattr(all_gather_list, '_in_buffer') or \ max_size != all_gather_list._in_buffer.size(): all_gather_list._in_buffer = torch.cuda.ByteTensor(max_size) all_gather_list._out_buffers = [ torch.cuda.ByteTensor(max_size) for i in range(world_size) ] in_buffer = all_gather_list._in_buffer out_buffers = all_gather_list._out_buffers enc = pickle.dumps(data) enc_size = len(enc) if enc_size + 2 > max_size: raise ValueError( 'encoded data exceeds max_size: {}'.format(enc_size + 2)) assert max_size < 255*256 in_buffer[0] = enc_size // 255 # this encoding works for max_size < 65k in_buffer[1] = enc_size % 255 in_buffer[2:enc_size+2] = torch.ByteTensor(list(enc)) torch.distributed.all_gather(out_buffers, in_buffer.cuda()) results = [] for i in range(world_size): out_buffer = out_buffers[i] size = (255 * out_buffer[0].item()) + out_buffer[1].item() bytes_list = bytes(out_buffer[2:size+2].tolist()) result = pickle.loads(bytes_list) results.append(result) return results
3,926
30.926829
77
py
data-to-text-hierarchical
data-to-text-hierarchical-master/onmt/utils/alignment.py
# -*- coding: utf-8 -*- import torch from itertools import accumulate def make_batch_align_matrix(index_tensor, size=None, normalize=False): """ Convert a sparse index_tensor into a batch of alignment matrix, with row normalize to the sum of 1 if set normalize. Args: index_tensor (LongTensor): ``(N, 3)`` of [batch_id, tgt_id, src_id] size (List[int]): Size of the sparse tensor. normalize (bool): if normalize the 2nd dim of resulting tensor. """ n_fill, device = index_tensor.size(0), index_tensor.device value_tensor = torch.ones([n_fill], dtype=torch.float) dense_tensor = torch.sparse_coo_tensor( index_tensor.t(), value_tensor, size=size, device=device).to_dense() if normalize: row_sum = dense_tensor.sum(-1, keepdim=True) # sum by row(tgt) # threshold on 1 to avoid div by 0 torch.nn.functional.threshold(row_sum, 1, 1, inplace=True) dense_tensor.div_(row_sum) return dense_tensor def extract_alignment(align_matrix, tgt_mask, src_lens, n_best): """ Extract a batched align_matrix into its src indice alignment lists, with tgt_mask to filter out invalid tgt position as EOS/PAD. BOS already excluded from tgt_mask in order to match prediction. Args: align_matrix (Tensor): ``(B, tgt_len, src_len)``, attention head normalized by Softmax(dim=-1) tgt_mask (BoolTensor): ``(B, tgt_len)``, True for EOS, PAD. src_lens (LongTensor): ``(B,)``, containing valid src length n_best (int): a value indicating number of parallel translation. * B: denote flattened batch as B = batch_size * n_best. Returns: alignments (List[List[FloatTensor]]): ``(batch_size, n_best,)``, containing valid alignment matrix for each translation. """ batch_size_n_best = align_matrix.size(0) assert batch_size_n_best % n_best == 0 alignments = [[] for _ in range(batch_size_n_best // n_best)] # treat alignment matrix one by one as each have different lengths for i, (am_b, tgt_mask_b, src_len) in enumerate( zip(align_matrix, tgt_mask, src_lens)): valid_tgt = ~tgt_mask_b valid_tgt_len = valid_tgt.sum() # get valid alignment (sub-matrix from full paded aligment matrix) am_valid_tgt = am_b.masked_select(valid_tgt.unsqueeze(-1)) \ .view(valid_tgt_len, -1) valid_alignment = am_valid_tgt[:, :src_len] # only keep valid src alignments[i // n_best].append(valid_alignment) return alignments def build_align_pharaoh(valid_alignment): """Convert valid alignment matrix to i-j Pharaoh format.(0 indexed)""" align_pairs = [] tgt_align_src_id = valid_alignment.argmax(dim=-1) for tgt_id, src_id in enumerate(tgt_align_src_id.tolist()): align_pairs.append(str(src_id) + "-" + str(tgt_id)) align_pairs.sort(key=lambda x: int(x.split('-')[-1])) # sort by tgt_id align_pairs.sort(key=lambda x: int(x.split('-')[0])) # sort by src_id return align_pairs def to_word_align(src, tgt, subword_align, mode): """Convert subword alignment to word alignment. Args: src (string): tokenized sentence in source language. tgt (string): tokenized sentence in target language. subword_align (string): align_pharaoh correspond to src-tgt. mode (string): tokenization mode used by src and tgt, choose from ["joiner", "spacer"]. Returns: word_align (string): converted alignments correspand to detokenized src-tgt. """ src, tgt = src.strip().split(), tgt.strip().split() subword_align = {(int(a), int(b)) for a, b in (x.split("-") for x in subword_align.split())} if mode == 'joiner': src_map = subword_map_by_joiner(src, marker='■') tgt_map = subword_map_by_joiner(tgt, marker='■') elif mode == 'spacer': src_map = subword_map_by_spacer(src, marker='▁') tgt_map = subword_map_by_spacer(tgt, marker='▁') else: raise ValueError("Invalid value for argument mode!") word_align = list({"{}-{}".format(src_map[a], tgt_map[b]) for a, b in subword_align}) word_align.sort(key=lambda x: int(x.split('-')[-1])) # sort by tgt_id word_align.sort(key=lambda x: int(x.split('-')[0])) # sort by src_id return " ".join(word_align) def subword_map_by_joiner(subwords, marker='■'): """Return word id for each subword token (annotate by joiner).""" flags = [0] * len(subwords) for i, tok in enumerate(subwords): if tok.endswith(marker): flags[i] = 1 if tok.startswith(marker): assert i >= 1 and flags[i-1] != 1, \ "Sentence `{}` not correct!".format(" ".join(subwords)) flags[i-1] = 1 marker_acc = list(accumulate([0] + flags[:-1])) word_group = [(i - maker_sofar) for i, maker_sofar in enumerate(marker_acc)] return word_group def subword_map_by_spacer(subwords, marker='▁'): """Return word id for each subword token (annotate by spacer).""" word_group = list(accumulate([int(marker in x) for x in subwords])) if word_group[0] == 1: # when dummy prefix is set word_group = [item - 1 for item in word_group] return word_group
5,370
39.689394
76
py
data-to-text-hierarchical
data-to-text-hierarchical-master/onmt/utils/parse.py
import configargparse as cfargparse import os import torch import onmt.opts as opts from onmt.utils.logging import logger class ArgumentParser(cfargparse.ArgumentParser): def __init__( self, config_file_parser_class=cfargparse.YAMLConfigFileParser, formatter_class=cfargparse.ArgumentDefaultsHelpFormatter, **kwargs): super(ArgumentParser, self).__init__( config_file_parser_class=config_file_parser_class, formatter_class=formatter_class, **kwargs) @classmethod def defaults(cls, *args): """Get default arguments added to a parser by all ``*args``.""" dummy_parser = cls() for callback in args: callback(dummy_parser) defaults = dummy_parser.parse_known_args([])[0] return defaults @classmethod def update_model_opts(cls, model_opt): if model_opt.word_vec_size > 0: model_opt.src_word_vec_size = model_opt.word_vec_size model_opt.tgt_word_vec_size = model_opt.word_vec_size if model_opt.layers > 0: model_opt.enc_layers = model_opt.layers model_opt.dec_layers = model_opt.layers if model_opt.rnn_size > 0: model_opt.enc_rnn_size = model_opt.rnn_size model_opt.dec_rnn_size = model_opt.rnn_size model_opt.brnn = model_opt.encoder_type == "brnn" if model_opt.copy_attn_type is None: model_opt.copy_attn_type = model_opt.global_attention if model_opt.alignment_layer is None: model_opt.alignment_layer = -2 model_opt.lambda_align = 0.0 model_opt.full_context_alignment = False @classmethod def validate_model_opts(cls, model_opt): assert model_opt.model_type in ["text", "table", "img", "audio", "vec"], \ "Unsupported model type %s" % model_opt.model_type # this check is here because audio allows the encoder and decoder to # be different sizes, but other model types do not yet same_size = model_opt.enc_rnn_size == model_opt.dec_rnn_size assert model_opt.model_type == 'audio' or same_size, \ "The encoder and decoder rnns must be the same size for now" assert model_opt.rnn_type != "SRU" or model_opt.gpu_ranks, \ "Using SRU requires -gpu_ranks set." if model_opt.share_embeddings: if model_opt.model_type not in ["text", "table"]: raise AssertionError( "--share_embeddings requires --model_type text.") if model_opt.lambda_align > 0.0: assert model_opt.decoder_type == 'transformer', \ "Only transformer is supported to joint learn alignment." assert model_opt.alignment_layer < model_opt.dec_layers and \ model_opt.alignment_layer >= -model_opt.dec_layers, \ "N° alignment_layer should be smaller than number of layers." logger.info("Joint learn alignment at layer [{}] " "with {} heads in full_context '{}'.".format( model_opt.alignment_layer, model_opt.alignment_heads, model_opt.full_context_alignment)) @classmethod def ckpt_model_opts(cls, ckpt_opt): # Load default opt values, then overwrite with the opts in # the checkpoint. That way, if there are new options added, # the defaults are used. opt = cls.defaults(opts.model_opts) opt.__dict__.update(ckpt_opt.__dict__) return opt @classmethod def validate_train_opts(cls, opt): if opt.epochs: raise AssertionError( "-epochs is deprecated please use -train_steps.") if opt.truncated_decoder > 0 and max(opt.accum_count) > 1: raise AssertionError("BPTT is not compatible with -accum > 1") if opt.gpuid: raise AssertionError( "gpuid is deprecated see world_size and gpu_ranks") if torch.cuda.is_available() and not opt.gpu_ranks: logger.warn("You have a CUDA device, should run with -gpu_ranks") if opt.world_size < len(opt.gpu_ranks): raise AssertionError( "parameter counts of -gpu_ranks must be less or equal " "than -world_size.") if opt.world_size == len(opt.gpu_ranks) and \ min(opt.gpu_ranks) > 0: raise AssertionError( "-gpu_ranks should have master(=0) rank " "unless -world_size is greater than len(gpu_ranks).") assert len(opt.data_ids) == len(opt.data_weights), \ "Please check -data_ids and -data_weights options!" assert len(opt.dropout) == len(opt.dropout_steps), \ "Number of dropout values must match accum_steps values" assert len(opt.attention_dropout) == len(opt.dropout_steps), \ "Number of attention_dropout values must match accum_steps values" @classmethod def validate_translate_opts(cls, opt): if opt.beam_size != 1 and opt.random_sampling_topk != 1: raise ValueError('Can either do beam search OR random sampling.') @classmethod def validate_preprocess_args(cls, opt): assert opt.max_shard_size == 0, \ "-max_shard_size is deprecated. Please use \ -shard_size (number of examples) instead." assert opt.shuffle == 0, \ "-shuffle is not implemented. Please shuffle \ your data before pre-processing." assert len(opt.train_src) == len(opt.train_tgt), \ "Please provide same number of src and tgt train files!" assert len(opt.train_src) == len(opt.train_ids), \ "Please provide proper -train_ids for your data!" for file in opt.train_src + opt.train_tgt: assert os.path.isfile(file), "Please check path of %s" % file if len(opt.train_align) == 1 and opt.train_align[0] is None: opt.train_align = [None] * len(opt.train_src) else: assert len(opt.train_align) == len(opt.train_src), \ "Please provide same number of word alignment train \ files as src/tgt!" for file in opt.train_align: assert os.path.isfile(file), "Please check path of %s" % file assert not opt.valid_align or os.path.isfile(opt.valid_align), \ "Please check path of your valid alignment file!" assert not opt.valid_src or os.path.isfile(opt.valid_src), \ "Please check path of your valid src file!" assert not opt.valid_tgt or os.path.isfile(opt.valid_tgt), \ "Please check path of your valid tgt file!" assert not opt.src_vocab or os.path.isfile(opt.src_vocab), \ "Please check path of your src vocab!" assert not opt.tgt_vocab or os.path.isfile(opt.tgt_vocab), \ "Please check path of your tgt vocab!"
7,089
41.45509
82
py
triton
triton-main/python/setup.py
import os import platform import re import shutil import subprocess import sys import sysconfig import tarfile import tempfile import urllib.request from pathlib import Path from typing import NamedTuple from setuptools import Extension, setup from setuptools.command.build_ext import build_ext from setuptools.command.build_py import build_py # Taken from https://github.com/pytorch/pytorch/blob/master/tools/setup_helpers/env.py def check_env_flag(name: str, default: str = "") -> bool: return os.getenv(name, default).upper() in ["ON", "1", "YES", "TRUE", "Y"] def get_build_type(): if check_env_flag("DEBUG"): return "Debug" elif check_env_flag("REL_WITH_DEB_INFO"): return "RelWithDebInfo" elif check_env_flag("TRITON_REL_BUILD_WITH_ASSERTS"): return "TritonRelBuildWithAsserts" else: # TODO: change to release when stable enough return "TritonRelBuildWithAsserts" def get_codegen_backends(): backends = [] env_prefix = "TRITON_CODEGEN_" for name, _ in os.environ.items(): if name.startswith(env_prefix) and check_env_flag(name): assert name.count(env_prefix) <= 1 backends.append(name.replace(env_prefix, '').lower()) return backends # --- third party packages ----- class Package(NamedTuple): package: str name: str url: str include_flag: str lib_flag: str syspath_var_name: str # pybind11 def get_pybind11_package_info(): name = "pybind11-2.10.0" url = "https://github.com/pybind/pybind11/archive/refs/tags/v2.10.0.tar.gz" return Package("pybind11", name, url, "PYBIND11_INCLUDE_DIR", "", "PYBIND11_SYSPATH") # llvm def get_llvm_package_info(): # added statement for Apple Silicon system = platform.system() arch = 'x86_64' if system == "Darwin": system_suffix = "apple-darwin" cpu_type = os.popen('sysctl machdep.cpu.brand_string').read() if "apple" in cpu_type.lower(): arch = 'arm64' elif system == "Linux": vglibc = tuple(map(int, platform.libc_ver()[1].split('.'))) vglibc = vglibc[0] * 100 + vglibc[1] linux_suffix = 'ubuntu-18.04' if vglibc > 217 else 'centos-7' system_suffix = f"linux-gnu-{linux_suffix}" else: return Package("llvm", "LLVM-C.lib", "", "LLVM_INCLUDE_DIRS", "LLVM_LIBRARY_DIR", "LLVM_SYSPATH") use_assert_enabled_llvm = check_env_flag("TRITON_USE_ASSERT_ENABLED_LLVM", "False") release_suffix = "assert" if use_assert_enabled_llvm else "release" name = f'llvm+mlir-17.0.0-{arch}-{system_suffix}-{release_suffix}' version = "llvm-17.0.0-c5dede880d17" url = f"https://github.com/ptillet/triton-llvm-releases/releases/download/{version}/{name}.tar.xz" return Package("llvm", name, url, "LLVM_INCLUDE_DIRS", "LLVM_LIBRARY_DIR", "LLVM_SYSPATH") def get_thirdparty_packages(triton_cache_path): packages = [get_pybind11_package_info(), get_llvm_package_info()] thirdparty_cmake_args = [] for p in packages: package_root_dir = os.path.join(triton_cache_path, p.package) package_dir = os.path.join(package_root_dir, p.name) if p.syspath_var_name in os.environ: package_dir = os.environ[p.syspath_var_name] version_file_path = os.path.join(package_dir, "version.txt") if p.syspath_var_name not in os.environ and\ (not os.path.exists(version_file_path) or Path(version_file_path).read_text() != p.url): try: shutil.rmtree(package_root_dir) except Exception: pass os.makedirs(package_root_dir, exist_ok=True) print(f'downloading and extracting {p.url} ...') ftpstream = urllib.request.urlopen(p.url) file = tarfile.open(fileobj=ftpstream, mode="r|*") file.extractall(path=package_root_dir) # write version url to package_dir with open(os.path.join(package_dir, "version.txt"), "w") as f: f.write(p.url) if p.include_flag: thirdparty_cmake_args.append(f"-D{p.include_flag}={package_dir}/include") if p.lib_flag: thirdparty_cmake_args.append(f"-D{p.lib_flag}={package_dir}/lib") return thirdparty_cmake_args # ---- package data --- def download_and_copy_ptxas(): base_dir = os.path.dirname(__file__) src_path = "bin/ptxas" version = "12.2.91" url = f"https://conda.anaconda.org/nvidia/label/cuda-12.2.0/linux-64/cuda-nvcc-{version}-0.tar.bz2" dst_prefix = os.path.join(base_dir, "triton") dst_suffix = os.path.join("third_party", "cuda", src_path) dst_path = os.path.join(dst_prefix, dst_suffix) is_linux = platform.system() == "Linux" download = False if is_linux: download = True if os.path.exists(dst_path): curr_version = subprocess.check_output([dst_path, "--version"]).decode("utf-8").strip() curr_version = re.search(r"V([.|\d]+)", curr_version).group(1) download = curr_version != version if download: print(f'downloading and extracting {url} ...') ftpstream = urllib.request.urlopen(url) file = tarfile.open(fileobj=ftpstream, mode="r|*") with tempfile.TemporaryDirectory() as temp_dir: file.extractall(path=temp_dir) src_path = os.path.join(temp_dir, src_path) os.makedirs(os.path.split(dst_path)[0], exist_ok=True) shutil.copy(src_path, dst_path) return dst_suffix # ---- cmake extension ---- class CMakeBuildPy(build_py): def run(self) -> None: self.run_command('build_ext') return super().run() class CMakeExtension(Extension): def __init__(self, name, path, sourcedir=""): Extension.__init__(self, name, sources=[]) self.sourcedir = os.path.abspath(sourcedir) self.path = path class CMakeBuild(build_ext): user_options = build_ext.user_options + [('base-dir=', None, 'base directory of Triton')] def initialize_options(self): build_ext.initialize_options(self) self.base_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir)) def finalize_options(self): build_ext.finalize_options(self) def run(self): try: out = subprocess.check_output(["cmake", "--version"]) except OSError: raise RuntimeError( "CMake must be installed to build the following extensions: " + ", ".join(e.name for e in self.extensions) ) match = re.search(r"version\s*(?P<major>\d+)\.(?P<minor>\d+)([\d.]+)?", out.decode()) cmake_major, cmake_minor = int(match.group("major")), int(match.group("minor")) if (cmake_major, cmake_minor) < (3, 18): raise RuntimeError("CMake >= 3.18.0 is required") for ext in self.extensions: self.build_extension(ext) def get_cmake_dir(self): plat_name = sysconfig.get_platform() python_version = sysconfig.get_python_version() dir_name = f"cmake.{plat_name}-{sys.implementation.name}-{python_version}" cmake_dir = Path(self.base_dir) / "python" / "build" / dir_name cmake_dir.mkdir(parents=True, exist_ok=True) return cmake_dir def build_extension(self, ext): lit_dir = shutil.which('lit') user_home = os.getenv("HOME") or os.getenv("USERPROFILE") or \ os.getenv("HOMEPATH") or None if not user_home: raise RuntimeError("Could not find user home directory") triton_cache_path = os.path.join(user_home, ".triton") # lit is used by the test suite thirdparty_cmake_args = get_thirdparty_packages(triton_cache_path) extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.path))) # create build directories if not os.path.exists(self.build_temp): os.makedirs(self.build_temp) # python directories python_include_dir = sysconfig.get_path("platinclude") cmake_args = [ "-DCMAKE_EXPORT_COMPILE_COMMANDS=ON", "-DLLVM_ENABLE_WERROR=ON", "-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=" + extdir, "-DTRITON_BUILD_TUTORIALS=OFF", "-DTRITON_BUILD_PYTHON_MODULE=ON", "-DPython3_EXECUTABLE:FILEPATH=" + sys.executable, "-DCMAKE_VERBOSE_MAKEFILE:BOOL=ON", "-DPYTHON_INCLUDE_DIRS=" + python_include_dir, ] if lit_dir is not None: cmake_args.append("-DLLVM_EXTERNAL_LIT=" + lit_dir) cmake_args.extend(thirdparty_cmake_args) # configuration cfg = get_build_type() build_args = ["--config", cfg] codegen_backends = get_codegen_backends() if len(codegen_backends) > 0: all_codegen_backends = ';'.join(codegen_backends) cmake_args += ["-DTRITON_CODEGEN_BACKENDS=" + all_codegen_backends] if platform.system() == "Windows": cmake_args += [f"-DCMAKE_RUNTIME_OUTPUT_DIRECTORY_{cfg.upper()}={extdir}"] if sys.maxsize > 2**32: cmake_args += ["-A", "x64"] build_args += ["--", "/m"] else: cmake_args += ["-DCMAKE_BUILD_TYPE=" + cfg] max_jobs = os.getenv("MAX_JOBS", str(2 * os.cpu_count())) build_args += ['-j' + max_jobs] if check_env_flag("TRITON_BUILD_WITH_CLANG_LLD"): cmake_args += ["-DCMAKE_C_COMPILER=clang", "-DCMAKE_CXX_COMPILER=clang++", "-DCMAKE_LINKER=lld", "-DCMAKE_EXE_LINKER_FLAGS=-fuse-ld=lld", "-DCMAKE_MODULE_LINKER_FLAGS=-fuse-ld=lld", "-DCMAKE_SHARED_LINKER_FLAGS=-fuse-ld=lld"] env = os.environ.copy() cmake_dir = self.get_cmake_dir() subprocess.check_call(["cmake", self.base_dir] + cmake_args, cwd=cmake_dir, env=env) subprocess.check_call(["cmake", "--build", "."] + build_args, cwd=cmake_dir) download_and_copy_ptxas() setup( name="triton", version="2.1.0", author="Philippe Tillet", author_email="phil@openai.com", description="A language and compiler for custom Deep Learning operations", long_description="", packages=[ "triton", "triton/_C", "triton/common", "triton/compiler", "triton/interpreter", "triton/language", "triton/language/extra", "triton/ops", "triton/ops/blocksparse", "triton/runtime", "triton/runtime/backends", "triton/third_party", "triton/tools", ], install_requires=[ "filelock", ], include_package_data=True, ext_modules=[CMakeExtension("triton", "triton/_C/")], cmdclass={"build_ext": CMakeBuild, "build_py": CMakeBuildPy}, zip_safe=False, # for PyPI keywords=["Compiler", "Deep Learning"], url="https://github.com/openai/triton/", classifiers=[ "Development Status :: 4 - Beta", "Intended Audience :: Developers", "Topic :: Software Development :: Build Tools", "License :: OSI Approved :: MIT License", "Programming Language :: Python :: 3.7", "Programming Language :: Python :: 3.8", "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10", "Programming Language :: Python :: 3.11", ], test_suite="tests", extras_require={ "build": [ "cmake>=3.18", "lit", ], "tests": [ "autopep8", "flake8", "isort", "numpy", "pytest", "scipy>=1.7.1", ], "tutorials": [ "matplotlib", "pandas", "tabulate", ], }, )
11,910
34.876506
122
py
triton
triton-main/python/tutorials/05-layer-norm.py
""" Layer Normalization ==================== In this tutorial, you will write a high-performance layer normalization kernel that runs faster than the PyTorch implementation. In doing so, you will learn about: * Implementing backward pass in Triton. * Implementing parallel reduction in Triton. """ # %% # Motivations # ----------- # # The *LayerNorm* operator was first introduced in [BA2016]_ as a way to improve the performance # of sequential models (e.g., Transformers) or neural networks with small batch size. # It takes a vector :math:`x` as input and produces a vector :math:`y` of the same shape as output. # The normalization is performed by subtracting the mean and dividing by the standard deviation of :math:`x`. # After the normalization, a learnable linear transformation with weights :math:`w` and biases :math:`b` is applied. # The forward pass can be expressed as follows: # # .. math:: # y = \frac{ x - \text{E}[x] }{ \sqrt{\text{Var}(x) + \epsilon} } * w + b # # where :math:`\epsilon` is a small constant added to the denominator for numerical stability. # Let’s first take a look at the forward pass implementation. import torch import triton import triton.language as tl try: # This is https://github.com/NVIDIA/apex, NOT the apex on PyPi, so it # should not be added to extras_require in setup.py. import apex HAS_APEX = True except ModuleNotFoundError: HAS_APEX = False @triton.jit def _layer_norm_fwd_fused( X, # pointer to the input Y, # pointer to the output W, # pointer to the weights B, # pointer to the biases Mean, # pointer to the mean Rstd, # pointer to the 1/std stride, # how much to increase the pointer when moving by 1 row N, # number of columns in X eps, # epsilon to avoid division by zero BLOCK_SIZE: tl.constexpr, ): # Map the program id to the row of X and Y it should compute. row = tl.program_id(0) Y += row * stride X += row * stride # Compute mean mean = 0 _mean = tl.zeros([BLOCK_SIZE], dtype=tl.float32) for off in range(0, N, BLOCK_SIZE): cols = off + tl.arange(0, BLOCK_SIZE) a = tl.load(X + cols, mask=cols < N, other=0.).to(tl.float32) _mean += a mean = tl.sum(_mean, axis=0) / N # Compute variance _var = tl.zeros([BLOCK_SIZE], dtype=tl.float32) for off in range(0, N, BLOCK_SIZE): cols = off + tl.arange(0, BLOCK_SIZE) x = tl.load(X + cols, mask=cols < N, other=0.).to(tl.float32) x = tl.where(cols < N, x - mean, 0.) _var += x * x var = tl.sum(_var, axis=0) / N rstd = 1 / tl.sqrt(var + eps) # Write mean / rstd tl.store(Mean + row, mean) tl.store(Rstd + row, rstd) # Normalize and apply linear transformation for off in range(0, N, BLOCK_SIZE): cols = off + tl.arange(0, BLOCK_SIZE) mask = cols < N w = tl.load(W + cols, mask=mask) b = tl.load(B + cols, mask=mask) x = tl.load(X + cols, mask=mask, other=0.).to(tl.float32) x_hat = (x - mean) * rstd y = x_hat * w + b # Write output tl.store(Y + cols, y, mask=mask) # %% # Backward pass # ------------- # # The backward pass for the layer normalization operator is a bit more involved than the forward pass. # Let :math:`\hat{x}` be the normalized inputs :math:`\frac{ x - \text{E}[x] }{ \sqrt{\text{Var}(x) + \epsilon} }` before the linear transformation, # the Vector-Jacobian Products (VJP) :math:`\nabla_{x}` of :math:`x` are given by: # # .. math:: # \nabla_{x} = \frac{1}{\sigma}\Big( \nabla_{y} \odot w - \underbrace{ \big( \frac{1}{N} \hat{x} \cdot (\nabla_{y} \odot w) \big) }_{c_1} \odot \hat{x} - \underbrace{ \frac{1}{N} \nabla_{y} \cdot w }_{c_2} \Big) # # where :math:`\odot` denotes the element-wise multiplication, :math:`\cdot` denotes the dot product, and :math:`\sigma` is the standard deviation. # :math:`c_1` and :math:`c_2` are intermediate constants that improve the readability of the following implementation. # # For the weights :math:`w` and biases :math:`b`, the VJPs :math:`\nabla_{w}` and :math:`\nabla_{b}` are more straightforward: # # .. math:: # \nabla_{w} = \nabla_{y} \odot \hat{x} \quad \text{and} \quad \nabla_{b} = \nabla_{y} # # Since the same weights :math:`w` and biases :math:`b` are used for all rows in the same batch, their gradients need to sum up. # To perform this step efficiently, we use a parallel reduction strategy: each kernel instance accumulates # partial :math:`\nabla_{w}` and :math:`\nabla_{b}` across certain rows into one of :math:`\text{GROUP_SIZE_M}` independent buffers. # These buffers stay in the L2 cache and then are further reduced by another function to compute the actual :math:`\nabla_{w}` and :math:`\nabla_{b}`. # # Let the number of input rows :math:`M = 4` and :math:`\text{GROUP_SIZE_M} = 2`, # here's a diagram of the parallel reduction strategy for :math:`\nabla_{w}` (:math:`\nabla_{b}` is omitted for brevity): # # .. image:: parallel_reduction.png # # In Stage 1, the rows of X that have the same color share the same buffer and thus a lock is used to ensure that only one kernel instance writes to the buffer at a time. # In Stage 2, the buffers are further reduced to compute the final :math:`\nabla_{w}` and :math:`\nabla_{b}`. # In the following implementation, Stage 1 is implemented by the function :code:`_layer_norm_bwd_dx_fused` and Stage 2 is implemented by the function :code:`_layer_norm_bwd_dwdb`. @triton.jit def _layer_norm_bwd_dx_fused( DX, # pointer to the input gradient DY, # pointer to the output gradient DW, # pointer to the partial sum of weights gradient DB, # pointer to the partial sum of biases gradient X, # pointer to the input W, # pointer to the weights B, # pointer to the biases Mean, # pointer to the mean Rstd, # pointer to the 1/std Lock, # pointer to the lock stride, # how much to increase the pointer when moving by 1 row N, # number of columns in X eps, # epsilon to avoid division by zero GROUP_SIZE_M: tl.constexpr, BLOCK_SIZE_N: tl.constexpr ): # Map the program id to the elements of X, DX, and DY it should compute. row = tl.program_id(0) cols = tl.arange(0, BLOCK_SIZE_N) mask = cols < N X += row * stride DY += row * stride DX += row * stride # Offset locks and weights/biases gradient pointer for parallel reduction lock_id = row % GROUP_SIZE_M Lock += lock_id Count = Lock + GROUP_SIZE_M DW = DW + lock_id * N + cols DB = DB + lock_id * N + cols # Load data to SRAM x = tl.load(X + cols, mask=mask, other=0).to(tl.float32) dy = tl.load(DY + cols, mask=mask, other=0).to(tl.float32) w = tl.load(W + cols, mask=mask).to(tl.float32) mean = tl.load(Mean + row) rstd = tl.load(Rstd + row) # Compute dx xhat = (x - mean) * rstd wdy = w * dy xhat = tl.where(mask, xhat, 0.) wdy = tl.where(mask, wdy, 0.) c1 = tl.sum(xhat * wdy, axis=0) / N c2 = tl.sum(wdy, axis=0) / N dx = (wdy - (xhat * c1 + c2)) * rstd # Write dx tl.store(DX + cols, dx, mask=mask) # Accumulate partial sums for dw/db partial_dw = (dy * xhat).to(w.dtype) partial_db = (dy).to(w.dtype) while tl.atomic_cas(Lock, 0, 1) == 1: pass count = tl.load(Count) # First store doesn't accumulate if count == 0: tl.atomic_xchg(Count, 1) else: partial_dw += tl.load(DW, mask=mask) partial_db += tl.load(DB, mask=mask) tl.store(DW, partial_dw, mask=mask) tl.store(DB, partial_db, mask=mask) # Release the lock tl.atomic_xchg(Lock, 0) @triton.jit def _layer_norm_bwd_dwdb( DW, # pointer to the partial sum of weights gradient DB, # pointer to the partial sum of biases gradient FINAL_DW, # pointer to the weights gradient FINAL_DB, # pointer to the biases gradient M, # GROUP_SIZE_M N, # number of columns BLOCK_SIZE_M: tl.constexpr, BLOCK_SIZE_N: tl.constexpr ): # Map the program id to the elements of DW and DB it should compute. pid = tl.program_id(0) cols = pid * BLOCK_SIZE_N + tl.arange(0, BLOCK_SIZE_N) dw = tl.zeros((BLOCK_SIZE_M, BLOCK_SIZE_N), dtype=tl.float32) db = tl.zeros((BLOCK_SIZE_M, BLOCK_SIZE_N), dtype=tl.float32) # Iterate through the rows of DW and DB to sum the partial sums. for i in range(0, M, BLOCK_SIZE_M): rows = i + tl.arange(0, BLOCK_SIZE_M) mask = (rows[:, None] < M) & (cols[None, :] < N) offs = rows[:, None] * N + cols[None, :] dw += tl.load(DW + offs, mask=mask, other=0.) db += tl.load(DB + offs, mask=mask, other=0.) # Write the final sum to the output. sum_dw = tl.sum(dw, axis=0) sum_db = tl.sum(db, axis=0) tl.store(FINAL_DW + cols, sum_dw, mask=cols < N) tl.store(FINAL_DB + cols, sum_db, mask=cols < N) # %% # Benchmark # --------- # # We can now compare the performance of our kernel against that of PyTorch. # Here we focus on inputs that have Less than 64KB per feature. # Specifically, one can set :code:`'mode': 'backward'` to benchmark the backward pass. class LayerNorm(torch.autograd.Function): @staticmethod def forward(ctx, x, normalized_shape, weight, bias, eps): # allocate output y = torch.empty_like(x) # reshape input data into 2D tensor x_arg = x.reshape(-1, x.shape[-1]) M, N = x_arg.shape mean = torch.empty((M, ), dtype=torch.float32, device='cuda') rstd = torch.empty((M, ), dtype=torch.float32, device='cuda') # Less than 64KB per feature: enqueue fused kernel MAX_FUSED_SIZE = 65536 // x.element_size() BLOCK_SIZE = min(MAX_FUSED_SIZE, triton.next_power_of_2(N)) if N > BLOCK_SIZE: raise RuntimeError("This layer norm doesn't support feature dim >= 64KB.") # heuristics for number of warps num_warps = min(max(BLOCK_SIZE // 256, 1), 8) # enqueue kernel _layer_norm_fwd_fused[(M,)](x_arg, y, weight, bias, mean, rstd, x_arg.stride(0), N, eps, BLOCK_SIZE=BLOCK_SIZE, num_warps=num_warps) ctx.save_for_backward(x, weight, bias, mean, rstd) ctx.BLOCK_SIZE = BLOCK_SIZE ctx.num_warps = num_warps ctx.eps = eps return y @staticmethod def backward(ctx, dy): x, w, b, m, v = ctx.saved_tensors # heuristics for amount of parallel reduction stream for DW/DB N = w.shape[0] GROUP_SIZE_M = 64 if N <= 8192: GROUP_SIZE_M = 96 if N <= 4096: GROUP_SIZE_M = 128 if N <= 1024: GROUP_SIZE_M = 256 # allocate output locks = torch.zeros(2 * GROUP_SIZE_M, dtype=torch.int32, device='cuda') _dw = torch.empty((GROUP_SIZE_M, w.shape[0]), dtype=x.dtype, device=w.device) _db = torch.empty((GROUP_SIZE_M, w.shape[0]), dtype=x.dtype, device=w.device) dw = torch.empty((w.shape[0],), dtype=w.dtype, device=w.device) db = torch.empty((w.shape[0],), dtype=w.dtype, device=w.device) dx = torch.empty_like(dy) # enqueue kernel using forward pass heuristics # also compute partial sums for DW and DB x_arg = x.reshape(-1, x.shape[-1]) M, N = x_arg.shape _layer_norm_bwd_dx_fused[(M,)](dx, dy, _dw, _db, x, w, b, m, v, locks, x_arg.stride(0), N, ctx.eps, BLOCK_SIZE_N=ctx.BLOCK_SIZE, GROUP_SIZE_M=GROUP_SIZE_M, num_warps=ctx.num_warps) grid = lambda meta: [triton.cdiv(N, meta['BLOCK_SIZE_N'])] # accumulate partial sums in separate kernel _layer_norm_bwd_dwdb[grid](_dw, _db, dw, db, GROUP_SIZE_M, N, BLOCK_SIZE_M=32, BLOCK_SIZE_N=128) return dx, None, dw, db, None layer_norm = LayerNorm.apply def test_layer_norm(M, N, dtype, eps=1e-5, device='cuda'): # create data x_shape = (M, N) w_shape = (x_shape[-1], ) weight = torch.rand(w_shape, dtype=dtype, device='cuda', requires_grad=True) bias = torch.rand(w_shape, dtype=dtype, device='cuda', requires_grad=True) x = -2.3 + 0.5 * torch.randn(x_shape, dtype=dtype, device='cuda') dy = .1 * torch.randn_like(x) x.requires_grad_(True) # forward pass y_tri = layer_norm(x, w_shape, weight, bias, eps) y_ref = torch.nn.functional.layer_norm(x, w_shape, weight, bias, eps).to(dtype) # backward pass (triton) y_tri.backward(dy, retain_graph=True) dx_tri, dw_tri, db_tri = [_.grad.clone() for _ in [x, weight, bias]] x.grad, weight.grad, bias.grad = None, None, None # backward pass (torch) y_ref.backward(dy, retain_graph=True) dx_ref, dw_ref, db_ref = [_.grad.clone() for _ in [x, weight, bias]] # compare assert torch.allclose(y_tri, y_ref, atol=1e-2, rtol=0) assert torch.allclose(dx_tri, dx_ref, atol=1e-2, rtol=0) assert torch.allclose(db_tri, db_ref, atol=1e-2, rtol=0) assert torch.allclose(dw_tri, dw_ref, atol=1e-2, rtol=0) @triton.testing.perf_report( triton.testing.Benchmark( x_names=['N'], x_vals=[512 * i for i in range(2, 32)], line_arg='provider', line_vals=['triton', 'torch'] + (['apex'] if HAS_APEX else []), line_names=['Triton', 'Torch'] + (['Apex'] if HAS_APEX else []), styles=[('blue', '-'), ('green', '-'), ('orange', '-')], ylabel='GB/s', plot_name='layer-norm-backward', args={'M': 4096, 'dtype': torch.float16, 'mode': 'backward'} ) ) def bench_layer_norm(M, N, dtype, provider, mode='backward', eps=1e-5, device='cuda'): # create data x_shape = (M, N) w_shape = (x_shape[-1], ) weight = torch.rand(w_shape, dtype=dtype, device='cuda', requires_grad=True) bias = torch.rand(w_shape, dtype=dtype, device='cuda', requires_grad=True) x = -2.3 + 0.5 * torch.randn(x_shape, dtype=dtype, device='cuda') dy = .1 * torch.randn_like(x) x.requires_grad_(True) quantiles = [0.5, 0.2, 0.8] # utility functions if provider == 'triton': y_fwd = lambda: layer_norm(x, w_shape, weight, bias, eps) if provider == 'torch': y_fwd = lambda: torch.nn.functional.layer_norm(x, w_shape, weight, bias, eps) if provider == 'apex': apex_layer_norm = apex.normalization.FusedLayerNorm(w_shape).to(x.device).to(x.dtype) y_fwd = lambda: apex_layer_norm(x) # forward pass if mode == 'forward': gbps = lambda ms: 2 * x.numel() * x.element_size() / ms * 1e-6 ms, min_ms, max_ms = triton.testing.do_bench(y_fwd, quantiles=quantiles, rep=500) # backward pass if mode == 'backward': gbps = lambda ms: 3 * x.numel() * x.element_size() / ms * 1e-6 y = y_fwd() ms, min_ms, max_ms = triton.testing.do_bench(lambda: y.backward(dy, retain_graph=True), quantiles=quantiles, grad_to_none=[x], rep=500) return gbps(ms), gbps(max_ms), gbps(min_ms) test_layer_norm(1151, 8192, torch.float16) bench_layer_norm.run(save_path='.', print_data=True) # %% # References # ---------- # # .. [BA2016] Jimmy Lei Ba and Jamie Ryan Kiros and Geoffrey E. Hinton, "Layer Normalization", Arxiv 2016
15,483
40.290667
214
py
triton
triton-main/python/tutorials/02-fused-softmax.py
""" Fused Softmax ============= In this tutorial, you will write a fused softmax operation that is significantly faster than PyTorch's native op for a particular class of matrices: those whose rows can fit in the GPU's SRAM. In doing so, you will learn about: * The benefits of kernel fusion for bandwidth-bound operations. * Reduction operators in Triton. """ # %% # Motivations # ----------- # # Custom GPU kernels for elementwise additions are educationally valuable but won't get you very far in practice. # Let us consider instead the case of a simple (numerically stabilized) softmax operation: import torch import triton import triton.language as tl @torch.jit.script def naive_softmax(x): """Compute row-wise softmax of X using native pytorch We subtract the maximum element in order to avoid overflows. Softmax is invariant to this shift. """ # read MN elements ; write M elements x_max = x.max(dim=1)[0] # read MN + M elements ; write MN elements z = x - x_max[:, None] # read MN elements ; write MN elements numerator = torch.exp(z) # read MN elements ; write M elements denominator = numerator.sum(dim=1) # read MN + M elements ; write MN elements ret = numerator / denominator[:, None] # in total: read 5MN + 2M elements ; wrote 3MN + 2M elements return ret # %% # When implemented naively in PyTorch, computing :code:`y = naive_softmax(x)` for :math:`x \in R^{M \times N}` # requires reading :math:`5MN + 2M` elements from DRAM and writing back :math:`3MN + 2M` elements. # This is obviously wasteful; we'd prefer to have a custom "fused" kernel that only reads # X once and does all the necessary computations on-chip. # Doing so would require reading and writing back only :math:`MN` bytes, so we could # expect a theoretical speed-up of ~4x (i.e., :math:`(8MN + 4M) / 2MN`). # The `torch.jit.script` flags aims to perform this kind of "kernel fusion" automatically # but, as we will see later, it is still far from ideal. # %% # Compute Kernel # -------------- # # Our softmax kernel works as follows: each program loads a row of the input matrix X, # normalizes it and writes back the result to the output Y. # # Note that one important limitation of Triton is that each block must have a # power-of-two number of elements, so we need to internally "pad" each row and guard the # memory operations properly if we want to handle any possible input shapes: @triton.jit def softmax_kernel( output_ptr, input_ptr, input_row_stride, output_row_stride, n_cols, BLOCK_SIZE: tl.constexpr ): # The rows of the softmax are independent, so we parallelize across those row_idx = tl.program_id(0) # The stride represents how much we need to increase the pointer to advance 1 row row_start_ptr = input_ptr + row_idx * input_row_stride # The block size is the next power of two greater than n_cols, so we can fit each # row in a single block col_offsets = tl.arange(0, BLOCK_SIZE) input_ptrs = row_start_ptr + col_offsets # Load the row into SRAM, using a mask since BLOCK_SIZE may be > than n_cols row = tl.load(input_ptrs, mask=col_offsets < n_cols, other=-float('inf')) # Subtract maximum for numerical stability row_minus_max = row - tl.max(row, axis=0) # Note that exponentiation in Triton is fast but approximate (i.e., think __expf in CUDA) numerator = tl.exp(row_minus_max) denominator = tl.sum(numerator, axis=0) softmax_output = numerator / denominator # Write back output to DRAM output_row_start_ptr = output_ptr + row_idx * output_row_stride output_ptrs = output_row_start_ptr + col_offsets tl.store(output_ptrs, softmax_output, mask=col_offsets < n_cols) # %% # We can create a helper function that enqueues the kernel and its (meta-)arguments for any given input tensor. def softmax(x): n_rows, n_cols = x.shape # The block size is the smallest power of two greater than the number of columns in `x` BLOCK_SIZE = triton.next_power_of_2(n_cols) # Another trick we can use is to ask the compiler to use more threads per row by # increasing the number of warps (`num_warps`) over which each row is distributed. # You will see in the next tutorial how to auto-tune this value in a more natural # way so you don't have to come up with manual heuristics yourself. num_warps = 4 if BLOCK_SIZE >= 2048: num_warps = 8 if BLOCK_SIZE >= 4096: num_warps = 16 # Allocate output y = torch.empty_like(x) # Enqueue kernel. The 1D launch grid is simple: we have one kernel instance per row o # f the input matrix softmax_kernel[(n_rows,)]( y, x, x.stride(0), y.stride(0), n_cols, num_warps=num_warps, BLOCK_SIZE=BLOCK_SIZE, ) return y # %% # Unit Test # --------- # %% # We make sure that we test our kernel on a matrix with an irregular number of rows and columns. # This will allow us to verify that our padding mechanism works. torch.manual_seed(0) x = torch.randn(1823, 781, device='cuda') y_triton = softmax(x) y_torch = torch.softmax(x, axis=1) assert torch.allclose(y_triton, y_torch), (y_triton, y_torch) # %% # As expected, the results are identical. # %% # Benchmark # --------- # # Here we will benchmark our operation as a function of the number of columns in the input matrix -- assuming 4096 rows. # We will then compare its performance against (1) :code:`torch.softmax` and (2) the :code:`naive_softmax` defined above. @triton.testing.perf_report( triton.testing.Benchmark( x_names=['N'], # argument names to use as an x-axis for the plot x_vals=[ 128 * i for i in range(2, 100) ], # different possible values for `x_name` line_arg='provider', # argument name whose value corresponds to a different line in the plot line_vals=[ 'triton', 'torch-native', 'torch-jit', ], # possible values for `line_arg`` line_names=[ "Triton", "Torch (native)", "Torch (jit)", ], # label name for the lines styles=[('blue', '-'), ('green', '-'), ('green', '--')], # line styles ylabel="GB/s", # label name for the y-axis plot_name="softmax-performance", # name for the plot. Used also as a file name for saving the plot. args={'M': 4096}, # values for function arguments not in `x_names` and `y_name` ) ) def benchmark(M, N, provider): x = torch.randn(M, N, device='cuda', dtype=torch.float32) quantiles = [0.5, 0.2, 0.8] if provider == 'torch-native': ms, min_ms, max_ms = triton.testing.do_bench(lambda: torch.softmax(x, axis=-1), quantiles=quantiles) if provider == 'triton': ms, min_ms, max_ms = triton.testing.do_bench(lambda: softmax(x), quantiles=quantiles) if provider == 'torch-jit': ms, min_ms, max_ms = triton.testing.do_bench(lambda: naive_softmax(x), quantiles=quantiles) gbps = lambda ms: 2 * x.nelement() * x.element_size() * 1e-9 / (ms * 1e-3) return gbps(ms), gbps(max_ms), gbps(min_ms) benchmark.run(show_plots=True, print_data=True) # %% # In the above plot, we can see that: # - Triton is 4x faster than the Torch JIT. This confirms our suspicions that the Torch JIT does not do any fusion here. # - Triton is noticeably faster than :code:`torch.softmax` -- in addition to being **easier to read, understand and maintain**. # Note however that the PyTorch `softmax` operation is more general and will work on tensors of any shape.
7,632
36.975124
128
py
triton
triton-main/python/tutorials/06-fused-attention.py
""" Fused Attention =============== This is a Triton implementation of the Flash Attention v2 algorithm from Tri Dao (https://tridao.me/publications/flash2/flash2.pdf) Extra Credits: - Original flash attention paper (https://arxiv.org/abs/2205.14135) - Rabe and Staats (https://arxiv.org/pdf/2112.05682v2.pdf) - Adam P. Goucher for simplified vector math """ import pytest import torch import triton import triton.language as tl @triton.jit def max_fn(x, y): return tl.math.max(x, y) @triton.jit def _fwd_kernel( Q, K, V, sm_scale, L, M, Out, stride_qz, stride_qh, stride_qm, stride_qk, stride_kz, stride_kh, stride_kn, stride_kk, stride_vz, stride_vh, stride_vk, stride_vn, stride_oz, stride_oh, stride_om, stride_on, Z, H, N_CTX, BLOCK_M: tl.constexpr, BLOCK_DMODEL: tl.constexpr, BLOCK_N: tl.constexpr, MODE: tl.constexpr, ): start_m = tl.program_id(0) off_hz = tl.program_id(1) qvk_offset = off_hz * stride_qh Q_block_ptr = tl.make_block_ptr( base=Q + qvk_offset, shape=(N_CTX, BLOCK_DMODEL), strides=(stride_qm, stride_qk), offsets=(start_m * BLOCK_M, 0), block_shape=(BLOCK_M, BLOCK_DMODEL), order=(1, 0) ) K_block_ptr = tl.make_block_ptr( base=K + qvk_offset, shape=(BLOCK_DMODEL, N_CTX), strides=(stride_kk, stride_kn), offsets=(0, 0), block_shape=(BLOCK_DMODEL, BLOCK_N), order=(0, 1) ) V_block_ptr = tl.make_block_ptr( base=V + qvk_offset, shape=(N_CTX, BLOCK_DMODEL), strides=(stride_vk, stride_vn), offsets=(0, 0), block_shape=(BLOCK_N, BLOCK_DMODEL), order=(1, 0) ) O_block_ptr = tl.make_block_ptr( base=Out + qvk_offset, shape=(N_CTX, BLOCK_DMODEL), strides=(stride_om, stride_on), offsets=(start_m * BLOCK_M, 0), block_shape=(BLOCK_M, BLOCK_DMODEL), order=(1, 0) ) # initialize offsets offs_m = start_m * BLOCK_M + tl.arange(0, BLOCK_M) offs_n = tl.arange(0, BLOCK_N) # initialize pointer to m and l m_i = tl.zeros([BLOCK_M], dtype=tl.float32) - float("inf") l_i = tl.zeros([BLOCK_M], dtype=tl.float32) acc = tl.zeros([BLOCK_M, BLOCK_DMODEL], dtype=tl.float32) # causal check on every loop iteration can be expensive # and peeling the last iteration of the loop does not work well with ptxas # so we have a mode to do the causal check in a separate kernel entirely if MODE == 0: # entire non-causal attention lo, hi = 0, N_CTX if MODE == 1: # entire causal attention lo, hi = 0, (start_m + 1) * BLOCK_M if MODE == 2: # off band-diagonal lo, hi = 0, start_m * BLOCK_M if MODE == 3: # on band-diagonal l_ptrs = L + off_hz * N_CTX + offs_m m_ptrs = M + off_hz * N_CTX + offs_m m_i = tl.load(m_ptrs) l_i = tl.load(l_ptrs) acc += tl.load(O_block_ptr).to(tl.float32) lo, hi = start_m * BLOCK_M, (start_m + 1) * BLOCK_M # scale sm_scale by log_2(e) and use # 2^x instead of exp in the loop because CSE and LICM # don't work as expected with `exp` in the loop qk_scale = sm_scale * 1.44269504 # load q: it will stay in SRAM throughout q = tl.load(Q_block_ptr) q = (q * qk_scale).to(tl.float16) # advance block pointers to first iteration of the loop K_block_ptr = tl.advance(K_block_ptr, (0, lo)) V_block_ptr = tl.advance(V_block_ptr, (lo, 0)) # loop over k, v and update accumulator for start_n in range(lo, hi, BLOCK_N): start_n = tl.multiple_of(start_n, BLOCK_N) # -- compute qk ---- k = tl.load(K_block_ptr) qk = tl.zeros([BLOCK_M, BLOCK_N], dtype=tl.float32) qk += tl.dot(q, k) if MODE == 1 or MODE == 3: qk = tl.where(offs_m[:, None] >= (start_n + offs_n[None, :]), qk, float("-inf")) # -- compute m_ij, p, l_ij m_ij = tl.maximum(m_i, tl.max(qk, 1)) p = tl.math.exp2(qk - m_ij[:, None]) l_ij = tl.sum(p, 1) # -- update m_i and l_i alpha = tl.math.exp2(m_i - m_ij) l_i *= alpha l_i_new = l_i + l_ij # scale acc acc_scale = l_i * 0 + alpha acc = acc * acc_scale[:, None] # update acc v = tl.load(V_block_ptr) p = p.to(tl.float16) acc += tl.dot(p, v) # update m_i and l_i l_i = l_i_new m_i = m_ij # update pointers K_block_ptr = tl.advance(K_block_ptr, (0, BLOCK_N)) V_block_ptr = tl.advance(V_block_ptr, (BLOCK_N, 0)) # write back l and m acc = acc / l_i[:, None] l_ptrs = L + off_hz * N_CTX + offs_m m_ptrs = M + off_hz * N_CTX + offs_m tl.store(l_ptrs, l_i) tl.store(m_ptrs, m_i) # write back O tl.store(O_block_ptr, acc.to(tl.float16)) @triton.jit def _bwd_preprocess( Out, DO, L, NewDO, Delta, BLOCK_M: tl.constexpr, D_HEAD: tl.constexpr, ): off_m = tl.program_id(0) * BLOCK_M + tl.arange(0, BLOCK_M) off_n = tl.arange(0, D_HEAD) # load o = tl.load(Out + off_m[:, None] * D_HEAD + off_n[None, :]).to(tl.float32) do = tl.load(DO + off_m[:, None] * D_HEAD + off_n[None, :]).to(tl.float32) denom = tl.load(L + off_m).to(tl.float32) # compute do = do / denom[:, None] delta = tl.sum(o * do, axis=1) # write-back tl.store(NewDO + off_m[:, None] * D_HEAD + off_n[None, :], do) tl.store(Delta + off_m, delta) @triton.jit def _bwd_kernel( Q, K, V, sm_scale, Out, DO, DQ, DK, DV, L, M, D, stride_qz, stride_qh, stride_qm, stride_qk, stride_kz, stride_kh, stride_kn, stride_kk, stride_vz, stride_vh, stride_vk, stride_vn, Z, H, N_CTX, num_block, BLOCK_M: tl.constexpr, BLOCK_DMODEL: tl.constexpr, BLOCK_N: tl.constexpr, MODE: tl.constexpr, ): off_hz = tl.program_id(0) off_z = off_hz // H off_h = off_hz % H qk_scale = sm_scale * 1.44269504 # offset pointers for batch/head Q += off_z * stride_qz + off_h * stride_qh K += off_z * stride_qz + off_h * stride_qh V += off_z * stride_qz + off_h * stride_qh DO += off_z * stride_qz + off_h * stride_qh DQ += off_z * stride_qz + off_h * stride_qh DK += off_z * stride_qz + off_h * stride_qh DV += off_z * stride_qz + off_h * stride_qh for start_n in range(0, num_block): if MODE == 0: lo = 0 else: lo = start_n * BLOCK_M # initialize row/col offsets offs_qm = lo + tl.arange(0, BLOCK_M) offs_n = start_n * BLOCK_M + tl.arange(0, BLOCK_M) offs_m = tl.arange(0, BLOCK_N) offs_k = tl.arange(0, BLOCK_DMODEL) # initialize pointers to value-like data q_ptrs = Q + (offs_qm[:, None] * stride_qm + offs_k[None, :] * stride_qk) k_ptrs = K + (offs_n[:, None] * stride_kn + offs_k[None, :] * stride_kk) v_ptrs = V + (offs_n[:, None] * stride_qm + offs_k[None, :] * stride_qk) do_ptrs = DO + (offs_qm[:, None] * stride_qm + offs_k[None, :] * stride_qk) dq_ptrs = DQ + (offs_qm[:, None] * stride_qm + offs_k[None, :] * stride_qk) # pointer to row-wise quantities in value-like data D_ptrs = D + off_hz * N_CTX m_ptrs = M + off_hz * N_CTX # initialize dv amd dk dv = tl.zeros([BLOCK_M, BLOCK_DMODEL], dtype=tl.float32) dk = tl.zeros([BLOCK_M, BLOCK_DMODEL], dtype=tl.float32) # k and v stay in SRAM throughout k = tl.load(k_ptrs) v = tl.load(v_ptrs) # loop over rows for start_m in range(lo, num_block * BLOCK_M, BLOCK_M): offs_m_curr = start_m + offs_m # load q, k, v, do on-chip q = tl.load(q_ptrs) # recompute p = softmax(qk, dim=-1).T # NOTE: `do` is pre-divided by `l`; no normalization here # if MODE == 1: if MODE == 1: qk = tl.where(offs_m_curr[:, None] >= (offs_n[None, :]), float(0.), float("-inf")) else: qk = tl.zeros([BLOCK_M, BLOCK_N], dtype=tl.float32) qk += tl.dot(q, tl.trans(k)) qk *= qk_scale m = tl.load(m_ptrs + offs_m_curr) p = tl.math.exp2(qk - m[:, None]) # compute dv do = tl.load(do_ptrs) dv += tl.dot(tl.trans(p.to(Q.dtype.element_ty)), do) # compute dp = dot(v, do) Di = tl.load(D_ptrs + offs_m_curr) dp = tl.zeros([BLOCK_M, BLOCK_N], dtype=tl.float32) - Di[:, None] dp += tl.dot(do, tl.trans(v)) # compute ds = p * (dp - delta[:, None]) ds = p * dp * sm_scale # compute dk = dot(ds.T, q) dk += tl.dot(tl.trans(ds.to(Q.dtype.element_ty)), q) # compute dq dq = tl.load(dq_ptrs) dq += tl.dot(ds.to(Q.dtype.element_ty), k) tl.store(dq_ptrs, dq) # increment pointers dq_ptrs += BLOCK_M * stride_qm q_ptrs += BLOCK_M * stride_qm do_ptrs += BLOCK_M * stride_qm # write-back dv_ptrs = DV + (offs_n[:, None] * stride_qm + offs_k[None, :] * stride_qk) dk_ptrs = DK + (offs_n[:, None] * stride_kn + offs_k[None, :] * stride_kk) tl.store(dv_ptrs, dv) tl.store(dk_ptrs, dk) empty = torch.empty(128, device="cuda") class _attention(torch.autograd.Function): @staticmethod def forward(ctx, q, k, v, causal, sm_scale): # shape constraints Lq, Lk, Lv = q.shape[-1], k.shape[-1], v.shape[-1] assert Lq == Lk and Lk == Lv assert Lk in {16, 32, 64, 128} o = torch.empty_like(q) BLOCK_M = 128 BLOCK_N = 64 grid = (triton.cdiv(q.shape[2], BLOCK_M), q.shape[0] * q.shape[1], 1) L = torch.empty((q.shape[0] * q.shape[1], q.shape[2]), device=q.device, dtype=torch.float32) m = torch.empty((q.shape[0] * q.shape[1], q.shape[2]), device=q.device, dtype=torch.float32) num_warps = 4 if Lk <= 64 else 8 if causal: modes = [1] if q.shape[2] <= 2048 else [2, 3] else: modes = [0] for mode in modes: _fwd_kernel[grid]( q, k, v, sm_scale, L, m, o, q.stride(0), q.stride(1), q.stride(2), q.stride(3), k.stride(0), k.stride(1), k.stride(2), k.stride(3), v.stride(0), v.stride(1), v.stride(2), v.stride(3), o.stride(0), o.stride(1), o.stride(2), o.stride(3), q.shape[0], q.shape[1], q.shape[2], BLOCK_M=BLOCK_M, BLOCK_N=BLOCK_N, BLOCK_DMODEL=Lk, MODE=mode, num_warps=num_warps, num_stages=4) ctx.save_for_backward(q, k, v, o, L, m) ctx.grid = grid ctx.sm_scale = sm_scale ctx.BLOCK_DMODEL = Lk ctx.causal = causal return o @staticmethod def backward(ctx, do): BLOCK = 128 q, k, v, o, l, m = ctx.saved_tensors do = do.contiguous() dq = torch.zeros_like(q, dtype=torch.float32) dk = torch.empty_like(k) dv = torch.empty_like(v) do_scaled = torch.empty_like(do) delta = torch.empty_like(l) if ctx.causal: mode = 1 else: mode = 0 _bwd_preprocess[(ctx.grid[0] * ctx.grid[1], )]( o, do, l, do_scaled, delta, BLOCK_M=BLOCK, D_HEAD=ctx.BLOCK_DMODEL, ) _bwd_kernel[(ctx.grid[1],)]( q, k, v, ctx.sm_scale, o, do_scaled, dq, dk, dv, l, m, delta, q.stride(0), q.stride(1), q.stride(2), q.stride(3), k.stride(0), k.stride(1), k.stride(2), k.stride(3), v.stride(0), v.stride(1), v.stride(2), v.stride(3), q.shape[0], q.shape[1], q.shape[2], ctx.grid[0], BLOCK_M=BLOCK, BLOCK_N=BLOCK, BLOCK_DMODEL=ctx.BLOCK_DMODEL, num_warps=8, MODE=mode, num_stages=1, ) return dq, dk, dv, None, None attention = _attention.apply @pytest.mark.parametrize('Z, H, N_CTX, D_HEAD', [(6, 9, 1024, 64)]) @pytest.mark.parametrize('causal', [False, True]) def test_op(Z, H, N_CTX, D_HEAD, causal, dtype=torch.float16): torch.manual_seed(20) q = torch.empty((Z, H, N_CTX, D_HEAD), dtype=dtype, device="cuda").normal_(mean=0., std=0.5).requires_grad_() k = torch.empty((Z, H, N_CTX, D_HEAD), dtype=dtype, device="cuda").normal_(mean=0., std=0.5).requires_grad_() v = torch.empty((Z, H, N_CTX, D_HEAD), dtype=dtype, device="cuda").normal_(mean=0., std=0.5).requires_grad_() sm_scale = 0.5 dout = torch.randn_like(q) # reference implementation M = torch.tril(torch.ones((N_CTX, N_CTX), device="cuda")) p = torch.matmul(q, k.transpose(2, 3)) * sm_scale if causal: p[:, :, M == 0] = float("-inf") p = torch.softmax(p.float(), dim=-1).half() # p = torch.exp(p) ref_out = torch.matmul(p, v) ref_out.backward(dout) ref_dv, v.grad = v.grad.clone(), None ref_dk, k.grad = k.grad.clone(), None ref_dq, q.grad = q.grad.clone(), None # triton implementation tri_out = attention(q, k, v, causal, sm_scale).half() tri_out.backward(dout) tri_dv, v.grad = v.grad.clone(), None tri_dk, k.grad = k.grad.clone(), None tri_dq, q.grad = q.grad.clone(), None # compare assert torch.allclose(ref_out, tri_out, atol=1e-2, rtol=0) assert torch.allclose(ref_dv, tri_dv, atol=1e-2, rtol=0) assert torch.allclose(ref_dk, tri_dk, atol=1e-2, rtol=0) assert torch.allclose(ref_dq, tri_dq, atol=1e-2, rtol=0) try: from flash_attn.flash_attn_interface import flash_attn_func HAS_FLASH = True except BaseException: HAS_FLASH = False BATCH, N_HEADS, N_CTX, D_HEAD = 4, 48, 4096, 64 # vary seq length for fixed head and batch=4 configs = [triton.testing.Benchmark( x_names=['N_CTX'], x_vals=[2**i for i in range(10, 15)], line_arg='provider', line_vals=['triton'] + (['flash'] if HAS_FLASH else []), line_names=['Triton'] + (['Flash'] if HAS_FLASH else []), styles=[('red', '-'), ('blue', '-')], ylabel='ms', plot_name=f'fused-attention-batch{BATCH}-head{N_HEADS}-d{D_HEAD}-{mode}', args={'H': N_HEADS, 'BATCH': BATCH, 'D_HEAD': D_HEAD, 'dtype': torch.float16, 'mode': mode, 'causal': causal} ) for mode in ['fwd'] for causal in [False]] @triton.testing.perf_report(configs) def bench_flash_attention(BATCH, H, N_CTX, D_HEAD, causal, mode, provider, dtype=torch.float16, device="cuda"): assert mode in ['fwd', 'bwd'] warmup = 25 rep = 100 if provider == "triton": q = torch.randn((BATCH, H, N_CTX, D_HEAD), dtype=dtype, device="cuda", requires_grad=True) k = torch.randn((BATCH, H, N_CTX, D_HEAD), dtype=dtype, device="cuda", requires_grad=True) v = torch.randn((BATCH, H, N_CTX, D_HEAD), dtype=dtype, device="cuda", requires_grad=True) sm_scale = 1.3 fn = lambda: attention(q, k, v, causal, sm_scale) if mode == 'bwd': o = fn() do = torch.randn_like(o) fn = lambda: o.backward(do, retain_graph=True) ms = triton.testing.do_bench(fn, warmup=warmup, rep=rep) if provider == "flash": lengths = torch.full((BATCH,), fill_value=N_CTX, device=device) cu_seqlens = torch.zeros((BATCH + 1,), device=device, dtype=torch.int32) cu_seqlens[1:] = lengths.cumsum(0) qkv = torch.randn((BATCH * N_CTX, 3, H, D_HEAD), dtype=dtype, device=device, requires_grad=True) fn = lambda: flash_attn_func(qkv, cu_seqlens, 0., N_CTX, causal=causal) if mode == 'bwd': o = fn() do = torch.randn_like(o) fn = lambda: o.backward(do, retain_graph=True) ms = triton.testing.do_bench(fn, warmup=warmup, rep=rep) flops_per_matmul = 2. * BATCH * H * N_CTX * N_CTX * D_HEAD total_flops = 2 * flops_per_matmul if causal: total_flops *= 0.5 if mode == 'bwd': total_flops *= 2.5 # 2.0(bwd) + 0.5(recompute) return total_flops / ms * 1e-9 # only works on post-Ampere GPUs right now bench_flash_attention.run(save_path='.', print_data=True)
16,390
36.1678
131
py
triton
triton-main/python/tutorials/01-vector-add.py
""" Vector Addition =============== In this tutorial, you will write a simple vector addition using Triton. In doing so, you will learn about: * The basic programming model of Triton. * The `triton.jit` decorator, which is used to define Triton kernels. * The best practices for validating and benchmarking your custom ops against native reference implementations. """ # %% # Compute Kernel # -------------- import torch import triton import triton.language as tl @triton.jit def add_kernel( x_ptr, # *Pointer* to first input vector. y_ptr, # *Pointer* to second input vector. output_ptr, # *Pointer* to output vector. n_elements, # Size of the vector. BLOCK_SIZE: tl.constexpr, # Number of elements each program should process. # NOTE: `constexpr` so it can be used as a shape value. ): # There are multiple 'programs' processing different data. We identify which program # we are here: pid = tl.program_id(axis=0) # We use a 1D launch grid so axis is 0. # This program will process inputs that are offset from the initial data. # For instance, if you had a vector of length 256 and block_size of 64, the programs # would each access the elements [0:64, 64:128, 128:192, 192:256]. # Note that offsets is a list of pointers: block_start = pid * BLOCK_SIZE offsets = block_start + tl.arange(0, BLOCK_SIZE) # Create a mask to guard memory operations against out-of-bounds accesses. mask = offsets < n_elements # Load x and y from DRAM, masking out any extra elements in case the input is not a # multiple of the block size. x = tl.load(x_ptr + offsets, mask=mask) y = tl.load(y_ptr + offsets, mask=mask) output = x + y # Write x + y back to DRAM. tl.store(output_ptr + offsets, output, mask=mask) # %% # Let's also declare a helper function to (1) allocate the `z` tensor # and (2) enqueue the above kernel with appropriate grid/block sizes: def add(x: torch.Tensor, y: torch.Tensor): # We need to preallocate the output. output = torch.empty_like(x) assert x.is_cuda and y.is_cuda and output.is_cuda n_elements = output.numel() # The SPMD launch grid denotes the number of kernel instances that run in parallel. # It is analogous to CUDA launch grids. It can be either Tuple[int], or Callable(metaparameters) -> Tuple[int]. # In this case, we use a 1D grid where the size is the number of blocks: grid = lambda meta: (triton.cdiv(n_elements, meta['BLOCK_SIZE']),) # NOTE: # - Each torch.tensor object is implicitly converted into a pointer to its first element. # - `triton.jit`'ed functions can be indexed with a launch grid to obtain a callable GPU kernel. # - Don't forget to pass meta-parameters as keywords arguments. add_kernel[grid](x, y, output, n_elements, BLOCK_SIZE=1024) # We return a handle to z but, since `torch.cuda.synchronize()` hasn't been called, the kernel is still # running asynchronously at this point. return output # %% # We can now use the above function to compute the element-wise sum of two `torch.tensor` objects and test its correctness: torch.manual_seed(0) size = 98432 x = torch.rand(size, device='cuda') y = torch.rand(size, device='cuda') output_torch = x + y output_triton = add(x, y) print(output_torch) print(output_triton) print( f'The maximum difference between torch and triton is ' f'{torch.max(torch.abs(output_torch - output_triton))}' ) # %% # Seems like we're good to go! # %% # Benchmark # --------- # # We can now benchmark our custom op on vectors of increasing sizes to get a sense of how it does relative to PyTorch. # To make things easier, Triton has a set of built-in utilities that allow us to concisely plot the performance of our custom ops. # for different problem sizes. @triton.testing.perf_report( triton.testing.Benchmark( x_names=['size'], # Argument names to use as an x-axis for the plot. x_vals=[ 2 ** i for i in range(12, 28, 1) ], # Different possible values for `x_name`. x_log=True, # x axis is logarithmic. line_arg='provider', # Argument name whose value corresponds to a different line in the plot. line_vals=['triton', 'torch'], # Possible values for `line_arg`. line_names=['Triton', 'Torch'], # Label name for the lines. styles=[('blue', '-'), ('green', '-')], # Line styles. ylabel='GB/s', # Label name for the y-axis. plot_name='vector-add-performance', # Name for the plot. Used also as a file name for saving the plot. args={}, # Values for function arguments not in `x_names` and `y_name`. ) ) def benchmark(size, provider): x = torch.rand(size, device='cuda', dtype=torch.float32) y = torch.rand(size, device='cuda', dtype=torch.float32) quantiles = [0.5, 0.2, 0.8] if provider == 'torch': ms, min_ms, max_ms = triton.testing.do_bench(lambda: x + y, quantiles=quantiles) if provider == 'triton': ms, min_ms, max_ms = triton.testing.do_bench(lambda: add(x, y), quantiles=quantiles) gbps = lambda ms: 12 * size / ms * 1e-6 return gbps(ms), gbps(max_ms), gbps(min_ms) # %% # We can now run the decorated function above. Pass `print_data=True` to see the performance number, `show_plots=True` to plot them, and/or # `save_path='/path/to/results/' to save them to disk along with raw CSV data: benchmark.run(print_data=True, show_plots=True)
5,496
38.264286
139
py
triton
triton-main/python/tutorials/08-experimental-block-pointer.py
""" Block Pointer (Experimental) ============================ This tutorial will guide you through writing a matrix multiplication algorithm that utilizes block pointer semantics. These semantics are more friendly for Triton to optimize and can result in better performance on specific hardware. Note that this feature is still experimental and may change in the future. """ # %% # Motivations # ----------- # In the previous matrix multiplication tutorial, we constructed blocks of values by de-referencing blocks of pointers, # i.e., :code:`load(block<pointer_type<element_type>>) -> block<element_type>`, which involved loading blocks of # elements from memory. This approach allowed for flexibility in using hardware-managed cache and implementing complex # data structures, such as tensors of trees or unstructured look-up tables. # # However, the drawback of this approach is that it relies heavily on complex optimization passes by the compiler to # optimize memory access patterns. This can result in brittle code that may suffer from performance degradation when the # optimizer fails to perform adequately. Additionally, as memory controllers specialize to accommodate dense spatial # data structures commonly used in machine learning workloads, this problem is likely to worsen. # # To address this issue, we will use block pointers :code:`pointer_type<block<element_type>>` and load them into # :code:`block<element_type>`, in which way gives better friendliness for the compiler to optimize memory access # patterns. # # Let's start with the previous matrix multiplication example and demonstrate how to rewrite it to utilize block pointer # semantics. # %% # Make a Block Pointer # -------------------- # A block pointer pointers to a block in a parent tensor and is constructed by :code:`make_block_ptr` function, # which takes the following information as arguments: # # * :code:`base`: the base pointer to the parent tensor; # # * :code:`shape`: the shape of the parent tensor; # # * :code:`strides`: the strides of the parent tensor, which means how much to increase the pointer by when moving by 1 element in a specific axis; # # * :code:`offsets`: the offsets of the block; # # * :code:`block_shape`: the shape of the block; # # * :code:`order`: the order of the block, which means how the block is laid out in memory. # # For example, to a block pointer to a :code:`BLOCK_SIZE_M * BLOCK_SIZE_K` block in a row-major 2D matrix A by # offsets :code:`(pid_m * BLOCK_SIZE_M, 0)` and strides :code:`(stride_am, stride_ak)`, we can use the following code # (exactly the same as the previous matrix multiplication tutorial): # # .. code-block:: python # # a_block_ptr = tl.make_block_ptr(base=a_ptr, shape=(M, K), strides=(stride_am, stride_ak), # offsets=(pid_m * BLOCK_SIZE_M, 0), block_shape=(BLOCK_SIZE_M, BLOCK_SIZE_K), # order=(1, 0)) # # Note that the :code:`order` argument is set to :code:`(1, 0)`, which means the second axis is the inner dimension in # terms of storage, and the first axis is the outer dimension. This information may sound redundant, but it is necessary # for some hardware backends to optimize for better performance. # %% # Load/Store a Block Pointer # -------------------------- # To load/store a block pointer, we can use :code:`load/store` function, which takes a block pointer as an argument, # de-references it, and loads/stores a block. You may mask some values in the block, here we have an extra argument # :code:`boundary_check` to specify whether to check the boundary of each axis for the block pointer. With check on, # out-of-bound values will be masked according to the :code:`padding_option` argument (load only), which can be # :code:`zero` or :code:`nan`. Temporarily, we do not support other values due to some hardware limitations. In this # mode of block pointer load/store does not support :code:`mask` or :code:`other` arguments in the legacy mode. # # So to load the block pointer of A in the previous section, we can simply write # :code:`a = tl.load(a_block_ptr, boundary_check=(0, 1))`. Boundary check may cost extra performance, so if you can # guarantee that the block pointer is always in-bound in some axis, you can turn off the check by not passing the index # into the :code:`boundary_check` argument. For example, if we know that :code:`M` is a multiple of # :code:`BLOCK_SIZE_M`, we can replace with :code:`a = tl.load(a_block_ptr, boundary_check=(1, ))`, since axis 0 is # always in bound. # %% # Advance a Block Pointer # ----------------------- # To advance a block pointer, we can use :code:`advance` function, which takes a block pointer and the increment for # each axis as arguments and returns a new block pointer with the same shape and strides as the original one, # but with the offsets advanced by the specified amount. # # For example, to advance the block pointer by :code:`BLOCK_SIZE_K` in the second axis # (no need to multiply with strides), we can write :code:`a_block_ptr = tl.advance(a_block_ptr, (0, BLOCK_SIZE_K))`. # %% # Final Result # ------------ import torch import triton import triton.language as tl @triton.autotune( configs=[ triton.Config({'BLOCK_SIZE_M': 128, 'BLOCK_SIZE_N': 256, 'BLOCK_SIZE_K': 64, 'GROUP_SIZE_M': 8}, num_stages=4, num_warps=4), triton.Config({'BLOCK_SIZE_M': 64, 'BLOCK_SIZE_N': 256, 'BLOCK_SIZE_K': 32, 'GROUP_SIZE_M': 8}, num_stages=4, num_warps=4), triton.Config({'BLOCK_SIZE_M': 128, 'BLOCK_SIZE_N': 128, 'BLOCK_SIZE_K': 32, 'GROUP_SIZE_M': 8}, num_stages=4, num_warps=4), triton.Config({'BLOCK_SIZE_M': 128, 'BLOCK_SIZE_N': 64, 'BLOCK_SIZE_K': 32, 'GROUP_SIZE_M': 8}, num_stages=4, num_warps=4), triton.Config({'BLOCK_SIZE_M': 64, 'BLOCK_SIZE_N': 128, 'BLOCK_SIZE_K': 32, 'GROUP_SIZE_M': 8}, num_stages=4, num_warps=4), triton.Config({'BLOCK_SIZE_M': 128, 'BLOCK_SIZE_N': 32, 'BLOCK_SIZE_K': 32, 'GROUP_SIZE_M': 8}, num_stages=4, num_warps=4), triton.Config({'BLOCK_SIZE_M': 64, 'BLOCK_SIZE_N': 32, 'BLOCK_SIZE_K': 32, 'GROUP_SIZE_M': 8}, num_stages=5, num_warps=2), triton.Config({'BLOCK_SIZE_M': 32, 'BLOCK_SIZE_N': 64, 'BLOCK_SIZE_K': 32, 'GROUP_SIZE_M': 8}, num_stages=5, num_warps=2), ], key=['M', 'N', 'K'], ) @triton.jit def matmul_kernel_with_block_pointers( # Pointers to matrices a_ptr, b_ptr, c_ptr, # Matrix dimensions M, N, K, # The stride variables represent how much to increase the ptr by when moving by 1 # element in a particular dimension. E.g. `stride_am` is how much to increase `a_ptr` # by to get the element one row down (A has M rows). stride_am, stride_ak, stride_bk, stride_bn, stride_cm, stride_cn, # Meta-parameters BLOCK_SIZE_M: tl.constexpr, BLOCK_SIZE_N: tl.constexpr, BLOCK_SIZE_K: tl.constexpr, GROUP_SIZE_M: tl.constexpr ): """Kernel for computing the matmul C = A x B. A has shape (M, K), B has shape (K, N) and C has shape (M, N) """ # ----------------------------------------------------------- # Map program ids `pid` to the block of C it should compute. # This is done in a grouped ordering to promote L2 data reuse. # See the matrix multiplication tutorial for details. pid = tl.program_id(axis=0) num_pid_m = tl.cdiv(M, BLOCK_SIZE_M) num_pid_n = tl.cdiv(N, BLOCK_SIZE_N) num_pid_in_group = GROUP_SIZE_M * num_pid_n group_id = pid // num_pid_in_group first_pid_m = group_id * GROUP_SIZE_M group_size_m = min(num_pid_m - first_pid_m, GROUP_SIZE_M) pid_m = first_pid_m + (pid % group_size_m) pid_n = (pid % num_pid_in_group) // group_size_m # ---------------------------------------------------------- # Create block pointers for the first blocks of A and B. # We will advance this pointer as we move in the K direction and accumulate. # See above `Make a Block Pointer` section for details. a_block_ptr = tl.make_block_ptr(base=a_ptr, shape=(M, K), strides=(stride_am, stride_ak), offsets=(pid_m * BLOCK_SIZE_M, 0), block_shape=(BLOCK_SIZE_M, BLOCK_SIZE_K), order=(1, 0)) b_block_ptr = tl.make_block_ptr(base=b_ptr, shape=(K, N), strides=(stride_bk, stride_bn), offsets=(0, pid_n * BLOCK_SIZE_N), block_shape=(BLOCK_SIZE_K, BLOCK_SIZE_N), order=(1, 0)) # ----------------------------------------------------------- # Iterate to compute a block of the C matrix. # We accumulate into a `[BLOCK_SIZE_M, BLOCK_SIZE_N]` block. # of fp32 values for higher accuracy. # `accumulator` will be converted back to fp16 after the loop. accumulator = tl.zeros((BLOCK_SIZE_M, BLOCK_SIZE_N), dtype=tl.float32) for k in range(0, K, BLOCK_SIZE_K): # Load with boundary checks, no need to calculate the mask manually. # For better performance, you may remove some axis from the boundary # check, if you can guarantee that the access is always in-bound in # that axis. # See above `Load/Store a Block Pointer` section for details. a = tl.load(a_block_ptr, boundary_check=(0, 1)) b = tl.load(b_block_ptr, boundary_check=(0, 1)) # We accumulate along the K dimension. accumulator += tl.dot(a, b) # Advance the block pointer to the next K block. # See above `Advance a Block Pointer` section for details. a_block_ptr = tl.advance(a_block_ptr, (0, BLOCK_SIZE_K)) b_block_ptr = tl.advance(b_block_ptr, (BLOCK_SIZE_K, 0)) c = accumulator.to(tl.float16) # ---------------------------------------------------------------- # Write back the block of the output matrix C with boundary checks. # See above `Load/Store a Block Pointer` section for details. c_block_ptr = tl.make_block_ptr(base=c_ptr, shape=(M, N), strides=(stride_cm, stride_cn), offsets=(pid_m * BLOCK_SIZE_M, pid_n * BLOCK_SIZE_N), block_shape=(BLOCK_SIZE_M, BLOCK_SIZE_N), order=(1, 0)) tl.store(c_block_ptr, c, boundary_check=(0, 1)) # We can now create a convenience wrapper function that only takes two input tensors, # and (1) checks any shape constraint; (2) allocates the output; (3) launches the above kernel. def matmul(a, b): # Check constraints. assert a.shape[1] == b.shape[0], "Incompatible dimensions" assert a.is_contiguous(), "Matrix A must be contiguous" assert b.is_contiguous(), "Matrix B must be contiguous" M, K = a.shape K, N = b.shape # Allocates output. c = torch.empty((M, N), device=a.device, dtype=a.dtype) # 1D launch kernel where each block gets its own program. grid = lambda META: ( triton.cdiv(M, META['BLOCK_SIZE_M']) * triton.cdiv(N, META['BLOCK_SIZE_N']), ) matmul_kernel_with_block_pointers[grid]( a, b, c, M, N, K, a.stride(0), a.stride(1), b.stride(0), b.stride(1), c.stride(0), c.stride(1), ) return c # %% # Unit Test # --------- # # Still we can test our matrix multiplication with block pointers against a native torch implementation (i.e., cuBLAS). torch.manual_seed(0) a = torch.randn((512, 512), device='cuda', dtype=torch.float16) b = torch.randn((512, 512), device='cuda', dtype=torch.float16) triton_output = matmul(a, b) torch_output = torch.matmul(a, b) print(f"triton_output={triton_output}") print(f"torch_output={torch_output}") if torch.allclose(triton_output, torch_output, atol=1e-2, rtol=0): print("✅ Triton and Torch match") else: print("❌ Triton and Torch differ")
11,784
50.462882
147
py
triton
triton-main/python/tutorials/07-math-functions.py
""" Libdevice (`tl.math`) function ============================== Triton can invoke a custom function from an external library. In this example, we will use the `libdevice` library (a.k.a `math` in triton) to apply `asin` on a tensor. Please refer to https://docs.nvidia.com/cuda/libdevice-users-guide/index.html regarding the semantics of all available libdevice functions. In `triton/language/math.py`, we try to aggregate functions with the same computation but different data types together. For example, both `__nv_asin` and `__nvasinf` calculate the principal value of the arc sine of the input, but `__nv_asin` operates on `double` and `__nv_asinf` operates on `float`. Using triton, you can simply call `tl.math.asin`. Triton automatically selects the correct underlying device function to invoke based on input and output types. """ # %% # asin Kernel # ------------ import torch import triton import triton.language as tl @triton.jit def asin_kernel( x_ptr, y_ptr, n_elements, BLOCK_SIZE: tl.constexpr, ): pid = tl.program_id(axis=0) block_start = pid * BLOCK_SIZE offsets = block_start + tl.arange(0, BLOCK_SIZE) mask = offsets < n_elements x = tl.load(x_ptr + offsets, mask=mask) x = tl.math.asin(x) tl.store(y_ptr + offsets, x, mask=mask) # %% # Using the default libdevice library path # ----------------------------------------- # We can use the default libdevice library path encoded in `triton/language/math.py` torch.manual_seed(0) size = 98432 x = torch.rand(size, device='cuda') output_triton = torch.zeros(size, device='cuda') output_torch = torch.asin(x) assert x.is_cuda and output_triton.is_cuda n_elements = output_torch.numel() grid = lambda meta: (triton.cdiv(n_elements, meta['BLOCK_SIZE']),) asin_kernel[grid](x, output_triton, n_elements, BLOCK_SIZE=1024) print(output_torch) print(output_triton) print( f'The maximum difference between torch and triton is ' f'{torch.max(torch.abs(output_torch - output_triton))}' ) # %% # Customize the libdevice library path # ------------------------------------- # We can also customize the libdevice library path by passing the path to the `libdevice` library to the `asin` kernel. output_triton = torch.empty_like(x) asin_kernel[grid](x, output_triton, n_elements, BLOCK_SIZE=1024, extern_libs={'libdevice': '/usr/local/cuda/nvvm/libdevice/libdevice.10.bc'}) print(output_torch) print(output_triton) print( f'The maximum difference between torch and triton is ' f'{torch.max(torch.abs(output_torch - output_triton))}' )
2,597
34.108108
180
py
triton
triton-main/python/tutorials/04-low-memory-dropout.py
""" Low-Memory Dropout ================== In this tutorial, you will write a memory-efficient implementation of dropout whose state will be composed of a single int32 seed. This differs from more traditional implementations of dropout, whose state is generally composed of a bit mask tensor of the same shape as the input. In doing so, you will learn about: * The limitations of naive implementations of Dropout with PyTorch. * Parallel pseudo-random number generation in Triton. """ # %% # Baseline # -------- # # The *dropout* operator was first introduced in [SRIVASTAVA2014]_ as a way to improve the performance # of deep neural networks in low-data regime (i.e. regularization). # # It takes a vector as input and produces a vector of the same shape as output. Each scalar in the # output has a probability :math:`p` of being changed to zero and otherwise it is copied from the input. # This forces the network to perform well even when only :math:`1 - p` scalars from the input are available. # # At evaluation time we want to use the full power of the network so we set :math:`p=0`. Naively this would # increase the norm of the output (which can be a bad thing, e.g. it can lead to artificial decrease # in the output softmax temperature). To prevent this we multiply the output by :math:`\frac{1}{1 - p}`, which # keeps the norm consistent regardless of the dropout probability. # # Let's first take a look at the baseline implementation. import tabulate import torch import triton import triton.language as tl @triton.jit def _dropout( x_ptr, # pointer to the input x_keep_ptr, # pointer to a mask of 0s and 1s output_ptr, # pointer to the output n_elements, # number of elements in the `x` tensor p, # probability that an element of `x` is changed to zero BLOCK_SIZE: tl.constexpr, ): pid = tl.program_id(axis=0) block_start = pid * BLOCK_SIZE offsets = block_start + tl.arange(0, BLOCK_SIZE) mask = offsets < n_elements # Load data x = tl.load(x_ptr + offsets, mask=mask) x_keep = tl.load(x_keep_ptr + offsets, mask=mask) # The line below is the crucial part, described in the paragraph above! output = tl.where(x_keep, x / (1 - p), 0.0) # Write-back output tl.store(output_ptr + offsets, output, mask=mask) def dropout(x, x_keep, p): output = torch.empty_like(x) assert x.is_contiguous() n_elements = x.numel() grid = lambda meta: (triton.cdiv(n_elements, meta['BLOCK_SIZE']),) _dropout[grid](x, x_keep, output, n_elements, p, BLOCK_SIZE=1024) return output # Input tensor x = torch.randn(size=(10,)).cuda() # Dropout mask p = 0.5 x_keep = (torch.rand(size=(10,)) > p).to(torch.int32).cuda() # output = dropout(x, x_keep=x_keep, p=p) print(tabulate.tabulate([ ["input"] + x.tolist(), ["keep mask"] + x_keep.tolist(), ["output"] + output.tolist() ])) # %% # Seeded dropout # -------------- # # The above implementation of dropout works fine, but it can be a bit awkward to deal with. Firstly # we need to store the dropout mask for backpropagation. Secondly, dropout state management can get # very tricky when using recompute/checkpointing (e.g. see all the notes about `preserve_rng_state` in # https://pytorch.org/docs/1.9.0/checkpoint.html). In this tutorial we'll describe an alternative implementation # that (1) has a smaller memory footprint; (2) requires less data movement; and (3) simplifies the management # of persisting randomness across multiple invocations of the kernel. # # Pseudo-random number generation in Triton is simple! In this tutorial we will use the # :code:`triton.language.rand` function which generates a block of uniformly distributed :code:`float32` # values in [0, 1), given a seed and a block of :code:`int32` offsets. But if you need it, Triton also provides # other :ref:`random number generation strategies <Random Number Generation>`. # # .. note:: # Triton's implementation of PRNG is based on the Philox algorithm (described on [SALMON2011]_). # # Let's put it all together. @triton.jit def _seeded_dropout( x_ptr, output_ptr, n_elements, p, seed, BLOCK_SIZE: tl.constexpr, ): # compute memory offsets of elements handled by this instance pid = tl.program_id(axis=0) block_start = pid * BLOCK_SIZE offsets = block_start + tl.arange(0, BLOCK_SIZE) # load data from x mask = offsets < n_elements x = tl.load(x_ptr + offsets, mask=mask) # randomly prune it random = tl.rand(seed, offsets) x_keep = random > p # write-back output = tl.where(x_keep, x / (1 - p), 0.0) tl.store(output_ptr + offsets, output, mask=mask) def seeded_dropout(x, p, seed): output = torch.empty_like(x) assert x.is_contiguous() n_elements = x.numel() grid = lambda meta: (triton.cdiv(n_elements, meta['BLOCK_SIZE']),) _seeded_dropout[grid](x, output, n_elements, p, seed, BLOCK_SIZE=1024) return output x = torch.randn(size=(10,)).cuda() # Compare this to the baseline - dropout mask is never instantiated! output = seeded_dropout(x, p=0.5, seed=123) output2 = seeded_dropout(x, p=0.5, seed=123) output3 = seeded_dropout(x, p=0.5, seed=512) print(tabulate.tabulate([ ["input"] + x.tolist(), ["output (seed = 123)"] + output.tolist(), ["output (seed = 123)"] + output2.tolist(), ["output (seed = 512)"] + output3.tolist() ])) # %% # Et Voilà! We have a triton kernel that applies the same dropout mask provided the seed is the same! # If you'd like explore further applications of pseudorandomness in GPU programming, we encourage you # to explore the `triton/language/random` folder! # %% # Exercises # --------- # # 1. Extend the kernel to operate over a matrix and use a vector of seeds - one per row. # 2. Add support for striding. # 3. (challenge) Implement a kernel for sparse Johnson-Lindenstrauss transform which generates the projection matrix one the fly each time using a seed. # %% # References # ---------- # # .. [SALMON2011] John K. Salmon, Mark A. Moraes, Ron O. Dror, and David E. Shaw, "Parallel Random Numbers: As Easy as 1, 2, 3", 2011 # .. [SRIVASTAVA2014] Nitish Srivastava and Geoffrey Hinton and Alex Krizhevsky and Ilya Sutskever and Ruslan Salakhutdinov, "Dropout: A Simple Way to Prevent Neural Networks from Overfitting", JMLR 2014
6,337
35.425287
203
py
triton
triton-main/python/tutorials/03-matrix-multiplication.py
""" Matrix Multiplication ===================== In this tutorial, you will write a very short high-performance FP16 matrix multiplication kernel that achieves performance on parallel with cuBLAS. You will specifically learn about: * Block-level matrix multiplications. * Multi-dimensional pointer arithmetics. * Program re-ordering for improved L2 cache hit rate. * Automatic performance tuning. """ # %% # Motivations # ----------- # # Matrix multiplications are a key building block of most modern high-performance computing systems. # They are notoriously hard to optimize, hence their implementation is generally done by # hardware vendors themselves as part of so-called "kernel libraries" (e.g., cuBLAS). # Unfortunately, these libraries are often proprietary and cannot be easily customized # to accommodate the needs of modern deep learning workloads (e.g., fused activation functions). # In this tutorial, you will learn how to implement efficient matrix multiplications by # yourself with Triton, in a way that is easy to customize and extend. # # Roughly speaking, the kernel that we will write will implement the following blocked # algorithm to multiply a (M, K) by a (K, N) matrix: # # .. code-block:: python # # # Do in parallel # for m in range(0, M, BLOCK_SIZE_M): # # Do in parallel # for n in range(0, N, BLOCK_SIZE_N): # acc = zeros((BLOCK_SIZE_M, BLOCK_SIZE_N), dtype=float32) # for k in range(0, K, BLOCK_SIZE_K): # a = A[m : m+BLOCK_SIZE_M, k : k+BLOCK_SIZE_K] # b = B[k : k+BLOCK_SIZE_K, n : n+BLOCK_SIZE_N] # acc += dot(a, b) # C[m : m+BLOCK_SIZE_M, n : n+BLOCK_SIZE_N] = acc # # where each iteration of the doubly-nested for-loop is performed by a dedicated Triton program instance. # %% # Compute Kernel # -------------- # # The above algorithm is, actually, fairly straightforward to implement in Triton. # The main difficulty comes from the computation of the memory locations at which blocks # of :code:`A` and :code:`B` must be read in the inner loop. For that, we need # multi-dimensional pointer arithmetics. # # Pointer Arithmetics # ~~~~~~~~~~~~~~~~~~~ # # For a row-major 2D tensor :code:`X`, the memory location of :code:`X[i, j]` is given b # y :code:`&X[i, j] = X + i*stride_xi + j*stride_xj`. # Therefore, blocks of pointers for :code:`A[m : m+BLOCK_SIZE_M, k:k+BLOCK_SIZE_K]` and # :code:`B[k : k+BLOCK_SIZE_K, n : n+BLOCK_SIZE_N]` can be defined in pseudo-code as: # # .. code-block:: python # # &A[m : m+BLOCK_SIZE_M, k:k+BLOCK_SIZE_K] = a_ptr + (m : m+BLOCK_SIZE_M)[:, None]*A.stride(0) + (k : k+BLOCK_SIZE_K)[None, :]*A.stride(1); # &B[k : k+BLOCK_SIZE_K, n:n+BLOCK_SIZE_N] = b_ptr + (k : k+BLOCK_SIZE_K)[:, None]*B.stride(0) + (n : n+BLOCK_SIZE_N)[None, :]*B.stride(1); # # Which means that pointers for blocks of A and B can be initialized (i.e., :code:`k=0`) in Triton as the following # code. Also note that we need an extra modulo to handle the case where :code:`M` is not a multiple of # :code:`BLOCK_SIZE_M` or :code:`N` is not a multiple of :code:`BLOCK_SIZE_N`, in which case we can pad the data with # some useless values, which will not contribute to the results. For the :code:`K` dimension, we will handle that later # using masking load semantics. # # .. code-block:: python # # offs_am = (pid_m * BLOCK_SIZE_M + tl.arange(0, BLOCK_SIZE_M)) % M # offs_bn = (pid_n * BLOCK_SIZE_N + tl.arange(0, BLOCK_SIZE_N)) % N # offs_k = tl.arange(0, BLOCK_SIZE_K) # a_ptrs = a_ptr + (offs_am[:, None]*stride_am + offs_k [None, :]*stride_ak) # b_ptrs = b_ptr + (offs_k [:, None]*stride_bk + offs_bn[None, :]*stride_bn) # # And then updated in the inner loop as follows: # # .. code-block:: python # # a_ptrs += BLOCK_SIZE_K * stride_ak; # b_ptrs += BLOCK_SIZE_K * stride_bk; # # # L2 Cache Optimizations # ~~~~~~~~~~~~~~~~~~~~~~ # # As mentioned above, each program instance computes a :code:`[BLOCK_SIZE_M, BLOCK_SIZE_N]` # block of :code:`C`. # It is important to remember that the order in which these blocks are computed does # matter, since it affects the L2 cache hit rate of our program. and unfortunately, a # a simple row-major ordering # # .. code-block:: Python # # pid = triton.program_id(0); # grid_m = (M + BLOCK_SIZE_M - 1) // BLOCK_SIZE_M; # grid_n = (N + BLOCK_SIZE_N - 1) // BLOCK_SIZE_N; # pid_m = pid / grid_n; # pid_n = pid % grid_n; # # is just not going to cut it. # # One possible solution is to launch blocks in an order that promotes data reuse. # This can be done by 'super-grouping' blocks in groups of :code:`GROUP_M` rows before # switching to the next column: # # .. code-block:: python # # # Program ID # pid = tl.program_id(axis=0) # # Number of program ids along the M axis # num_pid_m = tl.cdiv(M, BLOCK_SIZE_M) # # Number of programs ids along the N axis # num_pid_n = tl.cdiv(N, BLOCK_SIZE_N) # # Number of programs in group # num_pid_in_group = GROUP_SIZE_M * num_pid_n # # Id of the group this program is in # group_id = pid // num_pid_in_group # # Row-id of the first program in the group # first_pid_m = group_id * GROUP_SIZE_M # # If `num_pid_m` isn't divisible by `GROUP_SIZE_M`, the last group is smaller # group_size_m = min(num_pid_m - first_pid_m, GROUP_SIZE_M) # # *Within groups*, programs are ordered in a column-major order # # Row-id of the program in the *launch grid* # pid_m = first_pid_m + (pid % group_size_m) # # Col-id of the program in the *launch grid* # pid_n = (pid % num_pid_in_group) // group_size_m # # For example, in the following matmul where each matrix is 9 blocks by 9 blocks, # we can see that if we compute the output in row-major ordering, we need to load 90 # blocks into SRAM to compute the first 9 output blocks, but if we do it in grouped # ordering, we only need to load 54 blocks. # # .. image:: grouped_vs_row_major_ordering.png # # In practice, this can improve the performance of our matrix multiplication kernel by # more than 10\% on some hardware architecture (e.g., 220 to 245 TFLOPS on A100). # # %% # Final Result # ------------ import torch import triton import triton.language as tl # `triton.jit`'ed functions can be auto-tuned by using the `triton.autotune` decorator, which consumes: # - A list of `triton.Config` objects that define different configurations of # meta-parameters (e.g., `BLOCK_SIZE_M`) and compilation options (e.g., `num_warps`) to try # - An auto-tuning *key* whose change in values will trigger evaluation of all the # provided configs @triton.autotune( configs=[ triton.Config({'BLOCK_SIZE_M': 128, 'BLOCK_SIZE_N': 256, 'BLOCK_SIZE_K': 64, 'GROUP_SIZE_M': 8}, num_stages=3, num_warps=8), triton.Config({'BLOCK_SIZE_M': 64, 'BLOCK_SIZE_N': 256, 'BLOCK_SIZE_K': 32, 'GROUP_SIZE_M': 8}, num_stages=4, num_warps=4), triton.Config({'BLOCK_SIZE_M': 128, 'BLOCK_SIZE_N': 128, 'BLOCK_SIZE_K': 32, 'GROUP_SIZE_M': 8}, num_stages=4, num_warps=4), triton.Config({'BLOCK_SIZE_M': 128, 'BLOCK_SIZE_N': 64, 'BLOCK_SIZE_K': 32, 'GROUP_SIZE_M': 8}, num_stages=4, num_warps=4), triton.Config({'BLOCK_SIZE_M': 64, 'BLOCK_SIZE_N': 128, 'BLOCK_SIZE_K': 32, 'GROUP_SIZE_M': 8}, num_stages=4, num_warps=4), triton.Config({'BLOCK_SIZE_M': 128, 'BLOCK_SIZE_N': 32, 'BLOCK_SIZE_K': 32, 'GROUP_SIZE_M': 8}, num_stages=4, num_warps=4), triton.Config({'BLOCK_SIZE_M': 64, 'BLOCK_SIZE_N': 32, 'BLOCK_SIZE_K': 32, 'GROUP_SIZE_M': 8}, num_stages=5, num_warps=2), triton.Config({'BLOCK_SIZE_M': 32, 'BLOCK_SIZE_N': 64, 'BLOCK_SIZE_K': 32, 'GROUP_SIZE_M': 8}, num_stages=5, num_warps=2), ], key=['M', 'N', 'K'], ) @triton.jit def matmul_kernel( # Pointers to matrices a_ptr, b_ptr, c_ptr, # Matrix dimensions M, N, K, # The stride variables represent how much to increase the ptr by when moving by 1 # element in a particular dimension. E.g. `stride_am` is how much to increase `a_ptr` # by to get the element one row down (A has M rows). stride_am, stride_ak, stride_bk, stride_bn, stride_cm, stride_cn, # Meta-parameters BLOCK_SIZE_M: tl.constexpr, BLOCK_SIZE_N: tl.constexpr, BLOCK_SIZE_K: tl.constexpr, GROUP_SIZE_M: tl.constexpr, ACTIVATION: tl.constexpr, ): """Kernel for computing the matmul C = A x B. A has shape (M, K), B has shape (K, N) and C has shape (M, N) """ # ----------------------------------------------------------- # Map program ids `pid` to the block of C it should compute. # This is done in a grouped ordering to promote L2 data reuse. # See above `L2 Cache Optimizations` section for details. pid = tl.program_id(axis=0) num_pid_m = tl.cdiv(M, BLOCK_SIZE_M) num_pid_n = tl.cdiv(N, BLOCK_SIZE_N) num_pid_in_group = GROUP_SIZE_M * num_pid_n group_id = pid // num_pid_in_group first_pid_m = group_id * GROUP_SIZE_M group_size_m = min(num_pid_m - first_pid_m, GROUP_SIZE_M) pid_m = first_pid_m + (pid % group_size_m) pid_n = (pid % num_pid_in_group) // group_size_m # ---------------------------------------------------------- # Create pointers for the first blocks of A and B. # We will advance this pointer as we move in the K direction # and accumulate # `a_ptrs` is a block of [BLOCK_SIZE_M, BLOCK_SIZE_K] pointers # `b_ptrs` is a block of [BLOCK_SIZE_K, BLOCK_SIZE_N] pointers # See above `Pointer Arithmetics` section for details offs_am = (pid_m * BLOCK_SIZE_M + tl.arange(0, BLOCK_SIZE_M)) % M offs_bn = (pid_n * BLOCK_SIZE_N + tl.arange(0, BLOCK_SIZE_N)) % N offs_k = tl.arange(0, BLOCK_SIZE_K) a_ptrs = a_ptr + (offs_am[:, None] * stride_am + offs_k[None, :] * stride_ak) b_ptrs = b_ptr + (offs_k[:, None] * stride_bk + offs_bn[None, :] * stride_bn) # ----------------------------------------------------------- # Iterate to compute a block of the C matrix. # We accumulate into a `[BLOCK_SIZE_M, BLOCK_SIZE_N]` block # of fp32 values for higher accuracy. # `accumulator` will be converted back to fp16 after the loop. accumulator = tl.zeros((BLOCK_SIZE_M, BLOCK_SIZE_N), dtype=tl.float32) for k in range(0, tl.cdiv(K, BLOCK_SIZE_K)): # Load the next block of A and B, generate a mask by checking the K dimension. # If it is out of bounds, set it to 0. a = tl.load(a_ptrs, mask=offs_k[None, :] < K - k * BLOCK_SIZE_K, other=0.0) b = tl.load(b_ptrs, mask=offs_k[:, None] < K - k * BLOCK_SIZE_K, other=0.0) # We accumulate along the K dimension. accumulator += tl.dot(a, b) # Advance the ptrs to the next K block. a_ptrs += BLOCK_SIZE_K * stride_ak b_ptrs += BLOCK_SIZE_K * stride_bk # You can fuse arbitrary activation functions here # while the accumulator is still in FP32! if ACTIVATION == "leaky_relu": accumulator = leaky_relu(accumulator) c = accumulator.to(tl.float16) # ----------------------------------------------------------- # Write back the block of the output matrix C with masks. offs_cm = pid_m * BLOCK_SIZE_M + tl.arange(0, BLOCK_SIZE_M) offs_cn = pid_n * BLOCK_SIZE_N + tl.arange(0, BLOCK_SIZE_N) c_ptrs = c_ptr + stride_cm * offs_cm[:, None] + stride_cn * offs_cn[None, :] c_mask = (offs_cm[:, None] < M) & (offs_cn[None, :] < N) tl.store(c_ptrs, c, mask=c_mask) # We can fuse `leaky_relu` by providing it as an `ACTIVATION` meta-parameter in `_matmul`. @triton.jit def leaky_relu(x): x = x + 1 return tl.where(x >= 0, x, 0.01 * x) # %% # We can now create a convenience wrapper function that only takes two input tensors, # and (1) checks any shape constraint; (2) allocates the output; (3) launches the above kernel. def matmul(a, b, activation=""): # Check constraints. assert a.shape[1] == b.shape[0], "Incompatible dimensions" assert a.is_contiguous(), "Matrix A must be contiguous" assert b.is_contiguous(), "Matrix B must be contiguous" M, K = a.shape K, N = b.shape # Allocates output. c = torch.empty((M, N), device=a.device, dtype=a.dtype) # 1D launch kernel where each block gets its own program. grid = lambda META: ( triton.cdiv(M, META['BLOCK_SIZE_M']) * triton.cdiv(N, META['BLOCK_SIZE_N']), ) matmul_kernel[grid]( a, b, c, M, N, K, a.stride(0), a.stride(1), b.stride(0), b.stride(1), c.stride(0), c.stride(1), ACTIVATION=activation ) return c # %% # Unit Test # --------- # # We can test our custom matrix multiplication operation against a native torch implementation (i.e., cuBLAS). torch.manual_seed(0) a = torch.randn((512, 512), device='cuda', dtype=torch.float16) b = torch.randn((512, 512), device='cuda', dtype=torch.float16) triton_output = matmul(a, b) torch_output = torch.matmul(a, b) print(f"triton_output={triton_output}") print(f"torch_output={torch_output}") if torch.allclose(triton_output, torch_output, atol=1e-2, rtol=0): print("✅ Triton and Torch match") else: print("❌ Triton and Torch differ") # %% # Benchmark # --------- # # Square Matrix Performance # ~~~~~~~~~~~~~~~~~~~~~~~~~~ # # We can now compare the performance of our kernel against that of cuBLAS. Here we focus on square matrices, # but feel free to arrange this script as you wish to benchmark any other matrix shape. @triton.testing.perf_report( triton.testing.Benchmark( x_names=['M', 'N', 'K'], # Argument names to use as an x-axis for the plot x_vals=[ 128 * i for i in range(2, 33) ], # Different possible values for `x_name` line_arg='provider', # Argument name whose value corresponds to a different line in the plot # Possible values for `line_arg` line_vals=['cublas', 'triton'], # Label name for the lines line_names=["cuBLAS", "Triton"], # Line styles styles=[('green', '-'), ('blue', '-')], ylabel="TFLOPS", # Label name for the y-axis plot_name="matmul-performance", # Name for the plot, used also as a file name for saving the plot. args={}, ) ) def benchmark(M, N, K, provider): a = torch.randn((M, K), device='cuda', dtype=torch.float16) b = torch.randn((K, N), device='cuda', dtype=torch.float16) quantiles = [0.5, 0.2, 0.8] if provider == 'cublas': ms, min_ms, max_ms = triton.testing.do_bench(lambda: torch.matmul(a, b), quantiles=quantiles) if provider == 'triton': ms, min_ms, max_ms = triton.testing.do_bench(lambda: matmul(a, b), quantiles=quantiles) perf = lambda ms: 2 * M * N * K * 1e-12 / (ms * 1e-3) return perf(ms), perf(max_ms), perf(min_ms) benchmark.run(show_plots=True, print_data=True)
14,818
41.219373
143
py
triton
triton-main/python/examples/empty.py
import torch import triton import triton.language as tl @triton.jit def kernel(X, stride_xm, stride_xn, BLOCK: tl.constexpr): pass X = torch.randn(1, device="cuda") pgm = kernel[(1,)](X, 1, 1, BLOCK=1024)
214
14.357143
57
py
triton
triton-main/python/test/unit/runtime/test_cache.py
import os import shutil import pytest import torch import triton import triton.language as tl from triton.runtime.jit import JITFunction tmpdir = ".tmp" @triton.jit def function_1(i): i = i + 1 i = function_2(i) return i @triton.jit def function_2(i): i = i + 1 return i @triton.jit def kernel(X, i, BLOCK: tl.constexpr): i = i + 1 i = function_1(i) tl.store(X, i) @triton.jit(do_not_specialize=["i"]) def kernel_nospec(X, i, BLOCK: tl.constexpr): i = i + 1 i = function_1(i) tl.store(X, i) def apply_src_change(target, old, new): kernel.hash = None function_1.hash = None function_2.hash = None function_1.src = function_1.src.replace(old, new) target.src = target.src.replace(old, new) ret = target.cache_key target.src = target.src.replace(new, old) return ret def test_nochange(): baseline = kernel.cache_key updated = apply_src_change(kernel, 'i + 1', 'i + 1') assert baseline == updated def test_toplevel_change(): baseline = kernel.cache_key updated = apply_src_change(kernel, 'i + 1', 'i + 2') assert baseline != updated def test_nested1_change(): baseline = kernel.cache_key updated = apply_src_change(function_1, 'i + 1', 'i + 2') assert baseline != updated def reset_tmp_dir(): os.environ["TRITON_CACHE_DIR"] = tmpdir if os.path.exists(tmpdir): shutil.rmtree(tmpdir) def test_reuse(): counter = 0 def inc_counter(*args, **kwargs): nonlocal counter counter += 1 JITFunction.cache_hook = inc_counter reset_tmp_dir() x = torch.empty(1, dtype=torch.int32, device='cuda') for i in range(10): kernel[(1,)](x, 1, BLOCK=1024) assert counter == 1 @pytest.mark.parametrize('mode', ['enable', 'disable']) def test_specialize(mode): counter = 0 def inc_counter(*args, **kwargs): nonlocal counter counter += 1 JITFunction.cache_hook = inc_counter reset_tmp_dir() x = torch.empty(1, dtype=torch.int32, device='cuda') function = {'enable': kernel, 'disable': kernel_nospec}[mode] target = {'enable': 3, 'disable': 1}[mode] for i in [1, 2, 4, 8, 16, 32]: function[(1,)](x, i, BLOCK=512) assert counter == target def test_constexpr_not_callable() -> None: @triton.jit def kernel(X, c: tl.constexpr): tl.store(X, 2) x = torch.empty(1, dtype=torch.int32, device='cuda') error = False try: kernel[(1, )](x, c="str") except BaseException: error = True assert error is False # try and catch try: kernel[(1, )](x, c=tl.abs) except BaseException: error = True assert error is True def test_jit_warmup_cache() -> None: @triton.jit def kernel_add(a, b, o, N: tl.constexpr): idx = tl.arange(0, N) tl.store(o + idx, tl.load(a + idx) + tl.load(b + idx)) args = [ torch.randn(32, dtype=torch.float32, device="cuda"), torch.randn(32, dtype=torch.float32, device="cuda"), torch.randn(32, dtype=torch.float32, device="cuda"), 32, ] assert len(kernel_add.cache) == 0 kernel_add.warmup(torch.float32, torch.float32, torch.float32, 32, grid=(1,)) assert len(kernel_add.cache) == 1 kernel_add.warmup(*args, grid=(1,)) assert len(kernel_add.cache) == 1 kernel_add.warmup(*args, grid=(1,)) assert len(kernel_add.cache) == 1 def test_jit_debug() -> None: @triton.jit def kernel_add(a, b, o, N: tl.constexpr): idx = tl.arange(0, N) tl.device_assert(idx < 32, "idx < 32") tl.store(o + idx, tl.load(a + idx) + tl.load(b + idx)) device = torch.cuda.current_device() assert len(kernel_add.cache[device]) == 0 kernel_add.warmup(torch.float32, torch.float32, torch.float32, 32, grid=(1,)) assert len(kernel_add.cache[device]) == 1 kernel_add.debug = False kernel_add.warmup(torch.float32, torch.float32, torch.float32, 32, grid=(1,)) assert len(kernel_add.cache[device]) == 2 kernel_add.debug = True kernel_add.warmup(torch.float32, torch.float32, torch.float32, 32, grid=(1,)) assert len(kernel_add.cache[device]) == 3 bins = list(kernel_add.cache[device].values()) assert bins[2].asm['ttir'] != bins[1].asm['ttir'] @triton.jit def add_fn(a, b, o, N: tl.constexpr): idx = tl.arange(0, N) tl.store(o + idx, tl.load(a + idx) + tl.load(b + idx)) def test_jit_noinline() -> None: @triton.jit def kernel_add_device(a, b, o, N: tl.constexpr): add_fn(a, b, o, N) device = torch.cuda.current_device() assert len(kernel_add_device.cache[device]) == 0 kernel_add_device.warmup(torch.float32, torch.float32, torch.float32, 32, grid=(1,)) assert len(kernel_add_device.cache[device]) == 1 bins = list(kernel_add_device.cache[device].values()) inline_ttir = bins[0].asm['ttir'] add_fn.noinline = True add_fn.hash = None kernel_add_device.hash = None kernel_add_device.cache[device].clear() kernel_add_device.warmup(torch.float32, torch.float32, torch.float32, 32, grid=(1,)) assert len(kernel_add_device.cache[device]) == 1 bins = list(kernel_add_device.cache[device].values()) noinline_ttir = bins[0].asm['ttir'] assert inline_ttir != noinline_ttir def test_memory_leak() -> None: @triton.jit def kernel(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 10 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + (x0), xmask) tl.store(out_ptr0 + (x0 + tl.zeros([XBLOCK], tl.int32)), tmp0, xmask)
5,762
26.574163
88
py
triton
triton-main/python/test/unit/runtime/test_subproc.py
import multiprocessing import os import shutil from collections import namedtuple import torch import triton import triton.language as tl tmpdir = ".tmp" def reset_tmp_dir(): os.environ["TRITON_CACHE_DIR"] = tmpdir if os.path.exists(tmpdir): shutil.rmtree(tmpdir) instance_descriptor = namedtuple("instance_descriptor", ["divisible_by_16", "equal_to_1"]) def compile_fn(config, cc): @triton.jit def kernel_sub(a, b, o, N: tl.constexpr): idx = tl.arange(0, N) tl.store(o + idx, tl.load(a + idx) - tl.load(b + idx) * 777) triton.compile( fn=kernel_sub, signature={0: "*fp32", 1: "*fp32", 2: "*fp32"}, device=0, constants={3: 32}, configs=[config], warm_cache_only=True, cc=cc, ) def test_compile_in_subproc() -> None: major, minor = torch.cuda.get_device_capability(0) cc = major * 10 + minor config = instance_descriptor(tuple(range(4)), ()) multiprocessing.set_start_method('fork') proc = multiprocessing.Process( target=compile_fn, args=(config, cc)) proc.start() proc.join() assert proc.exitcode == 0 def compile_fn_dot(config, cc): @triton.jit def kernel_dot(Z): offs = tl.arange(0, 16)[:, None] * 16 + tl.arange(0, 16)[None, :] z = tl.load(Z + offs) z = tl.dot(z, z) tl.store(Z + offs, z) triton.compile( fn=kernel_dot, signature={0: "*fp32"}, device=0, configs=[config], warm_cache_only=True, cc=cc, ) def test_compile_in_forked_subproc() -> None: reset_tmp_dir() major, minor = torch.cuda.get_device_capability(0) cc = major * 10 + minor config = instance_descriptor(tuple(range(1)), ()) assert multiprocessing.get_start_method() == 'fork' proc = multiprocessing.Process( target=compile_fn_dot, args=(config, cc)) proc.start() proc.join() assert proc.exitcode == 0
1,989
22.690476
90
py
triton
triton-main/python/test/unit/runtime/test_autotuner.py
import torch import triton import triton.language as tl def test_kwargs(): N = 1024 src = torch.empty(N, device='cuda') dst = torch.empty(N, device='cuda') configs = [triton.Config(kwargs={'BLOCK_SIZE': 32}), triton.Config(kwargs={'BLOCK_SIZE': 128})] @triton.autotune(configs=configs, key=['N']) @triton.jit def _kernel(dst, src, N, BLOCK_SIZE: tl.constexpr): offsets = tl.program_id(0) * BLOCK_SIZE + tl.arange(0, BLOCK_SIZE) x = tl.load(src + offsets, mask=offsets < N) tl.store(dst + offsets, x, mask=offsets < N) grid = lambda META: (triton.cdiv(N, META['BLOCK_SIZE']),) _kernel[grid](dst, src, N) _kernel[grid](dst=dst, src=src, N=N)
709
29.869565
99
py
triton
triton-main/python/test/unit/runtime/test_launch.py
import gc # import importlib # import os # import sys # import tempfile # import textwrap # import time import tracemalloc import torch import triton import triton.language as tl # from typing import Tuple def test_memory_leak() -> None: @triton.jit def kernel(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 10 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + (x0), xmask) tl.store(out_ptr0 + (x0 + tl.zeros([XBLOCK], tl.int32)), tmp0, xmask) tracemalloc.start() try: inp = torch.randn(10, device='cuda') out = torch.randn(10, device='cuda') kernel[(10,)](inp, out, 10, XBLOCK=16) gc.collect() begin, _ = tracemalloc.get_traced_memory() for _ in range(100): kernel[(10,)](inp, out, 10, XBLOCK=16) gc.collect() end, _ = tracemalloc.get_traced_memory() assert end - begin < 5000 finally: tracemalloc.stop() # LATENCY_THRESHOLD_US = 46 # def test_kernel_launch_latency() -> None: # def define_kernel(kernel_name: str, num_tensor_args: int) -> str: # arg_str = ",".join([f"arg{i}: torch.Tensor" for i in range(num_tensor_args)]) # arg_str += ", n_elements: int, BLOCK_SIZE: tl.constexpr" # func_str = f""" # import torch # import triton # import triton.language as tl # @triton.jit # def {kernel_name}({arg_str}): # pass # """ # with tempfile.NamedTemporaryFile(mode="w+t", suffix=".py", delete=False) as temp_file: # temp_file.write(textwrap.dedent(func_str)) # temp_file_path = temp_file.name # return temp_file_path # def import_kernel(file_path, kernel_name): # directory, filename = os.path.split(file_path) # module_name, _ = os.path.splitext(filename) # sys.path.insert(0, directory) # module = importlib.import_module(module_name) # kernel = getattr(module, kernel_name) # return kernel # def empty(*kernel_args: Tuple[torch.Tensor]): # first_arg = kernel_args[0] # n_elements = first_arg.numel() # grid = (triton.cdiv(n_elements, 1024),) # device = torch.cuda.current_device() # # Warmup # empty_kernel[grid](*kernel_args, n_elements, BLOCK_SIZE=1024, device=device) # torch.cuda.synchronize() # # Measure launch overhead at steady state # num_runs = 1000 # start_time = time.time() # for i in range(num_runs): # empty_kernel[grid](*kernel_args, n_elements, BLOCK_SIZE=1024, device=device) # end_time = time.time() # latency_us = (end_time - start_time) / num_runs * 1e6 # assert latency_us < LATENCY_THRESHOLD_US, "Kernel launch time has increased!" # num_tensor_args = 40 # kernel_name = 'empty_kernel' # file_path = define_kernel(kernel_name, num_tensor_args) # empty_kernel = import_kernel(file_path, kernel_name) # # Initialize random tensors for the empty_kernel # torch.manual_seed(0) # size = 1024 # kernel_args = (torch.rand(size, device='cuda') for i in range(num_tensor_args)) # # Run empty, which would run empty_kernel internally # empty(*kernel_args)
3,409
30.869159
96
py
triton
triton-main/python/test/unit/interpreter/test_interpreter.py
import random import torch import triton import triton.language as tl from triton.interpreter.interpreter import program_ids_from_grid def test_addition(): @triton.jit(interpret=True) def add_kernel( x_ptr, y_ptr, output_ptr, n_elements, BLOCK_SIZE: tl.constexpr, ): pid = tl.program_id(axis=0) block_start = pid * BLOCK_SIZE offsets = block_start + tl.arange(0, BLOCK_SIZE) mask = offsets < n_elements x = tl.load(x_ptr + offsets, mask=mask) y = tl.load(y_ptr + offsets, mask=mask) output = x + y tl.store(output_ptr + offsets, output, mask=mask) a = torch.rand((128,), device="cuda") b = torch.rand((128,), device="cuda") expected = a + b output = torch.empty((128,), device="cuda") def grid(meta): return (triton.cdiv(128, meta["BLOCK_SIZE"]),) add_kernel[grid](a, b, output, 128, BLOCK_SIZE=32) assert torch.allclose(expected, output, atol=1e-2, rtol=0) def test_program_ids_from_grid(): random.seed(123) grid = (3, 4) expected_combinations = 3 * 4 unique_combinations = set(program_ids_from_grid(grid)) assert len(unique_combinations) == expected_combinations first_run = list(program_ids_from_grid(grid)) second_run = list(program_ids_from_grid(grid)) assert first_run != second_run def test_atomic(): @triton.jit(interpret=True) def atomic( x_ptr, ): pid = tl.program_id(axis=0) tl.atomic_add(x_ptr + pid, 1) t = tl.atomic_xchg(x_ptr + pid, 3) t += 1 # 2 tl.atomic_cas(x_ptr + pid, 3, t) # match tl.atomic_cas(x_ptr + pid, 40, 9) # no match nb_dim = 16 a = torch.zeros((nb_dim, ), dtype=torch.int32, device="cuda") atomic[(nb_dim, )](a) assert torch.allclose(a, torch.full_like(a, 2))
1,878
25.842857
65
py
triton
triton-main/python/test/unit/operators/test_inductor.py
import torch import triton import triton.language as tl def test_normalization_with_remat(): @triton.jit def triton_(in_out_ptr0, in_out_ptr1, in_ptr0, in_ptr1, in_ptr2, in_ptr3, xnumel, rnumel, XBLOCK: tl.constexpr, RBLOCK: tl.constexpr): xnumel = 512 rnumel = 4096 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel rbase = tl.arange(0, RBLOCK)[None, :] x3 = xindex x0 = xindex % 64 tmp1 = tl.load(in_ptr0 + (x0), xmask) tmp3 = tl.load(in_ptr1 + (x0), xmask) tmp11 = tl.load(in_ptr2 + (x0), xmask) tmp13 = tl.load(in_ptr3 + (x0), xmask) _tmp17 = tl.zeros([XBLOCK, RBLOCK], tl.float32) + 0 for roffset in range(0, rnumel, RBLOCK): rindex = roffset + rbase rmask = rindex < rnumel r2 = rindex tmp0 = tl.load(in_out_ptr0 + (r2 + (4096 * x3)), rmask & xmask, eviction_policy='evict_last', other=0) tmp2 = tmp0 - tmp1 tmp4 = 1e-05 tmp5 = tmp3 + tmp4 tmp6 = tl.sqrt(tmp5) tmp7 = 1 / tmp6 tmp8 = 1.0 tmp9 = tmp7 * tmp8 tmp10 = tmp2 * tmp9 tmp12 = tmp10 * tmp11 tmp14 = tmp12 + tmp13 _tmp17 = tl.where(rmask & xmask, _tmp17 + tmp14, _tmp17) tl.store(in_out_ptr0 + (r2 + (4096 * x3) + tl.zeros([XBLOCK, RBLOCK], tl.int32)), tmp14, rmask & xmask) tmp17 = tl.sum(_tmp17, 1)[:, None] tmp18 = 4096.0 tmp19 = tmp17 / tmp18 tl.store(in_out_ptr1 + (x3 + tl.zeros([XBLOCK, 1], tl.int32)), tmp19, xmask) torch.manual_seed(123) buf14 = torch.rand(8, 64, 64, 64, device="cuda") buf16 = torch.rand(8, 1, 64, device="cuda") arg114_1 = torch.rand(64, device="cuda") arg115_1 = torch.rand(64, device="cuda") arg8_1 = torch.rand(64, device="cuda") arg9_1 = torch.rand(64, device="cuda") triton_[(512,)](buf14, buf16, arg114_1, arg115_1, arg8_1, arg9_1, 512, 4096, 1, 2048) torch.testing.assert_allclose(buf16.mean().item(), buf14.mean().item(), atol=1e-7, rtol=0) def test_avg_pool_bw(): @triton.jit def triton_(in_ptr0, out_ptr0, XBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] x1 = (xindex // 8) % 8 x0 = xindex % 8 x2 = (xindex // 64) x5 = xindex tmp0 = (-1) + x1 tmp1 = (-1) + x0 tmp2 = 2 + x1 tmp3 = 2 + x0 tmp4 = 0 tmp5 = tl.where(tmp0 != tmp0, tmp0, tl.where(tmp0 > tmp4, tmp0, tmp4)) tmp6 = tl.where(tmp1 != tmp1, tmp1, tl.where(tmp1 > tmp4, tmp1, tmp4)) tmp7 = 8 tmp8 = tl.where(tmp2 != tmp2, tmp2, tl.where(tmp2 < tmp7, tmp2, tmp7)) tmp9 = tl.where(tmp3 != tmp3, tmp3, tl.where(tmp3 < tmp7, tmp3, tmp7)) tmp10 = tmp5 + tmp4 tmp11 = tmp6 + tmp4 tmp12 = 1 tmp13 = tmp8 - tmp12 tmp14 = tl.where(tmp10 != tmp10, tmp10, tl.where(tmp10 < tmp13, tmp10, tmp13)) tmp15 = tmp9 - tmp12 tmp16 = tl.where(tmp11 != tmp11, tmp11, tl.where(tmp11 < tmp15, tmp11, tmp15)) tmp17 = tl.load(in_ptr0 + (tmp16 + (8 * tmp14) + (64 * x2)), None).to(tl.float32) tmp18 = tmp17 / 9 tmp19 = tmp10 < tmp8 tmp20 = tmp11 < tmp9 tmp21 = tmp19 & tmp20 tmp22 = 0.0 tmp23 = tl.where(tmp21, tmp18, tmp22) tmp24 = tmp6 + tmp12 tmp25 = tl.where(tmp24 != tmp24, tmp24, tl.where(tmp24 < tmp15, tmp24, tmp15)) tmp26 = tl.load(in_ptr0 + (tmp25 + (8 * tmp14) + (64 * x2)), None).to(tl.float32) tmp27 = tmp26 / 9 tmp28 = tmp24 < tmp9 tmp29 = tmp19 & tmp28 tmp30 = tmp23 + tmp27 tmp31 = tl.where(tmp29, tmp30, tmp23) tmp32 = 2 tmp33 = tmp6 + tmp32 tmp34 = tl.where(tmp33 != tmp33, tmp33, tl.where(tmp33 < tmp15, tmp33, tmp15)) tmp35 = tl.load(in_ptr0 + (tmp34 + (8 * tmp14) + (64 * x2)), None).to(tl.float32) tmp36 = tmp35 / 9 tmp37 = tmp33 < tmp9 tmp38 = tmp19 & tmp37 tmp39 = tmp31 + tmp36 tmp40 = tl.where(tmp38, tmp39, tmp31) tmp41 = tmp5 + tmp12 tmp42 = tl.where(tmp41 != tmp41, tmp41, tl.where(tmp41 < tmp13, tmp41, tmp13)) tmp43 = tl.load(in_ptr0 + (tmp16 + (8 * tmp42) + (64 * x2)), None).to(tl.float32) tmp44 = tmp43 / 9 tmp45 = tmp41 < tmp8 tmp46 = tmp45 & tmp20 tmp47 = tmp40 + tmp44 tmp48 = tl.where(tmp46, tmp47, tmp40) tmp49 = tl.load(in_ptr0 + (tmp25 + (8 * tmp42) + (64 * x2)), None).to(tl.float32) tmp50 = tmp49 / 9 tmp51 = tmp45 & tmp28 tmp52 = tmp48 + tmp50 tmp53 = tl.where(tmp51, tmp52, tmp48) tmp54 = tl.load(in_ptr0 + (tmp34 + (8 * tmp42) + (64 * x2)), None).to(tl.float32) tmp55 = tmp54 / 9 tmp56 = tmp45 & tmp37 tmp57 = tmp53 + tmp55 tmp58 = tl.where(tmp56, tmp57, tmp53) tmp59 = tmp5 + tmp32 tmp60 = tl.where(tmp59 != tmp59, tmp59, tl.where(tmp59 < tmp13, tmp59, tmp13)) tmp61 = tl.load(in_ptr0 + (tmp16 + (8 * tmp60) + (64 * x2)), None).to(tl.float32) tmp62 = tmp61 / 9 tmp63 = tmp59 < tmp8 tmp64 = tmp63 & tmp20 tmp65 = tmp58 + tmp62 tmp66 = tl.where(tmp64, tmp65, tmp58) tmp67 = tl.load(in_ptr0 + (tmp25 + (8 * tmp60) + (64 * x2)), None).to(tl.float32) tmp68 = tmp67 / 9 tmp69 = tmp63 & tmp28 tmp70 = tmp66 + tmp68 tmp71 = tl.where(tmp69, tmp70, tmp66) tmp72 = tl.load(in_ptr0 + (tmp34 + (8 * tmp60) + (64 * x2)), None).to(tl.float32) tmp73 = tmp72 / 9 tmp74 = tmp63 & tmp37 tmp75 = tmp71 + tmp73 tmp76 = tl.where(tmp74, tmp75, tmp71) tl.store(out_ptr0 + (x5 + tl.zeros([XBLOCK], tl.int32)), tmp76, None) inp = torch.ones(8, 2048, 8, 8, device="cuda", dtype=torch.half) out = torch.ones_like(inp) * 3 numel = inp.numel() triton_[(numel // 1024,)](inp, out, 1024) out_ref = torch.ones_like(inp) out_ref[:, :, 1:7, 0::7] = 2 / 3 out_ref[:, :, 0::7, 1:7] = 2 / 3 out_ref[:, :, 0::7, 0::7] = 4 / 9 torch.testing.assert_allclose(out, out_ref)
6,326
39.557692
138
py
triton
triton-main/python/test/unit/operators/test_cross_entropy.py
import pytest import torch import triton import triton.ops @pytest.mark.parametrize("M, N, dtype, mode", [ (M, N, dtype, mode) for M in [1024, 821] for N in [512, 857, 1871, 2089, 8573, 31000] for dtype in ['float16', 'float32'] for mode in ['forward', 'backward'] ] ) def test_op(M, N, dtype, mode): capability = torch.cuda.get_device_capability() if capability[0] < 8 and dtype == "bfloat16": pytest.skip("Only test bfloat16 on devices with sm >= 80") dtype = {'bfloat16': torch.bfloat16, 'float16': torch.float16, 'float32': torch.float32}[dtype] # create inputs x = torch.randn(M, N, dtype=dtype, device='cuda', requires_grad=True) idx = 4 + torch.ones(M, dtype=torch.int64, device='cuda') # forward pass tt_y = triton.ops.cross_entropy(x, idx) th_y = torch.nn.CrossEntropyLoss(reduction="none")(x, idx) if mode == 'forward': torch.testing.assert_allclose(th_y, tt_y) # backward pass elif mode == 'backward': dy = torch.randn_like(tt_y) # triton backward tt_y.backward(dy) tt_dx = x.grad.clone() # torch backward x.grad = None th_y.backward(dy) th_dx = x.grad.clone() torch.testing.assert_allclose(th_dx, tt_dx)
1,447
34.317073
99
py
triton
triton-main/python/test/unit/operators/test_blocksparse.py
import pytest import torch import triton import triton.ops def sparsify_tensor(x, mask, block): ret = torch.empty((x.size(0), mask.sum(), block, block), dtype=x.dtype, device=x.device) for idx, (h, i, j) in enumerate(zip(*mask.nonzero(as_tuple=True))): ret[:, idx, :, :] = x[:, h, i * block:(i + 1) * block, j * block:(j + 1) * block] return ret def make_pair(shape, device="cuda", alpha=1e-2, beta=0., trans=False, data=None, dtype=torch.float32): if data is None: data = torch.randn(shape, dtype=torch.float32, requires_grad=True, device=device) ref_ret = data ref_ret = ref_ret * alpha + beta ref_ret = ref_ret.half().to(dtype) if trans: ref_ret = ref_ret.t().requires_grad_() ref_ret = ref_ret.detach().requires_grad_() tri_ret = ref_ret.clone().detach().requires_grad_() return ref_ret, tri_ret def mask_tensor(x, mask, block, value=0): ret = x.clone() for h, i, j in zip(*(mask == 0).nonzero(as_tuple=True)): ret[:, h, i * block:(i + 1) * block, j * block:(j + 1) * block] = value return ret @pytest.mark.parametrize("MODE", ["sdd", "dds", "dsd"]) @pytest.mark.parametrize("TRANS_A", [False, True]) @pytest.mark.parametrize("TRANS_B", [False, True]) @pytest.mark.parametrize("BLOCK", [16, 32, 64]) @pytest.mark.parametrize("DTYPE", [torch.float16]) def test_matmul(MODE, TRANS_A, TRANS_B, BLOCK, DTYPE, Z=3, H=2, M=512, N=384, K=256): seed = 0 torch.manual_seed(seed) is_sdd = MODE == "sdd" is_dsd = MODE == "dsd" is_dds = MODE == "dds" do_sparsify = lambda x: sparsify_tensor(x, layout, BLOCK) do_mask = lambda x: mask_tensor(x, layout, BLOCK) # create inputs # create op a_shape = (Z, H, K, M) if TRANS_A else (Z, H, M, K) b_shape = (Z, H, N, K) if TRANS_B else (Z, H, K, N) c_shape = (Z, H, M, N) shape = { "sdd": (M, N), "dsd": (a_shape[2], a_shape[3]), "dds": (b_shape[2], b_shape[3]), }[MODE] layout = torch.randint(2, (H, shape[0] // BLOCK, shape[1] // BLOCK)) layout[1, 2, :] = 0 layout[1, :, 1] = 0 # create data a_ref, a_tri = make_pair(a_shape, alpha=.1, dtype=DTYPE) b_ref, b_tri = make_pair(b_shape, alpha=.1, dtype=DTYPE) dc_ref, dc_tri = make_pair(c_shape, dtype=DTYPE) # compute [torch] dc_ref = do_mask(dc_ref) if is_sdd else dc_ref a_ref = do_mask(a_ref) if is_dsd else a_ref b_ref = do_mask(b_ref) if is_dds else b_ref a_ref.retain_grad() b_ref.retain_grad() c_ref = torch.matmul(a_ref.transpose(2, 3) if TRANS_A else a_ref, b_ref.transpose(2, 3) if TRANS_B else b_ref) c_ref.backward(dc_ref) c_ref = do_sparsify(c_ref) if is_sdd else c_ref da_ref = do_sparsify(a_ref.grad) if is_dsd else a_ref.grad db_ref = do_sparsify(b_ref.grad) if is_dds else b_ref.grad # triton result dc_tri = do_sparsify(dc_tri) if is_sdd else dc_tri a_tri = do_sparsify(a_tri) if is_dsd else a_tri b_tri = do_sparsify(b_tri) if is_dds else b_tri a_tri.retain_grad() b_tri.retain_grad() op = triton.ops.blocksparse.matmul(layout, BLOCK, MODE, trans_a=TRANS_A, trans_b=TRANS_B, device="cuda") c_tri = op(a_tri, b_tri) c_tri.backward(dc_tri) da_tri = a_tri.grad db_tri = b_tri.grad # compare torch.testing.assert_allclose(c_ref, c_tri) torch.testing.assert_allclose(da_ref, da_tri) torch.testing.assert_allclose(db_ref, db_tri) configs = [ (16, 256), (32, 576), (64, 1871), (128, 2511), ] @pytest.mark.parametrize("is_dense", [False, True]) @pytest.mark.parametrize("BLOCK, WIDTH", configs) def test_softmax(BLOCK, WIDTH, is_dense, Z=2, H=2, is_causal=True, scale=0.4): # set seed torch.random.manual_seed(0) Z, H, M, N = 2, 3, WIDTH, WIDTH # initialize layout # make sure each row has at least one non-zero element layout = torch.randint(2, (H, M // BLOCK, N // BLOCK)) if is_dense: layout[:] = 1 else: layout[1, 2, :] = 0 layout[1, :, 1] = 0 # initialize data a_shape = (Z, H, M, N) a_ref, a_tri = make_pair(a_shape) dout_ref, dout_tri = make_pair(a_shape) # compute [torch] a_ref = mask_tensor(a_ref, layout, BLOCK, value=float("-inf")) a_ref.retain_grad() at_mask = torch.ones((M, N), device="cuda") if is_causal: at_mask = torch.tril(at_mask) M = at_mask[None, None, :, :] + torch.zeros_like(a_ref) a_ref[M == 0] = float("-inf") out_ref = torch.softmax(a_ref * scale, -1) out_ref.backward(dout_ref) out_ref = sparsify_tensor(out_ref, layout, BLOCK) da_ref = sparsify_tensor(a_ref.grad, layout, BLOCK) # compute [triton] a_tri = sparsify_tensor(a_tri, layout, BLOCK) a_tri.retain_grad() dout_tri = sparsify_tensor(dout_tri, layout, BLOCK) op = triton.ops.blocksparse.softmax(layout, BLOCK, device="cuda", is_dense=is_dense) out_tri = op(a_tri, scale=scale, is_causal=is_causal) out_tri.backward(dout_tri) da_tri = a_tri.grad # compare torch.testing.assert_allclose(out_tri, out_ref) torch.testing.assert_allclose(da_tri, da_ref) @pytest.mark.parametrize("block", [16, 32, 64]) @pytest.mark.parametrize("dtype", [torch.float16, torch.float32]) def test_attention_fwd_bwd( block, dtype, input_scale=1.0, scale=1 / 8.0, n_ctx=256, batch_size=2, n_heads=2, ): capability = torch.cuda.get_device_capability() if capability[0] < 7: pytest.skip("Only test tl.dot() on devices with sm >= 70") # inputs qkv_shape = (batch_size, n_heads, n_ctx, 64) qkvs = [ torch.nn.Parameter(input_scale * torch.randn(qkv_shape), requires_grad=True).to(dtype).cuda() for _ in range(3) ] # Triton: n_blocks = n_ctx // block layout = torch.tril(torch.ones([n_heads, n_blocks, n_blocks], dtype=torch.long)) query, key, value = [x.clone() for x in qkvs] query.retain_grad() key.retain_grad() value.retain_grad() attn_out = triton_attention(layout, block, query=query, key=key, value=value, scale=scale) # ad hoc loss loss = (attn_out ** 2).mean() loss.backward() grads = [query.grad, key.grad, value.grad] # Torch version: torch_q, torch_k, torch_v = [x.clone() for x in qkvs] attn_mask = torch.ones([n_ctx, n_ctx], device="cuda", dtype=dtype) attn_mask = torch.tril(attn_mask, diagonal=0) attn_mask = 1e6 * (-1 + (attn_mask.reshape((1, 1, n_ctx, n_ctx)).cuda())) torch_q.retain_grad() torch_k.retain_grad() torch_v.retain_grad() scores = scale * torch.einsum("bhsd,bhtd->bhst", torch_q, torch_k) scores = scores + attn_mask probs = torch.softmax(scores, dim=-1) torch_attn_out = torch.einsum("bhst,bhtd->bhsd", probs, torch_v) # ad hoc loss torch_loss = (torch_attn_out ** 2).mean() torch_loss.backward() torch_grads = [torch_q.grad, torch_k.grad, torch_v.grad] # comparison # print(f"Triton loss {loss} and torch loss {torch_loss}. Also checking grads...") torch.testing.assert_allclose(loss, torch_loss, atol=1e-3, rtol=0) for g1, g2 in zip(grads, torch_grads): torch.testing.assert_allclose(g1, g2) @pytest.mark.parametrize("block", [16, 32, 64]) def triton_attention( layout, block: int, query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, scale: float, ): sparse_dot_sdd_nt = triton.ops.blocksparse.matmul(layout, block, "sdd", trans_a=False, trans_b=True, device=value.device) sparse_dot_dsd_nn = triton.ops.blocksparse.matmul(layout, block, "dsd", trans_a=False, trans_b=False, device=value.device) sparse_softmax = triton.ops.blocksparse.softmax(layout, block, device=value.device) w = sparse_dot_sdd_nt(query, key) w = sparse_softmax(w, scale=scale, is_causal=True) a = sparse_dot_dsd_nn(w, value) return a
7,887
34.854545
126
py
triton
triton-main/python/test/unit/operators/test_matmul.py
import itertools import pytest import torch import triton import triton.language as tl import triton.ops def f8_to_f16(x, dtype): @triton.jit def kernel(Y, X, N, BLOCK_SIZE: tl.constexpr): pid = tl.program_id(0) offs = pid * BLOCK_SIZE + tl.arange(0, BLOCK_SIZE) mask = offs < N x = tl.load(X + offs, mask=mask) tl.store(Y + offs, x, mask=mask) ret = torch.empty(x.shape, dtype=torch.float16, device=x.device) grid = lambda META: (triton.cdiv(x.numel(), META['BLOCK_SIZE']),) dtype = getattr(tl, dtype) kernel[grid](ret, triton.reinterpret(x, dtype), ret.numel(), BLOCK_SIZE=1024) return ret @pytest.mark.parametrize( "BLOCK_M, BLOCK_N, BLOCK_K, SPLIT_K, NWARP, NSTAGE, M, N, K, AT, BT, ADTYPE, BDTYPE", itertools.chain( *[ [ # 1 warp (16, 16, 16, 1, 1, 2, None, None, None, AT, BT, DTYPE, DTYPE), (32, 16, 16, 1, 1, 2, None, None, None, AT, BT, DTYPE, DTYPE), (16, 32, 16, 1, 1, 2, None, None, None, AT, BT, DTYPE, DTYPE), (16, 16, 32, 1, 1, 2, None, None, None, AT, BT, DTYPE, DTYPE), (32, 16, 32, 1, 1, 2, None, None, None, AT, BT, DTYPE, DTYPE), (16, 32, 32, 1, 1, 2, None, None, None, AT, BT, DTYPE, DTYPE), (16, 16, 64, 1, 1, 2, None, None, None, AT, BT, DTYPE, DTYPE), (64, 16, 64, 1, 1, 2, None, None, None, AT, BT, DTYPE, DTYPE), (16, 64, 64, 1, 1, 2, None, None, None, AT, BT, DTYPE, DTYPE), # 2 warp (64, 32, 64, 1, 2, 2, None, None, None, AT, BT, DTYPE, DTYPE), (32, 64, 64, 1, 2, 2, None, None, None, AT, BT, DTYPE, DTYPE), (64, 32, 16, 1, 2, 2, None, None, None, AT, BT, DTYPE, DTYPE), (32, 64, 16, 1, 2, 2, None, None, None, AT, BT, DTYPE, DTYPE), (128, 32, 32, 1, 2, 2, None, None, None, AT, BT, DTYPE, DTYPE), (32, 128, 32, 1, 2, 2, None, None, None, AT, BT, DTYPE, DTYPE), # 4 warp (128, 64, 16, 1, 4, 2, None, None, None, AT, BT, DTYPE, DTYPE), (64, 128, 16, 1, 4, 2, None, None, None, AT, BT, DTYPE, DTYPE), (128, 32, 32, 1, 4, 2, None, None, None, AT, BT, DTYPE, DTYPE), (32, 128, 32, 1, 4, 2, None, None, None, AT, BT, DTYPE, DTYPE), (128, 32, 64, 1, 4, 2, None, None, None, AT, BT, DTYPE, DTYPE), (32, 128, 64, 1, 4, 2, None, None, None, AT, BT, DTYPE, DTYPE), # 8 warp (128, 256, 16, 1, 8, 2, None, None, None, AT, BT, DTYPE, DTYPE), (256, 128, 16, 1, 8, 2, None, None, None, AT, BT, DTYPE, DTYPE), (256, 128, 32, 1, 8, 2, None, None, None, AT, BT, DTYPE, DTYPE), # split-k (64, 64, 16, 2, 4, 2, None, None, None, AT, BT, DTYPE, DTYPE), (64, 64, 16, 4, 4, 2, None, None, None, AT, BT, DTYPE, DTYPE), (64, 64, 16, 8, 4, 2, None, None, None, AT, BT, DTYPE, DTYPE), # variable input (128, 128, 32, 1, 4, 2, 256, 384, 160, AT, BT, DTYPE, DTYPE), (128, 128, 32, 1, 4, 2, 107, 233, 256, AT, BT, DTYPE, DTYPE), (128, 128, 32, 1, 4, 2, 107, 233, 311, AT, BT, DTYPE, DTYPE), (128, 256, 64, 1, 8, 3, 256, 512, 160, AT, BT, DTYPE, DTYPE), ] for DTYPE in ["float16", "bfloat16", "float32"] for AT in [False, True] for BT in [False, True] ], # n-stage *[ [ (16, 16, 16, 1, 1, STAGES, 32, 32, 80, AT, BT, DTYPE, DTYPE), (64, 32, 64, 1, 2, STAGES, 128, 64, 128, AT, BT, DTYPE, DTYPE), (128, 64, 16, 1, 4, STAGES, 256, 128, 80, AT, BT, DTYPE, DTYPE), (256, 128, 32, 1, 8, STAGES, 512, 256, 160, AT, BT, DTYPE, DTYPE), (128, 128, 32, 1, 4, STAGES, 256, 256, 160, AT, BT, DTYPE, DTYPE), # split-k (64, 64, 16, 8, 4, STAGES, 128, 128, 768, AT, BT, DTYPE, DTYPE), (64, 64, 16, 8, 4, STAGES, 128, 128, 32, AT, BT, DTYPE, DTYPE), ] for DTYPE in ["float16", "bfloat16", "float32"] for AT in [False, True] for BT in [False, True] for STAGES in [4] ], # mixed-precision *[ [ (32, 32, 32, 1, 1, 2, None, None, None, AT, BT, ADTYPE, BDTYPE), (128, 256, 32, 1, 8, 2, None, None, None, AT, BT, ADTYPE, BDTYPE), (32, 64, 32, 1, 1, 2, 64, 128, 32, AT, BT, ADTYPE, BDTYPE), (128, 128, 32, 8, 4, 2, 256, 256, 128, AT, BT, ADTYPE, BDTYPE), ] for ADTYPE, BDTYPE in [("float8e4b15", "float8e5"), ("float8e4", "float16"), ("float16", "float8e5"), ("float16", "float32"), ("float32", "float16"), ("bfloat16", "float32"), ("float32", "bfloat16")] for AT in [False, True] for BT in [False, True] ] ), ) def test_op(BLOCK_M, BLOCK_N, BLOCK_K, SPLIT_K, NWARP, NSTAGE, M, N, K, AT, BT, ADTYPE, BDTYPE): capability = torch.cuda.get_device_capability() if capability[0] < 7: pytest.skip("Only test tl.dot() on devices with sm >= 70") if capability[0] < 8 and (ADTYPE == "bfloat16" or BDTYPE == "bfloat16"): pytest.skip("Only test bfloat16 on devices with sm >= 80") if (ADTYPE == "bfloat16" or BDTYPE == "bfloat16") and SPLIT_K != 1: pytest.skip("bfloat16 matmuls don't allow split_k for now") torch.manual_seed(0) # nuke kernel decorators -- will set meta-parameters manually kwargs = {'BLOCK_M': BLOCK_M, 'BLOCK_N': BLOCK_N, 'BLOCK_K': BLOCK_K, 'SPLIT_K': SPLIT_K} pre_hook = None if SPLIT_K == 1 else lambda nargs: nargs['C'].zero_() configs = [triton.Config(kwargs=kwargs, num_warps=NWARP, num_stages=NSTAGE, pre_hook=pre_hook)] kernel = triton.ops._matmul.kernel kernel.configs = configs # kernel.run = kernel.run.run.run # get matrix shape M = BLOCK_M if M is None else M N = BLOCK_N if N is None else N K = BLOCK_K * SPLIT_K if K is None else K a_fp8 = "float8" in ADTYPE b_fp8 = "float8" in BDTYPE def maybe_upcast(x, dtype, is_float8): if is_float8: return f8_to_f16(x, dtype) return x def init_input(n, m, t, dtype, is_float8): if t: return init_input(m, n, False, dtype, is_float8).t() if is_float8: return torch.randint(20, 60, (n, m), device="cuda", dtype=torch.int8) dtype = {"float16": torch.float16, "bfloat16": torch.bfloat16, "float32": torch.float32}[dtype] return .1 * torch.randn((n, m), device="cuda", dtype=dtype) # allocate/transpose inputs a = init_input(M, K, AT, ADTYPE, a_fp8) b = init_input(K, N, BT, BDTYPE, b_fp8) # run test th_a = maybe_upcast(a, ADTYPE, a_fp8).to(torch.float32) if AT and a_fp8: th_a = th_a.view(th_a.shape[::-1]).T th_b = maybe_upcast(b, BDTYPE, b_fp8).to(torch.float32) if BT and b_fp8: th_b = th_b.view(th_b.shape[::-1]).T th_c = torch.matmul(th_a, th_b) try: if a_fp8: a = triton.reinterpret(a, getattr(tl, ADTYPE)) if b_fp8: b = triton.reinterpret(b, getattr(tl, BDTYPE)) tt_c = triton.ops.matmul(a, b) atol, rtol = 1e-2, 0 if ADTYPE == torch.bfloat16 or BDTYPE == torch.bfloat16: atol, rtol = 3.5e-2, 0 torch.testing.assert_allclose(th_c, tt_c, atol=atol, rtol=rtol) except triton.OutOfResources as e: pytest.skip(str(e))
7,831
47.345679
127
py
triton
triton-main/python/test/unit/operators/test_flash_attention.py
import pytest import torch import triton import triton.ops @pytest.mark.parametrize('Z, H, N_CTX, D_HEAD', [(4, 48, 1024, 16), (4, 48, 1024, 32), (4, 48, 1024, 64), (4, 48, 1024, 128)]) @pytest.mark.parametrize('dtype', [torch.float16, torch.bfloat16]) @pytest.mark.parametrize('causal', [True, False]) @pytest.mark.parametrize('seq_par', [True, False]) def test_op(Z, H, N_CTX, D_HEAD, dtype, causal, seq_par): capability = torch.cuda.get_device_capability() if capability[0] < 8: pytest.skip("Flash attention only supported for compute capability < 80") torch.manual_seed(20) q = torch.empty((Z, H, N_CTX, D_HEAD), dtype=dtype, device="cuda").normal_(mean=0., std=0.5).requires_grad_() k = torch.empty((Z, H, N_CTX, D_HEAD), dtype=dtype, device="cuda").normal_(mean=0., std=0.5).requires_grad_() v = torch.empty((Z, H, N_CTX, D_HEAD), dtype=dtype, device="cuda").normal_(mean=0., std=0.5).requires_grad_() sm_scale = 0.5 dout = torch.randn_like(q) # reference implementation M = torch.tril(torch.ones((N_CTX, N_CTX), device="cuda")) p = torch.matmul(q, k.transpose(2, 3)) * sm_scale if causal: p[:, :, M == 0] = float("-inf") p = torch.softmax(p.float(), dim=-1).to(dtype) # p = torch.exp(p) ref_out = torch.matmul(p, v) ref_out.backward(dout) ref_dv, v.grad = v.grad.clone(), None ref_dk, k.grad = k.grad.clone(), None ref_dq, q.grad = q.grad.clone(), None # # triton implementation tri_out = triton.ops.attention(q, k, v, causal, sm_scale, seq_par) # print(ref_out) # print(tri_out) tri_out.backward(dout) tri_dv, v.grad = v.grad.clone(), None tri_dk, k.grad = k.grad.clone(), None tri_dq, q.grad = q.grad.clone(), None # compare atol = 1e-1 if dtype == torch.bfloat16 else 1e-2 torch.testing.assert_allclose(ref_out, tri_out, atol=atol, rtol=0) torch.testing.assert_allclose(ref_dv, tri_dv, atol=atol, rtol=0) torch.testing.assert_allclose(ref_dk, tri_dk, atol=atol, rtol=0) torch.testing.assert_allclose(ref_dq, tri_dq, atol=atol, rtol=0)
2,243
43
113
py
triton
triton-main/python/test/unit/language/test_core.py
# flake8: noqa: F821,F841 import itertools import os import re from typing import Optional, Union import numpy as np import pytest import torch from numpy.random import RandomState import triton import triton._C.libtriton.triton as _triton import triton.language as tl from triton.runtime.jit import JITFunction, TensorWrapper, reinterpret int_dtypes = ['int8', 'int16', 'int32', 'int64'] uint_dtypes = ['uint8', 'uint16', 'uint32', 'uint64'] float_dtypes = ['float16', 'float32', 'float64'] dtypes = int_dtypes + uint_dtypes + float_dtypes dtypes_with_bfloat16 = dtypes + ['bfloat16'] torch_dtypes = ['bool'] + int_dtypes + ['uint8'] + float_dtypes + ['bfloat16'] def _bitwidth(dtype: str) -> int: # ex.: "int64" -> 64 return int(re.search(r'(\d+)$', dtype).group(1)) def numpy_random(shape, dtype_str, rs: Optional[RandomState] = None, low=None, high=None): """ Override `rs` if you're calling this function twice and don't want the same result for both calls. """ if isinstance(shape, int): shape = (shape, ) if rs is None: rs = RandomState(seed=17) if dtype_str in int_dtypes + uint_dtypes: iinfo = np.iinfo(getattr(np, dtype_str)) low = iinfo.min if low is None else max(low, iinfo.min) high = iinfo.max if high is None else min(high, iinfo.max) dtype = getattr(np, dtype_str) x = rs.randint(low, high, shape, dtype=dtype) x[x == 0] = 1 # Hack. Never return zero so tests of division don't error out. return x elif dtype_str in float_dtypes: return rs.normal(0, 1, shape).astype(dtype_str) elif dtype_str == 'bfloat16': return (rs.normal(0, 1, shape).astype('float32').view('uint32') & np.uint32(0xffff0000)).view('float32') elif dtype_str in ['bool', 'int1', 'bool_']: return rs.normal(0, 1, shape) > 0.0 else: raise RuntimeError(f'Unknown dtype {dtype_str}') def to_triton(x: np.ndarray, device='cuda', dst_type=None) -> Union[TensorWrapper, torch.Tensor]: ''' Note: We need dst_type because the type of x can be different from dst_type. For example: x is of type `float32`, dst_type is `bfloat16`. If dst_type is None, we infer dst_type from x. ''' t = x.dtype.name if t in uint_dtypes: signed_type_name = t.lstrip('u') # e.g. "uint16" -> "int16" x_signed = x.astype(getattr(np, signed_type_name)) return reinterpret(torch.tensor(x_signed, device=device), getattr(tl, t)) else: if t == 'float32' and dst_type == 'bfloat16': return torch.tensor(x, device=device).bfloat16() return torch.tensor(x, device=device) def torch_dtype_name(dtype) -> str: if isinstance(dtype, triton.language.dtype): return dtype.name elif isinstance(dtype, torch.dtype): # 'torch.int64' -> 'int64' m = re.match(r'^torch\.(\w+)$', str(dtype)) return m.group(1) else: raise TypeError(f'not a triton or torch dtype: {type(dtype)}') def to_numpy(x): if isinstance(x, TensorWrapper): return x.base.cpu().numpy().astype(getattr(np, torch_dtype_name(x.dtype))) elif isinstance(x, torch.Tensor): if x.dtype is torch.bfloat16: return x.cpu().float().numpy() return x.cpu().numpy() else: raise ValueError(f"Not a triton-compatible tensor: {x}") def patch_kernel(template, to_replace): kernel = triton.JITFunction(template.fn) for key, value in to_replace.items(): kernel.src = kernel.src.replace(key, value) return kernel def check_cuda_only(device): if device not in ['cuda']: pytest.skip("Only for cuda") def check_type_supported(dtype, device): ''' skip test if dtype is not supported on the current device ''' if device in ['cuda']: cc = torch.cuda.get_device_capability() if cc[0] < 8 and (dtype is tl.bfloat16 or dtype == "bfloat16" or dtype is torch.bfloat16): pytest.skip("bfloat16 is only supported on NVGPU with cc >= 80") class MmaLayout: def __init__(self, version, warps_per_cta): self.version = version self.warps_per_cta = warps_per_cta def __str__(self): return f"#triton_gpu.mma<{{versionMajor={self.version[0]}, versionMinor={self.version[1]}, warpsPerCTA={self.warps_per_cta}}}>" class BlockedLayout: def __init__(self, size_per_thread, threads_per_warp, warps_per_cta, order): self.sz_per_thread = size_per_thread self.threads_per_warp = threads_per_warp self.warps_per_cta = warps_per_cta self.order = order def __str__(self): return f"#triton_gpu.blocked<{{sizePerThread={self.sz_per_thread}, threadsPerWarp={self.threads_per_warp}, warpsPerCTA={self.warps_per_cta}, order={self.order}}}>" class SharedLayout: def __init__(self, vec, per_phase, max_phase, order): self.vec = str(vec) self.per_phase = str(per_phase) self.max_phase = str(max_phase) self.order = str(order) def __str__(self): return f"#triton_gpu.shared<{{vec={self.vec}, perPhase={self.per_phase}, maxPhase={self.max_phase}, order={self.order}}}>" @pytest.mark.parametrize("dtype_x", list(dtypes) + ["bfloat16"]) def test_empty_kernel(dtype_x, device): SIZE = 128 @triton.jit def kernel(X, SIZE: tl.constexpr): pass check_type_supported(dtype_x, device) x = to_triton(numpy_random(SIZE, dtype_str=dtype_x), device=device, dst_type=dtype_x) kernel[(1, )](x, SIZE=SIZE, num_warps=4) # generic test functions def _test_unary(dtype_x, expr, numpy_expr=None, device='cuda'): check_type_supported(dtype_x, device) # early return if dtype_x is not supported SIZE = 128 # define the kernel / launch-grid @triton.jit def kernel(Z, X, SIZE: tl.constexpr): off = tl.arange(0, SIZE) x = tl.load(X + off) z = GENERATE_TEST_HERE tl.store(Z + off, z) kernel = patch_kernel(kernel, {'GENERATE_TEST_HERE': expr}) # inputs x = numpy_random(SIZE, dtype_str=dtype_x) if 'log' in expr: x = np.abs(x) + 0.01 # reference result z_ref = eval(expr if numpy_expr is None else numpy_expr) # triton result x_tri = to_triton(x, device=device, dst_type=dtype_x) z_tri = to_triton(np.empty_like(z_ref), device=device, dst_type=dtype_x) kernel[(1, )](z_tri, x_tri, SIZE=SIZE, num_warps=4) # compare np.testing.assert_allclose(z_ref, to_numpy(z_tri), rtol=0.01) def _binary_op_dtype_override(a: str, b: str) -> Optional[np.dtype]: """ Given two dtype strings, returns the numpy dtype Triton thinks binary operations on the two types should return. Returns None if the return value matches numpy. This is generally needed because Triton and pytorch return narrower floating point types than numpy in mixed operations, and because Triton follows C/C++ semantics around mixed signed/unsigned operations, and numpy/pytorch do not. """ overrides = { ('float16', 'int16'): np.float16, ('float16', 'int32'): np.float16, ('float16', 'int64'): np.float16, ('float16', 'uint16'): np.float16, ('float16', 'uint32'): np.float16, ('float16', 'uint64'): np.float16, ('int8', 'uint8'): np.uint8, ('int8', 'uint16'): np.uint16, ('int8', 'uint32'): np.uint32, ('int8', 'uint64'): np.uint64, ('int16', 'uint16'): np.uint16, ('int16', 'uint32'): np.uint32, ('int16', 'uint64'): np.uint64, ('int32', 'uint32'): np.uint32, ('int32', 'uint64'): np.uint64, ('int64', 'uint64'): np.uint64, } key = (a, b) if a < b else (b, a) return overrides.get(key) def _test_binary(dtype_x, dtype_y, expr, numpy_expr=None, mode_x='real', mode_y='real', device='cuda', y_low=None, y_high=None): check_type_supported(dtype_x, device) # early return if dtype_x is not supported check_type_supported(dtype_y, device) SIZE = 128 # define the kernel / launch-grid @triton.jit def kernel(Z, X, Y, SIZE: tl.constexpr): off = tl.arange(0, SIZE) x = tl.load(X + off) y = tl.load(Y + off) z = GENERATE_TEST_HERE tl.store(Z + off, z) kernel = patch_kernel(kernel, {'GENERATE_TEST_HERE': expr}) # inputs rs = RandomState(17) x = numpy_random(SIZE, dtype_str=dtype_x, rs=rs) y = numpy_random(SIZE, dtype_str=dtype_y, rs=rs, low=y_low, high=y_high) if mode_x == 'nan': x[:] = float('nan') if mode_y == 'nan': y[:] = float('nan') # reference result z_ref = eval(expr if numpy_expr is None else numpy_expr) dtype_z = _binary_op_dtype_override(dtype_x, dtype_y) if dtype_z is not None: z_ref = z_ref.astype(dtype_z) # triton result x_tri = to_triton(x, device=device, dst_type=dtype_x) y_tri = to_triton(y, device=device, dst_type=dtype_y) z_tri = to_triton(np.empty(SIZE, dtype=z_ref.dtype), device=device) kernel[(1, )](z_tri, x_tri, y_tri, SIZE=SIZE, num_warps=4) np.testing.assert_allclose(z_ref, to_numpy(z_tri), err_msg=expr, rtol=0.01) def _mod_operation_ill_conditioned(dtype_x, dtype_y) -> bool: # The result of x % y is ill-conditioned if x % y is much smaller than x. # pytorch/CUDA has slightly different (probably better) rounding on # remainders than stock LLVM. We currently don't expect to match it # bit-for-bit. return (dtype_x, dtype_y) in [ ('int32', 'bfloat16'), ('int32', 'float16'), ('int32', 'float32'), ('int64', 'bfloat16'), ('int64', 'float16'), ('int64', 'float32'), ('int64', 'float64'), ('uint16', 'bfloat16'), ('uint16', 'float16'), ('uint16', 'float32'), ('uint32', 'bfloat16'), ('uint32', 'float16'), ('uint32', 'float32'), ('uint64', 'bfloat16'), ('uint64', 'float16'), ('uint64', 'float32'), ('uint64', 'float64'), ] # --------------- # test binary ops # --------------- @pytest.mark.parametrize("dtype_x, dtype_y, op", [ (dtype_x, dtype_y, op) for op in ['+', '-', '*', '/', '%'] for dtype_x in dtypes_with_bfloat16 for dtype_y in dtypes_with_bfloat16 ]) def test_bin_op(dtype_x, dtype_y, op, device): expr = f' x {op} y' if op == '%' and dtype_x in int_dtypes + uint_dtypes and dtype_y in int_dtypes + uint_dtypes: # LLVM has 'numpy.fmod', not 'numpy.remainder', semantics on integer remainders. numpy_expr = 'np.fmod(x, y)' elif op in ('/', '%') and dtype_x in ('int16', 'float16', 'bfloat16') and dtype_y in ('int16', 'float16', 'bfloat16'): # Triton promotes 16-bit floating-point / and % to 32-bit because there # are no native div or FRem operations on float16. Since we have to # convert anyway, we may as well take the accuracy bump. numpy_expr = f'x.astype(np.float32) {op} y.astype(np.float32)' elif (dtype_x in uint_dtypes and dtype_y in int_dtypes and _bitwidth(dtype_x) >= _bitwidth(dtype_y)): numpy_expr = f'x.astype(np.{dtype_x}) {op} y.astype(np.{dtype_x})' elif (dtype_y in uint_dtypes and dtype_x in int_dtypes and _bitwidth(dtype_y) >= _bitwidth(dtype_x)): numpy_expr = f'x.astype(np.{dtype_y}) {op} y.astype(np.{dtype_y})' else: numpy_expr = None if op == '%' and _mod_operation_ill_conditioned(dtype_x, dtype_y): with pytest.raises(AssertionError, match='Not equal to tolerance'): _test_binary(dtype_x, dtype_y, expr, numpy_expr, device=device) elif (op in ('%', '/') and ((dtype_x in int_dtypes and dtype_y in uint_dtypes) or (dtype_x in uint_dtypes and dtype_y in int_dtypes))): with pytest.raises(triton.CompilationError) as exc_info: _test_binary(dtype_x, dtype_y, expr, numpy_expr, device=device) assert re.match('Cannot use .* because they have different signedness', str(exc_info.value.__cause__)) else: _test_binary(dtype_x, dtype_y, expr, numpy_expr, device=device) @pytest.mark.parametrize("dtype_x, dtype_y", [(dtype_x, dtype_y) for dtype_x in int_dtypes for dtype_y in int_dtypes] + [(dtype_x, dtype_y) for dtype_x in uint_dtypes for dtype_y in uint_dtypes] ) def test_floordiv(dtype_x, dtype_y, device): # Triton has IEEE, not numpy/torch, semantics for %, and those carry # through to //, so we have to use a nonstandard expression to get a # reference result for //. expr = 'x // y' numpy_expr = '((x - np.fmod(x, y)) / y)' _test_binary(dtype_x, dtype_y, expr, numpy_expr, device=device) def test_unsigned_name_mangling(device): # Test that uint32 and int32 are mangled differently by the compiler SIZE = 128 # define the kernel / launch-grid @triton.jit def kernel(O1, O2, X, Y, SIZE: tl.constexpr): off = tl.arange(0, SIZE) x = tl.load(X + off) y = tl.load(Y + off) out1 = tl.abs(x) # uint32 -> nop out2 = tl.abs(-y) # int32 -> should have an effect tl.store(O1 + off, out1) tl.store(O2 + off, out2) dtype_x = 'uint32' dtype_y = 'int32' # inputs rs = RandomState(17) x = numpy_random(SIZE, dtype_str=dtype_x, rs=rs) y = numpy_random(SIZE, dtype_str=dtype_y, rs=rs) # reference result expect = (np.abs(x), np.abs(-y)) # triton result x_tri = to_triton(x, device=device, dst_type=dtype_x) y_tri = to_triton(y, device=device, dst_type=dtype_y) actual = tuple( to_triton(np.empty_like(e), device=device) for e in expect ) kernel[(1, )](actual[0], actual[1], x_tri, y_tri, SIZE=SIZE, num_warps=4) # Bitwise op, so expect exact equality assert (expect[0] == to_numpy(actual[0])).all() assert (expect[1] == to_numpy(actual[1])).all() # --------------- # test bitwise ops # --------------- @pytest.mark.parametrize("dtype_x, dtype_y, op", [ (dtype_x, dtype_y, op) for op in ['&', '|', '^'] for dtype_x in dtypes + dtypes_with_bfloat16 for dtype_y in dtypes + dtypes_with_bfloat16 ]) def test_bitwise_op(dtype_x, dtype_y, op, device): expr = f'x {op} y' if (dtype_x in uint_dtypes and dtype_y in int_dtypes and _bitwidth(dtype_x) >= _bitwidth(dtype_y)): numpy_expr = f'x.astype(np.{dtype_x}) {op} y.astype(np.{dtype_x})' elif (dtype_y in uint_dtypes and dtype_x in int_dtypes and _bitwidth(dtype_y) >= _bitwidth(dtype_x)): numpy_expr = f'x.astype(np.{dtype_y}) {op} y.astype(np.{dtype_y})' else: numpy_expr = None if 'float' in dtype_x + dtype_y: with pytest.raises(triton.CompilationError) as exc_info: _test_binary(dtype_x, dtype_y, expr, numpy_expr='np.array([])', device=device) # The CompilationError must have been caused by a C++ exception with this text. assert re.match('invalid operands of type', str(exc_info.value.__cause__)) else: _test_binary(dtype_x, dtype_y, expr, numpy_expr, device=device) @pytest.mark.parametrize("dtype_x, dtype_y, op", [ (dtype_x, dtype_y, op) for op in ['<<', '>>'] for dtype_x in int_dtypes + uint_dtypes for dtype_y in int_dtypes + uint_dtypes ]) def test_shift_op(dtype_x, dtype_y, op, device): expr = f'x {op} y' bw = max(_bitwidth(dtype_x), _bitwidth(dtype_y)) if dtype_x.startswith('int'): dtype_z = f'int{bw}' else: dtype_z = f'uint{bw}' numpy_expr = f'x.astype(np.{dtype_z}) {op} y.astype(np.{dtype_z})' _test_binary(dtype_x, dtype_y, expr, numpy_expr, device=device, y_low=0, y_high=65) # --------------- # test compare ops # --------------- ops = ['==', '!=', '>', '<', '>=', '<='] @pytest.mark.parametrize("dtype_x, dtype_y, op, mode_x, mode_y", # real [ (dtype_x, dtype_y, op, 'real', 'real') for op in ops for dtype_x in dtypes for dtype_y in dtypes ] + # NaNs [('float32', 'float32', op, mode_x, mode_y) for op in ops for mode_x, mode_y in [('nan', 'real'), ('real', 'nan'), ('nan', 'nan')] ]) def test_compare_op(dtype_x, dtype_y, op, mode_x, mode_y, device): expr = f'x {op} y' if (dtype_x in uint_dtypes and dtype_y in int_dtypes and _bitwidth(dtype_x) >= _bitwidth(dtype_y)): numpy_expr = f'x.astype(np.{dtype_x}) {op} y.astype(np.{dtype_x})' elif (dtype_y in uint_dtypes and dtype_x in int_dtypes and _bitwidth(dtype_y) >= _bitwidth(dtype_x)): numpy_expr = f'x.astype(np.{dtype_y}) {op} y.astype(np.{dtype_y})' else: numpy_expr = None _test_binary(dtype_x, dtype_y, expr, numpy_expr, mode_x=mode_x, mode_y=mode_y, device=device) # --------------- # test broadcast # --------------- @pytest.mark.parametrize("dtype", dtypes_with_bfloat16) def test_broadcast(dtype, device): @triton.jit def broadcast_kernel(x_ptr, y_ptr, y_broadcasted_ptr, M: tl.constexpr, N: tl.constexpr): offset1 = tl.arange(0, M) offset2 = tl.arange(0, N) x = tl.load(x_ptr + N * offset1[:, None] + offset2[None, :]) y = tl.load(y_ptr + offset2) _, y_broadcasted = tl.broadcast(x, y) tl.store(y_broadcasted_ptr + N * offset1[:, None] + offset2[None, :], y_broadcasted) M = 32 N = 64 rs = RandomState(17) x = numpy_random((M, N), dtype_str=dtype, rs=rs) y = numpy_random(N, dtype_str=dtype, rs=rs) _, y_broadcasted_np = np.broadcast_arrays(x, y) x_tri = to_triton(x, device=device, dst_type=dtype) y_tri = to_triton(y, device=device, dst_type=dtype) y_broadcasted_tri = to_triton(np.empty((M, N), dtype=y_broadcasted_np.dtype), device=device, dst_type=dtype) broadcast_kernel[(1,)](x_tri, y_tri, y_broadcasted_tri, M=M, N=N) assert (y_broadcasted_np == to_numpy(y_broadcasted_tri)).all() # ------------------ # test invalid slice # ------------------ def test_invalid_slice(device): dst = torch.empty(128, device=device) @triton.jit def _kernel(dst): dst[10:] with pytest.raises(triton.CompilationError, match='unsupported tensor index'): _kernel[(1,)](dst=dst) # ---------------- # test expand_dims # ---------------- def test_expand_dims(device): @triton.jit def expand_dims_kernel(dummy, N: tl.constexpr): offset1 = tl.arange(0, N) t = tl.expand_dims(offset1, 0) tl.static_assert(t.shape == [1, N]) t = tl.expand_dims(offset1, 1) tl.static_assert(t.shape == [N, 1]) t = tl.expand_dims(offset1, -1) tl.static_assert(t.shape == [N, 1]) t = tl.expand_dims(offset1, -2) tl.static_assert(t.shape == [1, N]) t = tl.expand_dims(offset1, (0, -1)) tl.static_assert(t.shape == [1, N, 1]) t = tl.expand_dims(offset1, (0, 1, 3)) tl.static_assert(t.shape == [1, 1, N, 1]) t = tl.expand_dims(offset1, (-4, 2, -1)) tl.static_assert(t.shape == [1, N, 1, 1]) t = tl.expand_dims(offset1, (3, 1, 2)) tl.static_assert(t.shape == [N, 1, 1, 1]) N = 32 dummy_tensor = torch.empty((), device=device) expand_dims_kernel[(1,)](dummy_tensor, N) def test_expand_dims_error_cases(device): @triton.jit def dim_out_of_range1(dummy, N: tl.constexpr): offset1 = tl.arange(0, N) t = tl.expand_dims(offset1, -2) t = tl.expand_dims(offset1, -3) @triton.jit def dim_out_of_range2(dummy, N: tl.constexpr): offset1 = tl.arange(0, N) t = tl.expand_dims(offset1, 1) t = tl.expand_dims(offset1, 2) @triton.jit def duplicate_dim1(dummy, N: tl.constexpr): offset1 = tl.arange(0, N) t = tl.expand_dims(offset1, (0, 0)) @triton.jit def duplicate_dim2(dummy, N: tl.constexpr): offset1 = tl.arange(0, N) t = tl.expand_dims(offset1, (0, -3)) N = 32 dummy_tensor = torch.empty((), device=device) with pytest.raises(triton.CompilationError, match="invalid axis -3"): dim_out_of_range1[(1,)](dummy_tensor, N) with pytest.raises(triton.CompilationError, match="invalid axis 2"): dim_out_of_range2[(1,)](dummy_tensor, N) with pytest.raises(triton.CompilationError, match=r"duplicate axes, normalized axes = \[0, 0\]"): duplicate_dim1[(1,)](dummy_tensor, N) with pytest.raises(triton.CompilationError, match=r"duplicate axes, normalized axes = \[0, 0\]"): duplicate_dim2[(1,)](dummy_tensor, N) # ---------------------------- # test invalid program id axis # ---------------------------- def test_invalid_pid_axis(device): dst = torch.empty(128, device=device) @triton.jit def _kernel(dst): pid = tl.program_id(20) with pytest.raises(triton.CompilationError, match=r"program_id must be in \[0,3\]"): _kernel[(1,)](dst) # --------------- # test where # --------------- @pytest.mark.parametrize("dtype", dtypes_with_bfloat16 + ["*int32"]) def test_where(dtype, device): select_ptrs = False if dtype == "*int32": dtype = "int64" select_ptrs = True check_type_supported(dtype, device) @triton.jit def where_kernel(cond_ptr, a_ptr, b_ptr, output_ptr, n_elements, BLOCK_SIZE: tl.constexpr, TEST_POINTERS: tl.constexpr, TEST_SCALAR_POINTERS: tl.constexpr): offsets = tl.program_id(axis=0) * BLOCK_SIZE + tl.arange(0, BLOCK_SIZE) mask = offsets < n_elements decide = tl.load(cond_ptr + offsets, mask=mask) if TEST_SCALAR_POINTERS: ptr = tl.where(tl.load(cond_ptr), a_ptr, b_ptr) output = tl.load(ptr + offsets, mask=mask) else: if TEST_POINTERS: a = tl.load(a_ptr + offsets, mask=mask).to(tl.pi32_t) b = tl.load(b_ptr + offsets, mask=mask).to(tl.pi32_t) else: a = tl.load(a_ptr + offsets, mask=mask) b = tl.load(b_ptr + offsets, mask=mask) output = tl.where(decide, a, b) tl.store(output_ptr + offsets, output, mask=mask) SIZE = 1_000 rs = RandomState(17) cond = numpy_random(SIZE, 'bool', rs) x = numpy_random(SIZE, dtype_str=dtype, rs=rs) y = numpy_random(SIZE, dtype_str=dtype, rs=rs) z = np.where(cond, x, y) cond_tri = to_triton(cond, device=device) x_tri = to_triton(x, device=device, dst_type=dtype) y_tri = to_triton(y, device=device, dst_type=dtype) z_tri = to_triton(np.empty(SIZE, dtype=z.dtype), device=device, dst_type=dtype) grid = lambda meta: (triton.cdiv(SIZE, meta['BLOCK_SIZE']),) where_kernel[grid](cond_tri, x_tri, y_tri, z_tri, SIZE, BLOCK_SIZE=1024, TEST_POINTERS=select_ptrs, TEST_SCALAR_POINTERS=False) assert (z == to_numpy(z_tri)).all() if select_ptrs: where_kernel[grid](cond_tri, x_tri, y_tri, z_tri, SIZE, BLOCK_SIZE=1024, TEST_POINTERS=select_ptrs, TEST_SCALAR_POINTERS=True) z = np.where(cond[0], x, y) assert (z == to_numpy(z_tri)).all() def test_where_broadcast(device): @triton.jit def where_kernel(cond_ptr, a_ptr, out_ptr, BLOCK_SIZE: tl.constexpr): xoffsets = tl.arange(0, BLOCK_SIZE)[:, None] yoffsets = tl.arange(0, BLOCK_SIZE)[None, :] mask = tl.load(cond_ptr + yoffsets) vals = tl.load(a_ptr + yoffsets + BLOCK_SIZE * xoffsets) res = tl.where(mask, vals, 0.) tl.store(out_ptr + yoffsets + BLOCK_SIZE * xoffsets, res) @triton.jit def where_scalar_condition(a_ptr, out_ptr, BLOCK_SIZE: tl.constexpr): xoffsets = tl.arange(0, BLOCK_SIZE)[:, None] yoffsets = tl.arange(0, BLOCK_SIZE)[None, :] mask = 0 vals = tl.load(a_ptr + yoffsets + BLOCK_SIZE * xoffsets) res = tl.where(mask, vals, 0.) tl.store(out_ptr + yoffsets + BLOCK_SIZE * xoffsets, res) SIZE = 32 dtype = 'float32' rs = RandomState(17) x = numpy_random((SIZE, SIZE), dtype_str=dtype, rs=rs) mask = numpy_random(SIZE, 'bool', rs=rs) z = np.where(mask, x, 0) cond_tri = to_triton(mask, device=device) x_tri = to_triton(x, device=device, dst_type=dtype) z_tri = to_triton(np.empty((SIZE, SIZE), dtype=z.dtype), device=device, dst_type=dtype) where_kernel[(1,)](cond_tri, x_tri, z_tri, SIZE) assert (z == to_numpy(z_tri)).all() where_scalar_condition[(1,)](x_tri, z_tri, SIZE) z = np.where(0, x, 0) assert (z == to_numpy(z_tri)).all() # --------------- # test unary ops # --------------- @pytest.mark.parametrize("dtype_x, expr", [ (dtype_x, ' -x') for dtype_x in dtypes_with_bfloat16 ] + [ (dtype_x, ' ~x') for dtype_x in int_dtypes ]) def test_unary_op(dtype_x, expr, device): _test_unary(dtype_x, expr, device=device) # ---------------- # test math ops # ---------------- @pytest.mark.parametrize("dtype_x, expr", [(dtype_x, expr) for dtype_x in ["float32", "float64"] for expr in ['exp', 'log', 'cos', 'sin']]) def test_math_op(dtype_x, expr, device): _test_unary(dtype_x, f'tl.{expr}(x)', f'np.{expr}(x) ', device=device) # ---------------- # test abs # ---------------- @pytest.mark.parametrize("dtype_x", [ (dtype_x) for dtype_x in dtypes_with_bfloat16 ]) def test_abs(dtype_x, device): _test_unary(dtype_x, 'tl.abs(x)', 'np.abs(x) ', device=device) @pytest.mark.parametrize("in_dtype", [tl.float8e4b15, tl.float8e4, tl.float8e5]) def test_abs_fp8(in_dtype, device): @triton.jit def abs_kernel(X, Z, SIZE: tl.constexpr): off = tl.arange(0, SIZE) x = tl.load(X + off) z = tl.abs(x) tl.store(Z + off, z) f8_tensor = torch.tensor(range(-128, 128), dtype=torch.int8, device=device) # f32_to_f8 doesn't handle nan, so we make sure f8_tensor doesn't contain any nan all_exp_ones = (f8_tensor & 0b01111100) == 128 - 2**in_dtype.fp_mantissa_width f8_tensor[all_exp_ones] = 0 f8 = triton.reinterpret(f8_tensor, in_dtype) n_elements = f8_tensor.numel() out_f8 = torch.empty_like(f8_tensor) abs_kernel[(1,)](f8, triton.reinterpret(out_f8, in_dtype), n_elements) f32_tensor = convert_float_to_float32(f8_tensor, in_dtype) expect = f32_tensor.abs() actual_f8 = convert_float_to_float32(out_f8, in_dtype) torch.testing.assert_allclose(actual_f8, expect) # ---------------- # test indexing # ---------------- def make_ptr_str(name, shape): rank = len(shape) offsets = [] stride = 1 for i in reversed(range(rank)): idx = ', '.join([':' if ii == i else 'None' for ii in range(rank)]) offsets += [f'tl.arange(0, {shape[i]})[{idx}]*{stride}'] stride *= shape[i] return f"{name} + {' + '.join(offsets)}" # TODO: handle `%4 = triton_gpu.convert_layout %3 : (tensor<32xi32, #blocked0>) -> tensor<32xi32, #triton_gpu.slice<{dim = 0, parent = #blocked1}>>`` @pytest.mark.parametrize("expr, dtype_str", [ (f'x[{s}]', d) for s in ['None, :', ':, None', 'None, :, :', ':, :, None'] for d in ['int32', 'uint32', 'uint16'] ]) def test_index1d(expr, dtype_str, device): rank_x = expr.count(':') rank_y = expr.count(',') + 1 shape_x = [32 for _ in range(rank_x)] shape_z = [32 for _ in range(rank_y)] shape_z_rank_mismatch = [32 for _ in range(rank_y + 1)] shape_z_dim_mismatch = [64 for _ in range(rank_y)] # Triton kernel @triton.jit def kernel(Z, X, SIZE: tl.constexpr): m = tl.arange(0, SIZE) n = tl.arange(0, SIZE) x = tl.load(X_PTR_EXPR) z = GENERATE_TEST_HERE tl.store(Z_PTR_EXPR, z) def generate_kernel(shape_x, shape_z): to_replace = { 'X_PTR_EXPR': make_ptr_str('X', shape_x), 'Z_PTR_EXPR': make_ptr_str('Z', shape_z), 'GENERATE_TEST_HERE': expr, } return patch_kernel(kernel, to_replace) kernel_match = generate_kernel(shape_x, shape_z) kernel_dim_mismatch = generate_kernel(shape_x, shape_z_dim_mismatch) kernel_rank_mismatch = generate_kernel(shape_x, shape_z_rank_mismatch) # torch result x = numpy_random(shape_x, dtype_str=dtype_str) y = np.zeros(shape_z, dtype=getattr(np, dtype_str)) z_ref = eval(expr) + y # triton result z_tri = to_triton(np.empty_like(z_ref), device=device) x_tri = to_triton(x, device=device) kernel_match[(1, )](z_tri, x_tri, num_warps=1, SIZE=shape_x[0]) # compare assert (z_ref == to_numpy(z_tri)).all() def catch_compilation_error(kernel): try: kernel[(1, )](z_tri, x_tri, num_warps=1, SIZE=shape_x[0]) except triton.CompilationError as e: np.testing.assert_(True) except BaseException: np.testing.assert_(False) catch_compilation_error(kernel_dim_mismatch) catch_compilation_error(kernel_rank_mismatch) # --------------- # test tuples # --------------- @triton.jit def tuples_fn(a, b): return a + b, \ a - b, \ a * b def test_tuples(device): @triton.jit def with_fn(X, Y, A, B, C): x = tl.load(X) y = tl.load(Y) a, b, c = tuples_fn(x, y) tl.store(A, a) tl.store(B, b) tl.store(C, c) @triton.jit def without_fn(X, Y, A, B, C): x = tl.load(X) y = tl.load(Y) a, b, c = x + y, x - y, x * y tl.store(A, a) tl.store(B, b) tl.store(C, c) x = torch.tensor([1.3], device=device, dtype=torch.float32) y = torch.tensor([1.9], device=device, dtype=torch.float32) a_tri = torch.tensor([0], device=device, dtype=torch.float32) b_tri = torch.tensor([0], device=device, dtype=torch.float32) c_tri = torch.tensor([0], device=device, dtype=torch.float32) for kernel in [with_fn, without_fn]: kernel[(1, )](x, y, a_tri, b_tri, c_tri, num_warps=1) a_ref, b_ref, c_ref = x + y, x - y, x * y assert a_tri == a_ref assert b_tri == b_ref assert c_tri == c_ref @triton.jit(noinline=True) def noinline_simple_fn(x, y, Z): z = x + y tl.store(Z, z) @triton.jit(noinline=True) def noinline_call_graph_fn1(x): return x + 1 @triton.jit(noinline=True) def noinline_call_graph_fn2(y): return y + 2 @triton.jit(noinline=True) def noinline_call_graph_fn(x, y, Z): t0 = noinline_call_graph_fn1(x) t1 = noinline_call_graph_fn2(y) z = t0 + t1 tl.store(Z, z) @triton.jit(noinline=True) def noinline_shared_fn(x, y, Z): offs = tl.arange(0, 16)[:, None] * 16 + tl.arange(0, 16)[None, :] z = tl.load(Z + offs) z = tl.dot(z, z) + x + y tl.store(Z + offs, z) @triton.jit(noinline=True) def noinline_dynamic_fn(x, y, Z): if x >= 1: x = noinline_call_graph_fn1(x) else: x = noinline_call_graph_fn2(x) if y >= 2: y = noinline_call_graph_fn2(y) else: y = noinline_call_graph_fn1(y) z = x + y tl.store(Z, z) @triton.jit(noinline=True) def noinline_call_multi_values_fn(x, y): return x + 1, y + 2 @triton.jit(noinline=True) def noinline_multi_values_fn(x, y, Z): x, y = noinline_call_multi_values_fn(x, y) z = x + y tl.store(Z, z) @pytest.mark.parametrize("mode", ["simple", "call_graph", "shared", "dynamic", "multi_values"]) def test_noinline(mode, device): @triton.jit def kernel(X, Y, Z): x = tl.load(X) y = tl.load(Y) GENERATE_TEST_HERE(x, y, Z) func_name = f'noinline_{mode}_fn' kernel = patch_kernel(kernel, {'GENERATE_TEST_HERE': func_name}) x = torch.tensor([1.0], device=device, dtype=torch.float32) y = torch.tensor([2.0], device=device, dtype=torch.float32) if mode == "shared": z = torch.ones((16, 16), device=device, dtype=torch.float32) else: z = torch.tensor([0.0], device=device, dtype=torch.float32) kernel[(1,)](x, y, z, num_warps=1) if mode == "simple": assert torch.equal(z, x + y) elif mode == "call_graph" or mode == "dynamic" or mode == "multi_values": assert torch.equal(z, x + 1 + y + 2) elif mode == "shared": ref = torch.full((16, 16), 16, device=device, dtype=torch.float32) assert torch.equal(z, ref + x + y) # --------------- # test atomics # --------------- @pytest.mark.parametrize("op, dtype_x_str, mode, sem", itertools.chain.from_iterable([ [ ('add', 'float16', mode, sem), ('add', 'uint32', mode, sem), ('add', 'int32', mode, sem), ('add', 'float32', mode, sem), ('max', 'uint32', mode, sem), ('max', 'int32', mode, sem), ('max', 'float32', mode, sem), ('min', 'uint32', mode, sem), ('min', 'int32', mode, sem), ('min', 'float32', mode, sem), ] for mode in ['all_neg', 'all_pos', 'min_neg', 'max_pos'] for sem in [None, 'acquire', 'release', 'acq_rel', 'relaxed']])) def test_atomic_rmw(op, dtype_x_str, mode, sem, device): check_cuda_only(device) capability = torch.cuda.get_device_capability() if capability[0] < 7: if dtype_x_str == 'float16': pytest.skip("Only test atomic float16 ops on devices with sm >= 70") n_programs = 5 # triton kernel @triton.jit def kernel(X, Z): pid = tl.program_id(0) x = tl.load(X + pid) old = GENERATE_TEST_HERE sem_arg = sem if sem is None else f'"{sem}"' kernel = patch_kernel(kernel, {'GENERATE_TEST_HERE': f'tl.atomic_{op}(Z, x, sem={sem_arg})'}) numpy_op = {'add': np.sum, 'max': np.max, 'min': np.min}[op] max_neutral = float('-inf') if dtype_x_str in float_dtypes else np.iinfo(getattr(np, dtype_x_str)).min min_neutral = float('inf') if dtype_x_str in float_dtypes else np.iinfo(getattr(np, dtype_x_str)).max neutral = {'add': 0, 'max': max_neutral, 'min': min_neutral}[op] # triton result rs = RandomState(17) x = np.array([2**i for i in range(n_programs)], dtype=getattr(np, dtype_x_str)) if mode == 'all_neg': x = -np.abs(x) if mode == 'all_pos': x = np.abs(x) if mode == 'min_neg': idx = rs.randint(n_programs, size=(1, )).item() x[idx] = -np.max(np.abs(x)) - 1 if mode == 'max_pos': idx = rs.randint(n_programs, size=(1, )).item() x[idx] = np.max(np.abs(x)) + 1 x_tri = to_triton(x, device=device) z_tri = to_triton(np.array([neutral], dtype=getattr(np, dtype_x_str)), device=device) h = kernel[(n_programs, )](x_tri, z_tri) # torch result z_ref = numpy_op(x).astype(getattr(np, dtype_x_str)) # compare exact = op not in ['add'] if exact: assert z_ref.item() == to_numpy(z_tri).item() else: np.testing.assert_allclose(z_ref, to_numpy(z_tri), rtol=0.01) sem_str = "acq_rel" if sem is None else sem assert f"atom.global.gpu.{sem_str}" in h.asm["ptx"] def test_atomic_rmw_predicate(device): @triton.jit def kernel(X): val = tl.program_id(0) if val < 64: tl.atomic_max(X, val) x = torch.zeros((1,), device=device, dtype=torch.int32) kernel[(4096,)](x) assert x.item() == 63 @pytest.mark.parametrize("shape, axis", [(shape, axis) for shape in [(2, 2), (2, 8), (8, 2), (8, 8), (32, 32)] for axis in [0, 1]]) def test_tensor_atomic_rmw(shape, axis, device): shape0, shape1 = shape # triton kernel @triton.jit def kernel(Z, X, AXIS: tl.constexpr, SHAPE0: tl.constexpr, SHAPE1: tl.constexpr): off0 = tl.arange(0, SHAPE0) off1 = tl.arange(0, SHAPE1) x = tl.load(X + off0[:, None] * SHAPE1 + off1[None, :]) z = tl.sum(x, axis=AXIS) if AXIS == 1: tl.atomic_add(Z + off0, z) else: tl.atomic_add(Z + off1, z) rs = RandomState(17) x = numpy_random((shape0, shape1), dtype_str="float32", rs=rs) # reference result z_ref = np.sum(x, axis=axis, keepdims=False) # triton result x_tri = to_triton(x, device=device) z_shape = (shape0, ) if axis == 1 else (shape1, ) z_tri = to_triton(np.zeros(z_shape, dtype="float32"), device=device) kernel[(1,)](z_tri, x_tri, axis, shape0, shape1) np.testing.assert_allclose(z_ref, to_numpy(z_tri), rtol=1e-4) def test_tensor_atomic_rmw_block(device): shape = (8, 8) @triton.jit def kernel(X, SHAPE0: tl.constexpr, SHAPE1: tl.constexpr): off0 = tl.arange(0, SHAPE0) off1 = tl.arange(0, SHAPE1) offs = off0[:, None] * SHAPE1 + off1[None, :] val = offs.to(tl.float32) x = X + offs tl.atomic_min(x, val) x = torch.ones((8, 8), device=device, dtype=torch.float32) kernel[(2,)](x, shape[0], shape[1]) assert torch.min(x).item() == 0.0 @pytest.mark.parametrize("sem", [None, 'acquire', 'release', 'acq_rel', 'relaxed']) def test_atomic_cas(sem, device): # 1. make sure that atomic_cas changes the original value (Lock) @triton.jit def change_value(Lock): tl.atomic_cas(Lock, 0, 1) Lock = torch.zeros((1,), device=device, dtype=torch.int32) change_value[(1,)](Lock) assert (Lock[0] == 1) # 2. only one block enters the critical section @triton.jit def serialized_add(data, Lock, SEM: tl.constexpr): ptrs = data + tl.arange(0, 128) while tl.atomic_cas(Lock, 0, 1, SEM) == 1: pass tl.store(ptrs, tl.load(ptrs) + 1.0) # release lock tl.atomic_xchg(Lock, 0) Lock = torch.zeros((1,), device=device, dtype=torch.int32) data = torch.zeros((128,), device=device, dtype=torch.float32) ref = torch.full((128,), 64.0) h = serialized_add[(64,)](data, Lock, SEM=sem) sem_str = "acq_rel" if sem is None else sem np.testing.assert_allclose(to_numpy(data), to_numpy(ref)) assert f"atom.global.{sem_str}" in h.asm["ptx"] # --------------- # test cast # --------------- @pytest.mark.parametrize("dtype_x, dtype_z, bitcast", [ (dtype_x, dtype_z, False) for dtype_x in dtypes for dtype_z in dtypes ] + [ ('float32', 'bfloat16', False), ('bfloat16', 'float32', False), ('float32', 'int32', True), ('float32', 'int1', False), ('int8', 'bfloat16', False), ] + [ (f'uint{x}', f'int{x}', True) for x in [8, 16, 32, 64] ] + [ (f'int{x}', f'uint{x}', True) for x in [8, 16, 32, 64] ]) def test_cast(dtype_x, dtype_z, bitcast, device): # bfloat16 on cc < 80 will not be tested check_type_supported(dtype_x, device) check_type_supported(dtype_z, device) size = 1024 # This is tricky because numpy doesn't have bfloat, and torch doesn't have uints. if dtype_x.startswith('bfloat'): x_tri = torch.randn(size, dtype=getattr(torch, dtype_x), device=device) else: x = numpy_random(size, dtype_str=dtype_x, low=-10, high=10) * 10 # Triton clamps negative values to zero, while numpy wraps around # intmax, so avoid negatives for now. # TODO: figure out which one should actually be happening, and test it if dtype_z in uint_dtypes: x = np.absolute(x) x_tri = to_triton(x, device=device) # triton kernel @triton.jit def kernel(X, Z, BITCAST: tl.constexpr, SIZE: tl.constexpr): x_ptr = X + tl.arange(0, SIZE) z_ptr = Z + tl.arange(0, SIZE) x = tl.load(x_ptr) z = x.to(Z.dtype.element_ty, bitcast=BITCAST) tl.store(z_ptr, z) dtype_z_np = dtype_z if dtype_z != 'int1' else 'bool_' # triton result if dtype_z.startswith('bfloat'): z_tri = torch.empty((size,), dtype=getattr(torch, dtype_z), device=device) else: z_tri = to_triton(np.empty((size, ), dtype=getattr(np, dtype_z_np)), device=device) kernel[(1, )](x_tri, z_tri, BITCAST=bitcast, SIZE=size, num_warps=1) # torch result if dtype_z.startswith('bfloat') or dtype_x.startswith('bfloat'): assert bitcast is False z_ref = x_tri.to(z_tri.dtype) torch.testing.assert_close(z_ref, z_tri, rtol=0, atol=0) else: if bitcast: z_ref = x.view(getattr(np, dtype_z_np)) else: z_ref = x.astype(getattr(np, dtype_z_np)) np.testing.assert_allclose(z_ref, to_numpy(z_tri), rtol=0, atol=0) @pytest.mark.parametrize("dtype_str, num_warps", [(dtype_str, num_warps) for dtype_str in int_dtypes + float_dtypes for num_warps in [4, 8]]) def test_cat(dtype_str, num_warps, device): check_type_supported(dtype_str, device) @triton.jit def kernel(X, Y, Z, N: tl.constexpr): offs = tl.arange(0, N) x = tl.load(X + offs) y = tl.load(Y + offs) z = tl.cat(x, y, can_reorder=True) tl.store(Z + tl.arange(0, 2 * N), z) x = torch.arange(0, 128, device=device).to(getattr(torch, dtype_str)) y = torch.arange(-128, 0, device=device).to(getattr(torch, dtype_str)) z_ref = torch.cat([x, y], dim=0).sum() z = torch.zeros((256,), dtype=getattr(torch, dtype_str), device=device) kernel[(1, )](x, y, z, N=128, num_warps=num_warps) assert z.sum() == z_ref # check if there's no duplicate value in z assert z.unique().size(0) == z.size(0) @pytest.mark.parametrize("dtype_str", list(torch_dtypes)) def test_store_constant(dtype_str, device): check_type_supported(dtype_str, device) """Tests that boolean True is stored as 1""" @triton.jit def kernel(output_ptr, n_elements, BLOCK_SIZE: tl.constexpr): offsets = tl.program_id(axis=0) * BLOCK_SIZE + tl.arange(0, BLOCK_SIZE) mask = offsets < n_elements output = GENERATE_TEST_HERE tl.store(output_ptr + offsets, output, mask=mask) triton_dtype_str = 'uint8' if dtype_str == 'bool' else dtype_str kernel = patch_kernel(kernel, {'GENERATE_TEST_HERE': f'tl.zeros([BLOCK_SIZE], dtype=tl.{triton_dtype_str}) + 1'}) block_size = 128 ref = torch.ones([block_size], dtype=getattr(torch, dtype_str), device=device) output = torch.zeros([block_size], dtype=getattr(torch, dtype_str), device=device) kernel[(1,)](output, block_size, BLOCK_SIZE=block_size) assert torch.all(output == ref) def test_load_store_same_ptr(device): @triton.jit() def kernel(in_out_ptr): pid = tl.program_id(axis=0) x = tl.load(in_out_ptr + pid) out = x * 2 tl.store(in_out_ptr + pid, out) for _ in range(1000): x = torch.ones((65536,), device=device, dtype=torch.float32) kernel[(65536,)](x, num_warps=32) assert torch.all(x == 2) def convert_float_to_float32(fp: torch.tensor, dtype=None): if not dtype: dtype = getattr(tl, torch_dtype_name(fp.dtype)) fp = fp.view(getattr(torch, f"int{dtype.primitive_bitwidth}")) exp_width = dtype.primitive_bitwidth - dtype.fp_mantissa_width - 1 exp_bias = dtype.exponent_bias sign = ((fp >> (dtype.primitive_bitwidth - 1)) & 0x01).int() exp = ((fp >> dtype.fp_mantissa_width) & ((1 << exp_width) - 1)).int() frac = (fp & ((1 << dtype.fp_mantissa_width) - 1)).int() output = torch.where(exp == 0, # subnormal ((-1.0) ** sign) * (2.0 ** (1 - exp_bias)) * (frac / (2.0 ** dtype.fp_mantissa_width)), # normal ((-1.0) ** sign) * (2.0 ** (exp - exp_bias)) * (1.0 + frac / (2.0 ** dtype.fp_mantissa_width))).float() extended_exp = ((1 << (tl.float32.primitive_bitwidth - tl.float32.fp_mantissa_width - 1)) - 1) << tl.float32.fp_mantissa_width # special cases, exp is 0b11..1 if dtype in [tl.float8e4, tl.float8e4b15]: # float8e4m3 does not have infinities output[fp == 0b01111111] = torch.nan output[fp == 0b11111111] = torch.nan else: output = torch.where(exp == (1 << exp_width) - 1, ((sign << (tl.float32.primitive_bitwidth - 1)) | extended_exp | (frac << (tl.float32.fp_mantissa_width - dtype.fp_mantissa_width))).view(torch.float32), output) return output @pytest.mark.parametrize("in_dtype", [torch.float16, torch.bfloat16]) def test_convert_float16_to_float32(in_dtype, device): """Tests that check convert_float_to_float32 function""" check_type_supported(in_dtype, device) f16_input = torch.tensor(range(-int(2 ** (16 - 1)), int(2 ** (16 - 1))), dtype=torch.int16).view(in_dtype) f32_output = convert_float_to_float32(f16_input) nan = f16_input.isnan() assert torch.all(f32_output[nan].isnan()) inf = f16_input.isinf() assert torch.all(f32_output[inf].isinf()) other = torch.logical_not(torch.logical_or(nan, inf)) assert torch.all(f16_input[other] == f32_output[other]) def serialize_fp8(np_data, in_dtype): if in_dtype == tl.float8e4b15: # triton's f8e4b15 format is optimized for software emulation # as a result, each pack of 4xfp8 values: # s0b0s1b1s2b2s3b3 (for s, b sign and bits respectively) # is actually internally stored as # s0s2b0b2s1s3b1b3 # we apply the conversion here f8x4 = np_data.view(np.uint32) s = [(f8x4 & (0x80000000 >> i)) << i for i in range(0, 32, 8)] b = [(f8x4 & (0x7f000000 >> i)) << i for i in range(0, 32, 8)] signs = (s[0] >> 0) | (s[1] >> 16) | (s[2] >> 1) | (s[3] >> 17) bits = (b[0] >> 1) | (b[1] >> 17) | (b[2] >> 8) | (b[3] >> 24) # tensor of triton fp8 data return (signs | bits).view(np.int8) else: return np_data @pytest.mark.parametrize("in_dtype", [tl.float8e4b15, tl.float8e4, tl.float8e5]) @pytest.mark.parametrize("out_dtype", [torch.float16, torch.bfloat16, torch.float32]) def test_fp8_fpN_roundtrip(in_dtype, out_dtype, device): """ For all possible float8 values (ref_fp8 = range(0, 256)), test that: - conversion tri_fp16 = convert(input=ref_fp8, out=out_dtype) matches the reference - conversion tri_fp8 = convert(input=tri_fp16, out=out_dtype) matches the original this is only possible if both conversions are correct """ check_type_supported(out_dtype, device) from contextlib import nullcontext as does_not_raise expectation = does_not_raise() err_msg = None if (in_dtype == tl.float8e4b15 and out_dtype != torch.float16) or\ (in_dtype != torch.float16 and out_dtype == tl.float8e4b15): expectation = pytest.raises(triton.CompilationError) err_msg = "fp8e4b15 can only be converted to/from fp16" @triton.jit def copy_kernel(input_ptr, output_ptr, n_elements, BLOCK_SIZE: tl.constexpr): offsets = tl.program_id(axis=0) * BLOCK_SIZE + tl.arange(0, BLOCK_SIZE) mask = offsets < n_elements input = tl.load(input_ptr + offsets, mask=mask) output = input tl.store(output_ptr + offsets, output, mask=mask) # initialize array containing all possible f8 values except NaN ref_fp8 = np.array(range(-128, 128), dtype=np.int8) is_nan = (ref_fp8 & 0b01111100) == 128 - 2**in_dtype.fp_mantissa_width exp_mask = 0b01111111 ^ ((1 << in_dtype.fp_mantissa_width) - 1) is_subnormal = np.logical_or((ref_fp8 & exp_mask) == 0, (ref_fp8 & exp_mask) == exp_mask) ref_fp8[is_nan] = 0 ref_fp8[is_subnormal] = 0 tri_fp8 = torch.from_numpy(serialize_fp8(ref_fp8, in_dtype)).cuda() tri_fp16 = torch.empty(256, dtype=out_dtype, device="cuda") with expectation as e: copy_kernel[(1,)](triton.reinterpret(tri_fp8, in_dtype), tri_fp16, tri_fp16.shape[0], BLOCK_SIZE=1024) ref_fp8 = torch.from_numpy(ref_fp8).cuda() ref_fp16 = convert_float_to_float32(ref_fp8, in_dtype) assert torch.all(tri_fp16[~is_subnormal] == ref_fp16[~is_subnormal]) ref_fp8 = torch.empty_like(tri_fp16, dtype=torch.int8) copy_kernel[(1,)](tri_fp16, triton.reinterpret(ref_fp8, in_dtype), tri_fp16.shape[0], BLOCK_SIZE=1024) assert torch.all(tri_fp8 == ref_fp8) if err_msg is not None: assert err_msg in str(e) # --------------- # test reduce # --------------- def get_reduced_dtype(dtype_str, op): if op in ('argmin', 'argmax'): return 'int32' if dtype_str in ['int8', 'uint8', 'int16', 'uint16']: return 'int32' if dtype_str == 'bfloat16': return 'float32' return dtype_str @pytest.mark.parametrize("op, dtype_str, shape", [(op, dtype, shape) for op in ['min', 'max', 'min-with-indices', 'max-with-indices', 'argmin-tie-break-left', 'argmax-tie-break-left', 'sum'] for dtype in dtypes_with_bfloat16 for shape in [32, 64, 128, 512]]) def test_reduce1d(op, dtype_str, shape, device): check_type_supported(dtype_str, device) # bfloat16 on cc < 80 will not be tested # triton kernel @triton.jit def kernel(X, Z, BLOCK: tl.constexpr): x = tl.load(X + tl.arange(0, BLOCK)) GENERATE_TEST_HERE tl.store(Z, z) if 'with-indices' in op: patch = f'z, _ = tl.{op.split("-")[0]}(x, axis=0, return_indices=True)' elif 'arg' in op: tie_break_left = 'tie-break-left' in op patch = f'z = tl.{op.split("-")[0]}(x, axis=0, tie_break_left={tie_break_left})' else: patch = f'z = tl.{op}(x, axis=0)' kernel = patch_kernel(kernel, {'GENERATE_TEST_HERE': patch}) # input rs = RandomState(17) # limit the range of integers so that the sum does not overflow x = numpy_random((shape,), dtype_str=dtype_str, rs=rs) numpy_op = {'sum': np.sum, 'max': np.max, 'min': np.min, 'max-with-indices': np.max, 'min-with-indices': np.min, 'argmin-tie-break-fast': np.argmin, 'argmin-tie-break-left': np.argmin, 'argmax-tie-break-fast': np.argmax, 'argmax-tie-break-left': np.argmax}[op] if 'tie-break-left' in op: x[3:10] = numpy_op(x) x_tri = to_triton(x, device=device) # numpy result z_dtype_str = 'int32' if op in ('argmin', 'argmax') else dtype_str z_tri_dtype_str = z_dtype_str if op not in ['argmin', 'argmax'] and dtype_str == 'bfloat16': z_dtype_str = 'float32' z_ref = numpy_op(x).astype(getattr(np, z_dtype_str)) # trunc mantissa for a fair comparison of accuracy z_ref = (z_ref.view('uint32') & np.uint32(0xffff0000)).view('float32') z_tri_dtype_str = 'bfloat16' else: z_ref = numpy_op(x).astype(getattr(np, z_dtype_str)) # triton result z_tri = to_triton(numpy_random((1,), dtype_str=z_dtype_str, rs=rs), device=device, dst_type=z_tri_dtype_str) kernel[(1,)](x_tri, z_tri, BLOCK=shape) z_tri = to_numpy(z_tri) # compare if op == 'sum': np.testing.assert_allclose(z_ref, z_tri, rtol=0.01) else: if op in ('argmin', 'argmax'): # argmin and argmax can have multiple valid indices. # so instead we compare the values pointed by indices np.testing.assert_equal(x[z_ref], x[z_tri]) else: np.testing.assert_equal(z_ref, z_tri) # TODO: [Qingyi] Fix argmin / argmax reduce_configs1 = [ (op, dtype, (1, 1024), axis) for dtype in dtypes_with_bfloat16 for op in ['min', 'max', 'sum', 'argmin', 'argmax'] for axis in [1] ] # shape (128, 256) and (32, 1024) are not enabled on sm86 because the required shared memory # exceeds the limit of 99KB reduce2d_shapes = [(2, 32), (4, 32), (4, 128)] # TODO: fix and uncomment # , (32, 64), (64, 128)] if torch.cuda.is_available() and 'V100' in torch.cuda.get_device_name(0): reduce2d_shapes += [(128, 256) and (32, 1024)] reduce_configs2 = [ (op, 'float32', shape, axis) for op in ['min', 'max', 'sum', 'argmin', 'argmax'] for shape in reduce2d_shapes for axis in [0, 1] ] + [ (op, 'float32', [16, 32], None) for op in ['min', 'max', 'sum'] ] @pytest.mark.parametrize("op, dtype_str, shape, axis", reduce_configs1 + reduce_configs2) def test_reduce2d(op, dtype_str, shape, axis, device): check_type_supported(dtype_str, device) # bfloat16 on cc < 80 will not be tested # triton kernel @triton.jit def kernel(X, Z, BLOCK_M: tl.constexpr, BLOCK_N: tl.constexpr, AXIS: tl.constexpr): range_m = tl.arange(0, BLOCK_M) range_n = tl.arange(0, BLOCK_N) x = tl.load(X + range_m[:, None] * BLOCK_N + range_n[None, :]) z = GENERATE_TEST_HERE if AXIS is None: tl.store(Z, z) elif AXIS == 1: tl.store(Z + range_m, z) else: tl.store(Z + range_n, z) kernel = patch_kernel(kernel, {'GENERATE_TEST_HERE': f'tl.{op}(x, axis=AXIS)'}) # input rs = RandomState(17) # limit the range of integers so that the sum does not overflow x = numpy_random(shape, dtype_str=dtype_str, rs=rs) x_tri = to_triton(x, device=device) numpy_op = {'sum': np.sum, 'max': np.max, 'min': np.min, 'argmin': np.argmin, 'argmax': np.argmax}[op] z_dtype_str = get_reduced_dtype(dtype_str, op) z_tri_dtype_str = z_dtype_str # numpy result if op not in ['argmin', 'argmax'] and dtype_str == 'bfloat16': z_dtype_str = 'float32' z_tri_dtype_str = 'bfloat16' z_ref = numpy_op(x, axis=axis).astype(getattr(np, z_dtype_str)) # trunc mantissa for a fair comparison of accuracy z_ref = (z_ref.view('uint32') & np.uint32(0xffff0000)).view('float32') else: z_ref = numpy_op(x, axis=axis).astype(getattr(np, z_dtype_str)) # triton result ret_numel = 1 if axis is None else shape[1 - axis] z_tri = to_triton(numpy_random((ret_numel,), dtype_str=z_dtype_str, rs=rs), device=device, dst_type=z_tri_dtype_str) kernel[(1,)](x_tri, z_tri, BLOCK_M=shape[0], BLOCK_N=shape[1], AXIS=axis) z_tri = to_numpy(z_tri) # compare if op == 'sum': np.testing.assert_allclose(z_ref, z_tri, rtol=0.01) else: if op in ('argmin', 'argmax'): # argmin and argmax can have multiple valid indices. # so instead we compare the values pointed by indices z_ref_index = np.expand_dims(z_ref, axis=axis) z_tri_index = np.expand_dims(z_tri, axis=axis) z_ref_value = np.take_along_axis(x, z_ref_index, axis=axis) z_tri_value = np.take_along_axis(x, z_tri_index, axis=axis) np.testing.assert_equal(z_ref_value, z_tri_value) else: np.testing.assert_equal(z_ref, z_tri) scan2d_shapes = [(8, 32), (16, 32), (32, 16), (2, 1024), (1024, 2), (32, 32), (1, 1024)] scan_configs = [ (op, type, shape, axis, num_warps) for num_warps in [4, 16] for type in ['int32', 'float32'] for axis in [1, 0] for shape in scan2d_shapes for op in ['cumsum', 'cumprod'] ] @pytest.mark.parametrize("op, dtype_str, shape, axis, num_warps", scan_configs) def test_scan2d(op, dtype_str, shape, axis, num_warps, device): check_type_supported(dtype_str, device) # triton kernel @triton.jit def kernel(X, Z, BLOCK_M: tl.constexpr, BLOCK_N: tl.constexpr, AXIS: tl.constexpr): range_m = tl.arange(0, BLOCK_M) range_n = tl.arange(0, BLOCK_N) x = tl.load(X + range_m[:, None] * BLOCK_N + range_n[None, :]) z = GENERATE_TEST_HERE tl.store(Z + range_m[:, None] * BLOCK_N + range_n[None, :], z) kernel = patch_kernel(kernel, {'GENERATE_TEST_HERE': f'tl.{op}(x, axis={axis})'}) # input rs = RandomState(17) x = numpy_random(shape, dtype_str=dtype_str, rs=rs) z = np.empty_like(x) x_tri = to_triton(x, device=device) numpy_op = {'cumsum': np.cumsum, 'cumprod': np.cumprod}[op] z_dtype_str = dtype_str z_ref = numpy_op(x, axis=axis).astype(getattr(np, z_dtype_str)) # triton result z_tri = to_triton(z, device=device) kernel[(1,)](x_tri, z_tri, BLOCK_M=shape[0], BLOCK_N=shape[1], AXIS=axis, num_warps=num_warps) z_tri = to_numpy(z_tri) # compare if dtype_str == 'float32': if op == 'cumprod': np.testing.assert_allclose(z_ref, z_tri, rtol=0.01, atol=1e-3) else: np.testing.assert_allclose(z_ref, z_tri, rtol=0.01) else: np.testing.assert_equal(z_ref, z_tri) scan_layouts = [ BlockedLayout([1, 4], [4, 8], [4, 1], [0, 1]), BlockedLayout([1, 4], [8, 4], [4, 1], [0, 1]), BlockedLayout([4, 1], [4, 8], [1, 4], [0, 1]), BlockedLayout([2, 2], [4, 8], [2, 2], [0, 1]), BlockedLayout([2, 2], [8, 4], [2, 2], [0, 1]), BlockedLayout([1, 4], [4, 8], [4, 1], [1, 0]), BlockedLayout([1, 4], [8, 4], [4, 1], [1, 0]), BlockedLayout([4, 1], [4, 8], [1, 4], [1, 0]), BlockedLayout([2, 2], [4, 8], [2, 2], [1, 0]), BlockedLayout([2, 2], [8, 4], [2, 2], [1, 0]), ] @pytest.mark.parametrize("M, N", [[32, 32], [32, 64], [64, 32]]) @pytest.mark.parametrize("src_layout", scan_layouts) @pytest.mark.parametrize("axis", [0, 1]) def test_scan_layouts(M, N, src_layout, axis, device): ir = f""" #blocked = {src_layout} module attributes {{"triton_gpu.num-warps" = 4 : i32, "triton_gpu.threads-per-warp" = 32 : i32}} {{ tt.func public @kernel_0d1d(%arg0: !tt.ptr<i32> {{tt.divisibility = 16 : i32}}, %arg1: !tt.ptr<i32> {{tt.divisibility = 16 : i32}}) {{ %cst = arith.constant dense<{N}> : tensor<{M}x1xi32, #blocked> %0 = tt.make_range {{end = {M} : i32, start = 0 : i32}} : tensor<{M}xi32, #triton_gpu.slice<{{dim = 1, parent = #blocked}}>> %1 = tt.expand_dims %0 {{axis = 1 : i32}} : (tensor<{M}xi32, #triton_gpu.slice<{{dim = 1, parent = #blocked}}>>) -> tensor<{M}x1xi32, #blocked> %2 = arith.muli %1, %cst : tensor<{M}x1xi32, #blocked> %3 = tt.splat %arg0 : (!tt.ptr<i32>) -> tensor<{M}x1x!tt.ptr<i32>, #blocked> %4 = tt.addptr %3, %2 : tensor<{M}x1x!tt.ptr<i32>, #blocked>, tensor<{M}x1xi32, #blocked> %5 = tt.make_range {{end = {N} : i32, start = 0 : i32}} : tensor<{N}xi32, #triton_gpu.slice<{{dim = 0, parent = #blocked}}>> %6 = tt.expand_dims %5 {{axis = 0 : i32}} : (tensor<{N}xi32, #triton_gpu.slice<{{dim = 0, parent = #blocked}}>>) -> tensor<1x{N}xi32, #blocked> %7 = tt.broadcast %4 : (tensor<{M}x1x!tt.ptr<i32>, #blocked>) -> tensor<{M}x{N}x!tt.ptr<i32>, #blocked> %8 = tt.broadcast %6 : (tensor<1x{N}xi32, #blocked>) -> tensor<{M}x{N}xi32, #blocked> %9 = tt.addptr %7, %8 : tensor<{M}x{N}x!tt.ptr<i32>, #blocked>, tensor<{M}x{N}xi32, #blocked> %10 = tt.load %9 {{cache = 1 : i32, evict = 1 : i32, isVolatile = false}} : tensor<{M}x{N}xi32, #blocked> %11 = "tt.scan"(%10) <{{axis = {axis} : i32}}> ({{ ^bb0(%arg2: i32, %arg3: i32): %16 = arith.addi %arg2, %arg3 : i32 tt.scan.return %16 : i32 }}) : (tensor<{M}x{N}xi32, #blocked>) -> tensor<{M}x{N}xi32, #blocked> %12 = tt.splat %arg1 : (!tt.ptr<i32>) -> tensor<{M}x1x!tt.ptr<i32>, #blocked> %13 = tt.addptr %12, %2 : tensor<{M}x1x!tt.ptr<i32>, #blocked>, tensor<{M}x1xi32, #blocked> %14 = tt.broadcast %13 : (tensor<{M}x1x!tt.ptr<i32>, #blocked>) -> tensor<{M}x{N}x!tt.ptr<i32>, #blocked> %15 = tt.addptr %14, %8 : tensor<{M}x{N}x!tt.ptr<i32>, #blocked>, tensor<{M}x{N}xi32, #blocked> tt.store %15, %11 {{cache = 1 : i32, evict = 1 : i32}} : tensor<{M}x{N}xi32, #blocked> tt.return }} }} """ import tempfile with tempfile.NamedTemporaryFile(mode='w', suffix='.ttgir') as f: f.write(ir) f.flush() kernel = triton.compile(f.name) rs = RandomState(17) x = rs.randint(-100, 100, (M, N)).astype('int32') z = np.zeros((M, N)).astype('int32') x_tri = torch.tensor(x, device=device) z_tri = torch.tensor(z, device=device) kernel[(1, 1, 1)](x_tri, z_tri) z_ref = np.cumsum(x, axis=axis) np.testing.assert_equal(z_ref, z_tri.cpu().numpy()) layouts = [ BlockedLayout([1, 4], [8, 4], [4, 1], [1, 0]), BlockedLayout([1, 4], [8, 4], [4, 1], [0, 1]), BlockedLayout([4, 4], [2, 16], [4, 1], [1, 0]), MmaLayout(version=(2, 0), warps_per_cta=[4, 1]), MmaLayout(version=(2, 0), warps_per_cta=[2, 2]) ] @pytest.mark.parametrize("M, N", [[128, 16], [128, 128], [32, 128], [32, 32]]) @pytest.mark.parametrize("src_layout", layouts) @pytest.mark.parametrize("axis", [0, 1]) def test_reduce_layouts(M, N, src_layout, axis, device): rdims_2d = f"1x{N}" if axis == 0 else f"{M}x1" rdims_1d = f"{N}" if axis == 0 else f"{M}" store_range = "%7" if axis == 0 else "%1" ir = f""" #blocked = #triton_gpu.blocked<{{sizePerThread = [1, 1], threadsPerWarp = [32, 1], warpsPerCTA = [4, 1], order = [0, 1]}}> #src = {src_layout} module attributes {{"triton_gpu.num-warps" = 4 : i32}} {{ tt.func public @kernel_0d1d2c3d4c(%arg0: !tt.ptr<i32> {{tt.divisibility = 16 : i32}}, %arg1: i32 {{tt.divisibility = 16 : i32}}, %arg2: !tt.ptr<i32> {{tt.divisibility = 16 : i32}}) {{ %0 = tt.make_range {{end = {M} : i32, start = 0 : i32}} : tensor<{M}xi32, #triton_gpu.slice<{{dim = 1, parent = #blocked}}>> %1 = tt.expand_dims %0 {{axis = 1 : i32}} : (tensor<{M}xi32, #triton_gpu.slice<{{dim = 1, parent = #blocked}}>>) -> tensor<{M}x1xi32, #blocked> %2 = tt.splat %arg1 : (i32) -> tensor<{M}x1xi32, #blocked> %3 = arith.muli %1, %2 : tensor<{M}x1xi32, #blocked> %4 = tt.splat %arg0 : (!tt.ptr<i32>) -> tensor<{M}x1x!tt.ptr<i32>, #blocked> %5 = tt.addptr %4, %3 : tensor<{M}x1x!tt.ptr<i32>, #blocked>, tensor<{M}x1xi32, #blocked> %6 = tt.make_range {{end = {N} : i32, start = 0 : i32}} : tensor<{N}xi32, #triton_gpu.slice<{{dim = 0, parent = #blocked}}>> %7 = tt.expand_dims %6 {{axis = 0 : i32}} : (tensor<{N}xi32, #triton_gpu.slice<{{dim = 0, parent = #blocked}}>>) -> tensor<1x{N}xi32, #blocked> %8 = tt.broadcast %5 : (tensor<{M}x1x!tt.ptr<i32>, #blocked>) -> tensor<{M}x{N}x!tt.ptr<i32>, #blocked> %9 = tt.broadcast %7 : (tensor<1x{N}xi32, #blocked>) -> tensor<{M}x{N}xi32, #blocked> %10 = tt.addptr %8, %9 : tensor<{M}x{N}x!tt.ptr<i32>, #blocked>, tensor<{M}x{N}xi32, #blocked> %11 = tt.splat %arg2 : (!tt.ptr<i32>) -> tensor<{rdims_2d}x!tt.ptr<i32>, #blocked> %12 = tt.addptr %11, {store_range} : tensor<{rdims_2d}x!tt.ptr<i32>, #blocked>, tensor<{rdims_2d}xi32, #blocked> %13 = tt.load %10 {{cache = 1 : i32, evict = 1 : i32, isVolatile = false}} : tensor<{M}x{N}xi32, #blocked> %14 = triton_gpu.convert_layout %13 : (tensor<{M}x{N}xi32, #blocked>) -> tensor<{M}x{N}xi32, #src> %15 = "tt.reduce"(%14) ({{ ^bb0(%arg3: i32, %arg4: i32): %17 = arith.addi %arg3, %arg4 : i32 tt.reduce.return %17 : i32 }}) {{axis = {axis} : i32}} : (tensor<{M}x{N}xi32, #src>) -> tensor<{rdims_1d}xi32, #triton_gpu.slice<{{dim = {axis}, parent = #src}}>> %18 = triton_gpu.convert_layout %15 : (tensor<{rdims_1d}xi32, #triton_gpu.slice<{{dim = {axis}, parent = #src}}>>) -> tensor<{rdims_1d}xi32, #triton_gpu.slice<{{dim = {axis}, parent = #blocked}}>> %19 = tt.expand_dims %18 {{axis = {axis} : i32}} : (tensor<{rdims_1d}xi32, #triton_gpu.slice<{{dim = {axis}, parent = #blocked}}>>) -> tensor<{rdims_2d}xi32, #blocked> tt.store %12, %19 {{cache = 1 : i32, evict = 1 : i32}} : tensor<{rdims_2d}xi32, #blocked> tt.return }} }} """ import tempfile with tempfile.NamedTemporaryFile(mode='w', suffix='.ttgir') as f: f.write(ir) f.flush() kernel = triton.compile(f.name) rs = RandomState(17) x = rs.randint(0, 20, (M, N)).astype('int32') if axis == 0: z = np.zeros((1, N)).astype('int32') else: z = np.zeros((M, 1)).astype('int32') x_tri = torch.tensor(x, device=device) z_tri = torch.tensor(z, device=device) pgm = kernel[(1, 1, 4)](x_tri, x_tri.stride(0), z_tri) z_ref = np.sum(x, axis=axis, keepdims=True) np.testing.assert_equal(z_ref, z_tri.cpu().numpy()) layouts = [ BlockedLayout([1, 4], [1, 32], [4, 1], [1, 0]), BlockedLayout([1, 4], [1, 32], [2, 2], [1, 0]), MmaLayout(version=(2, 0), warps_per_cta=[4, 1]) ] @pytest.mark.parametrize("M", [32, 64, 128, 256]) @pytest.mark.parametrize("src_layout", layouts) def test_store_op(M, src_layout, device): ir = f""" #src = {src_layout} module attributes {{"triton_gpu.num-warps" = 4 : i32}} {{ tt.func public @kernel(%arg0: !tt.ptr<f32> {{tt.divisibility = 16 : i32}}, %arg1: !tt.ptr<f32> {{tt.divisibility = 16 : i32}}) {{ %0 = tt.make_range {{end = {M} : i32, start = 0 : i32}} : tensor<{M}xi32, #triton_gpu.slice<{{dim = 1, parent = #src}}>> %1 = tt.splat %arg0 : (!tt.ptr<f32>) -> tensor<{M}x!tt.ptr<f32>, #triton_gpu.slice<{{dim = 1, parent = #src}}>> %2 = tt.addptr %1, %0 : tensor<{M}x!tt.ptr<f32>, #triton_gpu.slice<{{dim = 1, parent = #src}}>>, tensor<{M}xi32, #triton_gpu.slice<{{dim = 1, parent = #src}}>> %3 = tt.load %2 {{cache = 1 : i32, evict = 1 : i32, isVolatile = false}} : tensor<{M}xf32, #triton_gpu.slice<{{dim = 1, parent = #src}}>> %4 = tt.expand_dims %3 {{axis = 1 : i32}} : (tensor<{M}xf32, #triton_gpu.slice<{{dim = 1, parent = #src}}>>) -> tensor<{M}x1xf32, #src> %5 = tt.make_range {{end = {M} : i32, start = 0 : i32}} : tensor<{M}xi32, #triton_gpu.slice<{{dim = 1, parent = #src}}>> %6 = tt.expand_dims %5 {{axis = 1 : i32}} : (tensor<{M}xi32, #triton_gpu.slice<{{dim = 1, parent = #src}}>>) -> tensor<{M}x1xi32, #src> %7 = tt.splat %arg1 : (!tt.ptr<f32>) -> tensor<{M}x1x!tt.ptr<f32>, #src> %8 = tt.addptr %7, %6 : tensor<{M}x1x!tt.ptr<f32>, #src>, tensor<{M}x1xi32, #src> tt.store %8, %4 : tensor<{M}x1xf32, #src> tt.return }} }} """ import tempfile with tempfile.NamedTemporaryFile(mode='w', suffix='.ttgir') as f: f.write(ir) f.flush() store_kernel = triton.compile(f.name) rs = RandomState(17) x = rs.randint(0, 4, (M, 1)).astype('float32') y = np.zeros((M, 1), dtype='float32') x_tri = torch.tensor(x, device=device) y_tri = torch.tensor(y, device=device) pgm = store_kernel[(1, 1, 1)](x_tri, y_tri) y_ref = x np.testing.assert_allclose(y_ref, y_tri.cpu().numpy(), rtol=0.01, atol=1e-3) layouts = [ BlockedLayout([1, 4], [1, 32], [4, 1], [1, 0]), BlockedLayout([1, 4], [1, 32], [2, 2], [1, 0]), MmaLayout(version=(2, 0), warps_per_cta=[4, 1]) ] @pytest.mark.parametrize("M", [64, 128, 256]) @pytest.mark.parametrize("src_layout", layouts) @pytest.mark.parametrize("dst_layout", layouts) @pytest.mark.parametrize("src_dim", [0, 1]) @pytest.mark.parametrize("dst_dim", [0, 1]) def test_convert1d(M, src_layout, dst_layout, src_dim, dst_dim, device): ir = f""" #dst = {dst_layout} #src = {src_layout} module attributes {{"triton_gpu.num-warps" = 4 : i32}} {{ tt.func public @kernel(%arg0: !tt.ptr<i32> {{tt.divisibility = 16 : i32}}, %arg1: !tt.ptr<i32> {{tt.divisibility = 16 : i32}}) {{ %0 = tt.splat %arg0 : (!tt.ptr<i32>) -> tensor<{M}x!tt.ptr<i32>, #triton_gpu.slice<{{dim = {src_dim}, parent = #src}}>> %1 = tt.make_range {{end = {M} : i32, start = 0 : i32}} : tensor<{M}xi32, #triton_gpu.slice<{{dim = {src_dim}, parent = #src}}>> %2 = tt.addptr %0, %1 : tensor<{M}x!tt.ptr<i32>, #triton_gpu.slice<{{dim = {src_dim}, parent = #src}}>>, tensor<{M}xi32, #triton_gpu.slice<{{dim = {src_dim}, parent = #src}}>> %3 = tt.load %2 {{cache = 1 : i32, evict = 1 : i32, isVolatile = false}} : tensor<{M}xi32, #triton_gpu.slice<{{dim = {src_dim}, parent = #src}}>> %4 = tt.splat %arg1 : (!tt.ptr<i32>) -> tensor<{M}x!tt.ptr<i32>, #triton_gpu.slice<{{dim = {dst_dim}, parent = #dst}}>> %5 = tt.make_range {{end = {M} : i32, start = 0 : i32}} : tensor<{M}xi32, #triton_gpu.slice<{{dim = {dst_dim}, parent = #dst}}>> %6 = tt.addptr %4, %5 : tensor<{M}x!tt.ptr<i32>, #triton_gpu.slice<{{dim = {dst_dim}, parent = #dst}}>>, tensor<{M}xi32, #triton_gpu.slice<{{dim = {dst_dim}, parent = #dst}}>> %7 = triton_gpu.convert_layout %3 : (tensor<{M}xi32, #triton_gpu.slice<{{dim = {src_dim}, parent = #src}}>>) -> tensor<{M}xi32, #triton_gpu.slice<{{dim = {dst_dim}, parent = #dst}}>> tt.store %6, %7 : tensor<{M}xi32, #triton_gpu.slice<{{dim = {dst_dim}, parent = #dst}}>> tt.return }} }} """ import tempfile with tempfile.NamedTemporaryFile(mode='w', suffix='.ttgir') as f: f.write(ir) f.flush() kernel = triton.compile(f.name) rs = RandomState(17) x = rs.randint(0, 4, (M, )).astype('int32') y = np.zeros((M, ), dtype='int32') x_tri = torch.tensor(x, device=device) y_tri = torch.tensor(y, device=device) pgm = kernel[(1, 1, 1)](x_tri, y_tri) y_ref = x np.testing.assert_allclose(y_ref, y_tri.cpu().numpy(), rtol=0.01, atol=1e-3) @triton.jit def _welford_combine(mean_1, m2_1, weight_1, mean_2, m2_2, weight_2): delta = mean_2 - mean_1 new_weight = weight_1 + weight_2 w2_over_w = weight_2 / new_weight return ( mean_1 + delta * w2_over_w, m2_1 + m2_2 + delta * delta * weight_1 * w2_over_w, new_weight, ) layouts = [ BlockedLayout([1, 4], [1, 32], [4, 1], [1, 0]), BlockedLayout([1, 4], [1, 32], [2, 2], [1, 0]), BlockedLayout([1, 4], [1, 32], [1, 4], [1, 0]), BlockedLayout([1, 4], [8, 4], [2, 2], [0, 1]) ] @pytest.mark.parametrize("M, N", [[128, 128], [256, 128], [256, 256], [128, 256]]) @pytest.mark.parametrize("src_layout", layouts) @pytest.mark.parametrize("op", ["sum", "max"]) @pytest.mark.parametrize("first_axis", [0, 1]) def test_chain_reduce(M, N, src_layout, op, device, first_axis): op_str = "" if op == "sum": op_str = f""" %13 = arith.addi %arg2, %arg3 : i32 tt.reduce.return %13 : i32""" elif op == "max": op_str = f""" %13 = "triton_gpu.cmpi"(%arg2, %arg3) <{{predicate = 4 : i64}}> : (i32, i32) -> i1 %14 = arith.select %13, %arg2, %arg3 : i32 tt.reduce.return %14 : i32""" ir = f""" #src = {src_layout} module attributes {{"triton_gpu.num-warps" = 4 : i32}} {{ tt.func public @sum_kernel_0d1d(%arg0: !tt.ptr<i32> {{tt.divisibility = 16 : i32}}, %arg1: !tt.ptr<i32> {{tt.divisibility = 16 : i32}}) {{ %cst = arith.constant dense<{N}> : tensor<{M}x1xi32, #src> %0 = tt.make_range {{end = {M} : i32, start = 0 : i32}} : tensor<{M}xi32, #triton_gpu.slice<{{dim = 1, parent = #src}}>> %1 = tt.expand_dims %0 {{axis = 1 : i32}} : (tensor<{M}xi32, #triton_gpu.slice<{{dim = 1, parent = #src}}>>) -> tensor<{M}x1xi32, #src> %2 = arith.muli %1, %cst : tensor<{M}x1xi32, #src> %3 = tt.make_range {{end = {N} : i32, start = 0 : i32}} : tensor<{N}xi32, #triton_gpu.slice<{{dim = 0, parent = #src}}>> %4 = tt.expand_dims %3 {{axis = 0 : i32}} : (tensor<{N}xi32, #triton_gpu.slice<{{dim = 0, parent = #src}}>>) -> tensor<1x{N}xi32, #src> %5 = tt.broadcast %2 : (tensor<{M}x1xi32, #src>) -> tensor<{M}x{N}xi32, #src> %6 = tt.broadcast %4 : (tensor<1x{N}xi32, #src>) -> tensor<{M}x{N}xi32, #src> %7 = arith.addi %5, %6 : tensor<{M}x{N}xi32, #src> %8 = tt.splat %arg0 : (!tt.ptr<i32>) -> tensor<{M}x{N}x!tt.ptr<i32>, #src> %9 = tt.addptr %8, %7 : tensor<{M}x{N}x!tt.ptr<i32>, #src>, tensor<{M}x{N}xi32, #src> %10 = tt.load %9 {{cache = 1 : i32, evict = 1 : i32, isVolatile = false}} : tensor<{M}x{N}xi32, #src> %11 = "tt.reduce"(%10) ({{ ^bb0(%arg2: i32, %arg3: i32): {op_str} }}) {{axis = {first_axis} : i32}} : (tensor<{M}x{N}xi32, #src>) -> tensor<{M if first_axis == 1 else N}xi32, #triton_gpu.slice<{{dim = {first_axis}, parent = #src}}>> %12 = "tt.reduce"(%11) ({{ ^bb0(%arg2: i32, %arg3: i32): {op_str} }}) {{axis = 0 : i32}} : (tensor<{M if first_axis == 1 else N}xi32, #triton_gpu.slice<{{dim = {first_axis}, parent = #src}}>>) -> i32 tt.store %arg1, %12 {{cache = 1 : i32, evict = 1 : i32}} : i32 tt.return }} }} """ import tempfile with tempfile.NamedTemporaryFile(mode='w', suffix='.ttgir') as f: f.write(ir) f.flush() kernel = triton.compile(f.name) rs = RandomState(17) x = rs.randint(0, 4, (M, N)).astype('int32') z = np.zeros((1,)).astype('int32') x_tri = torch.tensor(x, device=device) z_tri = torch.tensor(z, device=device) pgm = kernel[(1, 1, 1)](x_tri, z_tri) if op == "sum": z_ref = np.sum(x) elif op == "max": z_ref = np.max(x) np.testing.assert_allclose(z_ref, z_tri.cpu().numpy(), rtol=0.01, atol=1e-3) def test_generic_reduction(device): @triton.jit def var_mean_kernel(X, out_mean, out_var, BLOCK: tl.constexpr): xindex = tl.arange(0, BLOCK) x = tl.load(X + xindex) mean = x m2 = tl.zeros_like(x) weight = tl.full(x.shape, 1, x.dtype) (mean, m2, weight) = tl.reduce((mean, m2, weight), 0, _welford_combine) tl.store(out_mean, mean) tl.store(out_var, m2 / weight) SIZE = 512 x = torch.rand(SIZE, device=device) out_mean = torch.empty((), device=device) out_var = torch.empty((), device=device) var_mean_kernel[(1,)](x, out_mean, out_var, BLOCK=SIZE) expect_var, expect_mean = torch.var_mean(x, dim=0, correction=0) torch.testing.assert_close(out_mean, expect_mean) torch.testing.assert_close(out_var, expect_var) # --------------- # test permute # --------------- @pytest.mark.parametrize("dtype_str, shape, perm", [(dtype, shape, perm) # TODO: bfloat16 for dtype in ['float16', 'float32'] for shape in [(64, 64), (128, 128)] for perm in [(1, 0)]]) def test_permute(dtype_str, shape, perm, device): check_type_supported(dtype_str, device) # bfloat16 on cc < 80 will not be tested # triton kernel @triton.jit def kernel(X, stride_xm, stride_xn, Z, stride_zm, stride_zn, BLOCK_M: tl.constexpr, BLOCK_N: tl.constexpr): off_m = tl.arange(0, BLOCK_M) off_n = tl.arange(0, BLOCK_N) Xs = X + off_m[:, None] * stride_xm + off_n[None, :] * stride_xn Zs = Z + off_m[:, None] * stride_zm + off_n[None, :] * stride_zn tl.store(Zs, tl.load(Xs)) # input x = numpy_random(shape, dtype_str=dtype_str) # triton result z_tri = to_triton(np.empty_like(x), device=device, dst_type=dtype_str) z_tri_contiguous = to_triton(np.empty_like(x), device=device, dst_type=dtype_str) x_tri = to_triton(x, device=device, dst_type=dtype_str) pgm = kernel[(1, 1)](x_tri, x_tri.stride(0), x_tri.stride(1), z_tri, z_tri.stride(1), z_tri.stride(0), BLOCK_M=shape[0], BLOCK_N=shape[1]) pgm_contiguous = kernel[(1, 1)](x_tri, x_tri.stride(1), x_tri.stride(0), z_tri_contiguous, z_tri_contiguous.stride(0), z_tri_contiguous.stride(1), BLOCK_M=shape[0], BLOCK_N=shape[1]) # numpy result z_ref = x.transpose(*perm) # compare np.testing.assert_allclose(to_numpy(z_tri), z_ref) np.testing.assert_allclose(to_numpy(z_tri_contiguous), z_ref) # parse ptx to make sure ld/st are vectorized ptx = pgm.asm['ptx'] assert 'ld.global.v4' in ptx assert 'st.global.v4' in ptx ptx = pgm_contiguous.asm['ptx'] assert 'ld.global.v4' in ptx assert 'st.global.v4' in ptx # --------------- # test dot # --------------- @pytest.mark.parametrize("M, N, K, num_warps, col_a, col_b, epilogue, allow_tf32, in_dtype, out_dtype", [(*shape, 4, False, False, epilogue, allow_tf32, in_dtype, out_dtype) for shape in [(64, 64, 64), (16, 16, 16)] for epilogue in ['none', 'trans', 'add-matrix', 'add-rows', 'add-cols', 'softmax', 'chain-dot'] for allow_tf32 in [True, False] for in_dtype, out_dtype in [('float16', 'float16'), ('float16', 'float32'), ('float32', 'float32')] if not (allow_tf32 and (in_dtype in ['float16']))] + [(*shape_nw, col_a, col_b, 'none', allow_tf32, in_dtype, out_dtype) for shape_nw in [[128, 256, 32, 8], [128, 16, 32, 4], [32, 128, 64, 4], [128, 128, 64, 4], [64, 128, 128, 4], [32, 128, 64, 2], [64, 64, 32, 4], [32, 32, 128, 16], [128, 128, 64, 2], [64, 128, 128, 2]] for allow_tf32 in [True] for col_a in [True, False] for col_b in [True, False] for in_dtype, out_dtype in [('int8', 'int8'), ('float16', 'float16'), ('float16', 'float32'), ('float32', 'float32')]]) def test_dot(M, N, K, num_warps, col_a, col_b, epilogue, allow_tf32, in_dtype, out_dtype, device): check_cuda_only(device) capability = torch.cuda.get_device_capability() if capability[0] < 7: pytest.skip("Only test tl.dot() on devices with sm >= 70") if capability[0] < 8: if in_dtype == 'int8': pytest.skip("Only test int8 on devices with sm >= 80") elif in_dtype == 'float32' and allow_tf32: pytest.skip("Only test tf32 on devices with sm >= 80") if capability[0] == 7: if (M, N, K, num_warps) == (128, 256, 32, 8): pytest.skip("shared memory out of resource") if out_dtype == 'float16': # TODO: support out_dtype=float16 for tl.dot on V100 pytest.skip("Only test out_dtype=float16 on devices with sm >=80") torch.backends.cuda.matmul.allow_tf32 = allow_tf32 # triton kernel @triton.jit def kernel(X, stride_xm, stride_xk, Y, stride_yk, stride_yn, W, stride_wn, stride_wl, Z, stride_zm, stride_zn, out_dtype: tl.constexpr, BLOCK_M: tl.constexpr, BLOCK_N: tl.constexpr, BLOCK_K: tl.constexpr, ADD_MATRIX: tl.constexpr, ADD_ROWS: tl.constexpr, ADD_COLS: tl.constexpr, ALLOW_TF32: tl.constexpr, DO_SOFTMAX: tl.constexpr, CHAIN_DOT: tl.constexpr, COL_A: tl.constexpr, COL_B: tl.constexpr): off_m = tl.arange(0, BLOCK_M) off_n = tl.arange(0, BLOCK_N) off_l = tl.arange(0, BLOCK_N) off_k = tl.arange(0, BLOCK_K) Xs = X + off_m[:, None] * stride_xm + off_k[None, :] * stride_xk Ys = Y + off_k[:, None] * stride_yk + off_n[None, :] * stride_yn Ws = W + off_n[:, None] * stride_wn + off_l[None, :] * stride_wl Zs = Z + off_m[:, None] * stride_zm + off_n[None, :] * stride_zn x = tl.load(Xs) y = tl.load(Ys) z = tl.dot(x, y, allow_tf32=ALLOW_TF32, out_dtype=out_dtype) if ADD_MATRIX: z += tl.load(Zs) if ADD_ROWS: ZRs = Z + off_m * stride_zm z += tl.load(ZRs)[:, None] if ADD_COLS: ZCs = Z + off_n * stride_zn z += tl.load(ZCs)[None, :] if DO_SOFTMAX: max = tl.max(z, 1) z = z - max[:, None] num = tl.exp(z.to(tl.float32)).to(max.dtype) den = tl.sum(num, 1) z = num / den[:, None] if CHAIN_DOT: w = tl.load(Ws) z = tl.dot(z.to(w.dtype), w, out_dtype=out_dtype) tl.store(Zs, z) # input rs = RandomState(17) if col_a: x = numpy_random((K, M), dtype_str=in_dtype, rs=rs).T else: x = numpy_random((M, K), dtype_str=in_dtype, rs=rs) if col_b: y = numpy_random((N, K), dtype_str=in_dtype, rs=rs).T else: y = numpy_random((K, N), dtype_str=in_dtype, rs=rs) w = numpy_random((N, N), dtype_str=in_dtype, rs=rs) if 'int' not in in_dtype: x *= .1 y *= .1 if in_dtype == 'float32' and allow_tf32: x = (x.view('uint32') & np.uint32(0xffffe000)).view('float32') y = (y.view('uint32') & np.uint32(0xffffe000)).view('float32') w = (w.view('uint32') & np.uint32(0xffffe000)).view('float32') x_tri = to_triton(x, device=device) y_tri = to_triton(y, device=device) w_tri = to_triton(w, device=device) # triton result if out_dtype == 'int8': z = 1 + numpy_random((M, N), dtype_str='int32', rs=rs) else: z = 1 + numpy_random((M, N), dtype_str=in_dtype, rs=rs) * .1 z_tri = to_triton(z, device=device) if epilogue == 'trans': z_tri = torch.as_strided(z_tri, (M, N), z_tri.stride()[::-1]) if out_dtype == 'int8': out_dtype = tl.int8 elif out_dtype == 'float16' and epilogue != 'softmax': # TODO: for out_dtype == 'float16' and epilogue == 'softmax', it will # fail with the following error: 'llvm.fmul' op requires the same type # for all operands and results out_dtype = tl.float16 else: out_dtype = tl.float32 pgm = kernel[(1, 1)](x_tri, x_tri.stride(0), x_tri.stride(1), y_tri, y_tri.stride(0), y_tri.stride(1), w_tri, w_tri.stride(0), w_tri.stride(1), z_tri, z_tri.stride(0), z_tri.stride(1), out_dtype, COL_A=col_a, COL_B=col_b, BLOCK_M=M, BLOCK_K=K, BLOCK_N=N, ADD_MATRIX=epilogue == 'add-matrix', ADD_ROWS=epilogue == 'add-rows', ADD_COLS=epilogue == 'add-cols', DO_SOFTMAX=epilogue == 'softmax', CHAIN_DOT=epilogue == 'chain-dot', ALLOW_TF32=allow_tf32, num_warps=num_warps) if epilogue == 'softmax' and (in_dtype != 'float32' or allow_tf32): ptx = pgm.asm["ptx"] start = ptx.find("shfl.sync") end = ptx.find("cvt.rn.f16.f32") red_code = ptx[start:end] assert len(red_code) > 0 assert "shared" not in red_code assert "bar.sync" not in red_code # torch result if in_dtype == 'int8': z_ref = np.matmul(x.astype(np.float32), y.astype(np.float32())).astype(np.int32) else: z_ref = np.matmul(x, y) if epilogue == 'add-matrix': z_ref += z if epilogue == 'add-rows': z_ref += z[:, 0][:, None] if epilogue == 'add-cols': z_ref += z[0, :][None, :] if epilogue == 'softmax': num = np.exp(z_ref - np.max(z_ref, axis=-1, keepdims=True)) denom = np.sum(num, axis=-1, keepdims=True) z_ref = num / denom if epilogue == 'chain-dot': z_ref = np.matmul(z_ref, w) # compare # print(z_ref[:,0], z_tri[:,0]) if in_dtype == 'float32': # XXX: Somehow there's a larger difference when we use float32 np.testing.assert_allclose(z_ref, to_numpy(z_tri), rtol=0.01, atol=1e-3) elif out_dtype == tl.float16: np.testing.assert_allclose(z_ref, to_numpy(z_tri), rtol=0.01, atol=1e-3) else: np.testing.assert_allclose(z_ref, to_numpy(z_tri), rtol=0.01) # make sure ld/st are vectorized ptx = pgm.asm['ptx'] if (K > 16 or N > 16 or M > 16) and (M * N // (num_warps * 32) >= 4): # XXX: skip small sizes because they are not vectorized assert 'ld.global.v4' in ptx assert 'st.global.v4' in ptx if in_dtype == 'float32' and allow_tf32: assert 'mma.sync.aligned.m16n8k8.row.col.f32.tf32.tf32.f32' in ptx elif in_dtype == 'float32' and allow_tf32: assert 'mma.sync.aligned.m16n8k8.row.col.f32.tf32.tf32.f32' not in ptx elif in_dtype == 'int8': assert 'mma.sync.aligned.m16n8k32.row.col.satfinite.s32.s8.s8.s32' in ptx elif out_dtype == tl.float16: assert 'mma.sync.aligned.m16n8k16.row.col.f16.f16.f16.f16' in ptx @pytest.mark.parametrize('in_dtype', ['float32']) def test_dot_mulbroadcastred(in_dtype, device): @triton.jit def kernel(Z, X, Y, M: tl.constexpr, N: tl.constexpr, K: tl.constexpr, BM: tl.constexpr, BN: tl.constexpr, BK: tl.constexpr): pidn = tl.program_id(1) pidm = tl.program_id(0) offm = tl.arange(0, BM)[:, None] offn = tl.arange(0, BN)[None, :] offak = tl.arange(0, BK)[None, :] offbk = tl.arange(0, BK)[:, None] acc = tl.full((BM, BN), 0.0, tl.float32) for ridx5 in range(0, K // BK): x = tl.load(X + ((pidm * K * BM) + (offm * K) + (ridx5 * BK) + offak)) y = tl.load(Y + ((pidn * BN) + (offbk * N) + (ridx5 * N * BK) + offn)) x = tl.expand_dims(x, axis=2) y = tl.expand_dims(y, axis=0) t = tl.sum(x * y, axis=1) acc = t + acc tl.store(Z + ((pidm * BM * N) + (pidn * BN) + (offm * N) + offn), acc) M, N, K = 256, 192, 160 BM, BN, BK = 128, 32, 32 rs = RandomState(17) x = numpy_random((M, K), dtype_str=in_dtype, rs=rs) y = numpy_random((K, N), dtype_str=in_dtype, rs=rs) x = x * 0.1 y = y * 0.1 z = numpy_random((M, N), dtype_str=in_dtype, rs=rs) x_tri = to_triton(x, device=device) y_tri = to_triton(y, device=device) z_tri = to_triton(z, device=device) grid = M // BM, N // BN h = kernel[grid](z_tri, x_tri, y_tri, M, N, K, BM, BN, BK) z_ref = np.matmul(x, y) np.testing.assert_allclose(z_ref, to_numpy(z_tri), atol=0.01) assert "tt.dot" in h.asm['ttir'] assert "triton_gpu.async_wait {num = 2 : i32}" in h.asm['ttgir'] @pytest.mark.parametrize("dtype_str", int_dtypes + uint_dtypes + float_dtypes + ['bfloat16']) def test_full(dtype_str, device): if dtype_str in uint_dtypes and not hasattr(torch, dtype_str): # PyTorch only has unsigned 8, but not 16, 32, or 64 dtype = getattr(torch, dtype_str[1:]) # uintx -> intx else: dtype = getattr(torch, dtype_str) check_type_supported(dtype, device) # bfloat16 on cc < 80 will not be tested @triton.jit def kernel_static(out): a = GENERATE_TEST_HERE out_ptr = out + tl.arange(0, 128)[:] tl.store(out_ptr, a) @triton.jit def kernel_dynamic(out, val, dtype: tl.constexpr): a = tl.full((128,), val, dtype) out_ptr = out + tl.arange(0, 128)[:] tl.store(out_ptr, a) kernel_static_patched = patch_kernel(kernel_static, {'GENERATE_TEST_HERE': f"tl.full((128,), 2, tl.{dtype_str})"}) out_static = torch.zeros((128), dtype=dtype, device=device) kernel_static_patched[(1,)](out_static) out_dynamic = torch.zeros((128), dtype=dtype, device=device) kernel_dynamic[(1,)](out_dynamic, 2, getattr(triton.language, dtype_str)) assert torch.all(out_static == 2) assert torch.all(out_dynamic == 2) @pytest.mark.parametrize("literal, dtype_str", [(1e+50, "f64"), (1e+10, "f32"), (1.0, "f32"), ('float("inf")', "f32"), ('float("-inf")', "f32"), ('float("nan")', "f32"), ('float("-nan")', "f32"), (0., "f32"), (5, "i32"), (2**40, "i64"),]) def test_constexpr(literal, dtype_str, device): @triton.jit def kernel(out_ptr): val = GENERATE_TEST_HERE tl.store(out_ptr.to(tl.pointer_type(val.dtype)), val) kernel_patched = patch_kernel(kernel, {'GENERATE_TEST_HERE': f"{literal}"}) out = torch.zeros((1,), dtype=torch.float32, device=device) h = kernel_patched[(1,)](out) assert re.search(r"arith.constant .* : " + dtype_str, h.asm["ttir"]) is not None @pytest.mark.parametrize("dtype_str", ['float32', 'float16']) def test_dot_without_load(dtype_str, device): @triton.jit def _kernel(out): a = GENERATE_TEST_HERE b = GENERATE_TEST_HERE c = tl.dot(a, b) out_ptr = out + tl.arange(0, 32)[:, None] * 32 + tl.arange(0, 32)[None, :] tl.store(out_ptr, c) kernel = patch_kernel(_kernel, {'GENERATE_TEST_HERE': f"tl.full((32, 32), 1.0, tl.{dtype_str})"}) a = torch.ones((32, 32), dtype=getattr(torch, dtype_str), device=device) b = torch.ones((32, 32), dtype=getattr(torch, dtype_str), device=device) out_ref = torch.matmul(a, b) out = torch.zeros((32, 32), dtype=getattr(torch, dtype_str), device=device) kernel[(1,)](out) assert torch.all(out == out_ref) # --------------- # test arange # --------------- @pytest.mark.parametrize("start", [0, 1, 7, 16]) def test_arange(start, device): BLOCK = 128 z_tri = torch.empty(BLOCK, dtype=torch.int32, device=device) @triton.jit def _kernel(z, BLOCK: tl.constexpr, START: tl.constexpr, END: tl.constexpr): off = tl.arange(0, BLOCK) val = tl.arange(START, END) tl.store(z + off, val) _kernel[(1,)](z_tri, START=start, END=start + BLOCK, BLOCK=BLOCK) z_ref = torch.arange(start, BLOCK + start, dtype=torch.int32, device=device) np.testing.assert_allclose(to_numpy(z_tri), to_numpy(z_ref)) # --------------- # test load # --------------- @pytest.mark.parametrize("dtype_str, size, size_diff", [(dtype_str, size, size_diff) for dtype_str in torch_dtypes for size in [128, 512] for size_diff in [0, 1, 2, 3, 4]]) def test_masked_load(dtype_str, size, size_diff, device): dtype = getattr(torch, dtype_str) check_type_supported(dtype, device) # bfloat16 on cc < 80 will not be tested input_size = size - size_diff output_size = size if dtype_str == 'bool': input = torch.randint(0, 2, (input_size,), dtype=dtype, device=device) elif dtype_str in int_dtypes or dtype_str in uint_dtypes: input = torch.randint(0, 127, (input_size,), dtype=dtype, device=device) else: input = torch.rand(input_size, dtype=dtype, device=device) output = torch.zeros((output_size,), dtype=dtype, device=device) @triton.jit def _kernel(in_ptr, out_ptr, in_size: tl.constexpr, out_size: tl.constexpr): in_offsets = tl.arange(0, out_size) # Load inputs. x = GENERATE_TEST_HERE # Store output output_offsets = tl.arange(0, out_size) tl.store(out_ptr + output_offsets, x) mask_str = "mask=in_offsets < in_size, other=1" if size_diff > 0 else "None" kernel = patch_kernel(_kernel, {'GENERATE_TEST_HERE': f"tl.load(in_ptr + in_offsets, {mask_str})"}) kernel[(1,)](input, output, input_size, output_size) reference_out = torch.cat((input, torch.ones((size_diff,), dtype=dtype, device=device))) # print((output - reference_out).nonzero()) torch.testing.assert_allclose(output, reference_out) # Testing masked loads with an intermate copy to shared memory run. @pytest.mark.parametrize("dtype", [torch.bfloat16, torch.float16, torch.float32]) def test_masked_load_shared_memory(dtype, device): check_type_supported(dtype, device) # bfloat16 on cc < 80 will not be tested M = 32 N = 32 K = 16 in1 = torch.rand((M, K), dtype=dtype, device=device) in2 = torch.rand((K, N), dtype=dtype, device=device) out = torch.zeros((M, N), dtype=dtype, device=device) @triton.jit def _kernel(in1_ptr, in2_ptr, output_ptr, in_stride, in2_stride, out_stride, in_numel, in2_numel, out_numel, M: tl.constexpr, N: tl.constexpr, K: tl.constexpr): M_offsets = tl.arange(0, M) N_offsets = tl.arange(0, N) K_offsets = tl.arange(0, K) in_offsets = M_offsets[:, None] * in_stride + K_offsets[None, :] in2_offsets = K_offsets[:, None] * in2_stride + N_offsets[None, :] # Load inputs. x = tl.load(in1_ptr + in_offsets, mask=in_offsets < M * K) w = tl.load(in2_ptr + in2_offsets, mask=in2_offsets < K * N) # Without a dot product the memory doesn't get promoted to shared. o = tl.dot(x, w, out_dtype=tl.float32) # Store output output_offsets = M_offsets[:, None] * out_stride + N_offsets[None, :] tl.store(output_ptr + output_offsets, o, mask=output_offsets < M * N) pgm = _kernel[(1,)](in1, in2, out, in1.stride()[0], in2.stride()[0], out.stride()[0], in1.numel(), in2.numel(), out.numel(), M=M, N=N, K=K) reference_out = torch.matmul(in1, in2) torch.testing.assert_allclose(out, reference_out, atol=1e-2, rtol=0) @pytest.mark.parametrize("cache", ["", ".ca", ".cg"]) def test_load_cache_modifier(cache, device): src = torch.empty(128, device=device) dst = torch.empty(128, device=device) @triton.jit def _kernel(dst, src, CACHE: tl.constexpr): offsets = tl.arange(0, 128) x = tl.load(src + offsets, cache_modifier=CACHE) tl.store(dst + offsets, x) pgm = _kernel[(1,)](dst, src, CACHE=cache) ptx = pgm.asm['ptx'] if cache == '': assert 'ld.global.ca' not in ptx assert 'ld.global.cg' not in ptx if cache == '.cg': assert 'ld.global.cg' in ptx assert 'ld.global.ca' not in ptx if cache == '.ca': assert 'ld.global.ca' in ptx assert 'ld.global.cg' not in ptx @pytest.mark.parametrize("N", [16, 10, 11, 1024]) def test_vectorization(N, device): src = torch.empty(1024, device=device) dst = torch.empty(1024, device=device) @triton.jit def _kernel(dst, src, N, BLOCK_SIZE: tl.constexpr): offsets = tl.program_id(0) * BLOCK_SIZE + tl.arange(0, BLOCK_SIZE) x = tl.load(src + offsets, mask=offsets < N) tl.store(dst + offsets, x, mask=offsets < N) pgm = _kernel[(1,)](dst, src, N=N, BLOCK_SIZE=src.shape[0]) ptx = pgm.asm["ptx"] if N % 16 == 0: assert "ld.global.v4.b32" in ptx else: assert "ld.global.b32" in ptx # np.testing.assert_allclose(dst, src[:N]) @pytest.mark.parametrize("has_hints", [False, True]) def test_vectorization_hints(has_hints, device): src = torch.empty(1024, device=device) dst = torch.empty(1024, device=device) off = torch.zeros(1, device=device, dtype=torch.int32) @triton.jit def _kernel(dst, src, off, N, BLOCK_SIZE: tl.constexpr, HINT: tl.constexpr): offsets = tl.program_id(0) * BLOCK_SIZE + tl.arange(0, BLOCK_SIZE) offsets = offsets + tl.load(off) if HINT: tl.max_contiguous(tl.multiple_of(offsets, 1024), 1024) x = tl.load(src + offsets, mask=offsets < N) tl.store(dst + offsets, x, mask=offsets < N) pgm = _kernel[(1,)](dst, src, off, N=1024, BLOCK_SIZE=src.shape[0], HINT=has_hints) ptx = pgm.asm["ptx"] if has_hints: assert "ld.global.v4.b32" in ptx else: assert "ld.global.v4.b32" not in ptx # --------------- # test store # --------------- @pytest.mark.parametrize("cache", ["", ".wb", ".cg", ".cs", ".wt"]) def test_store_cache_modifier(cache): src = torch.empty(128, device='cuda') dst = torch.empty(128, device='cuda') @triton.jit def _kernel(dst, src, CACHE: tl.constexpr): offsets = tl.arange(0, 128) x = tl.load(src + offsets) tl.store(dst + offsets, x, cache_modifier=CACHE) pgm = _kernel[(1,)](dst, src, CACHE=cache) ptx = pgm.asm['ptx'] if cache == '': assert 'st.global.wb' not in ptx assert 'st.global.cg' not in ptx assert 'st.global.cs' not in ptx assert 'st.global.wt' not in ptx if cache == '.wb': assert 'st.global.wb' in ptx assert 'st.global.cg' not in ptx assert 'st.global.cs' not in ptx assert 'st.global.wt' not in ptx if cache == '.cg': assert 'st.global.wb' not in ptx assert 'st.global.cg' in ptx assert 'st.global.cs' not in ptx assert 'st.global.wt' not in ptx if cache == '.cs': assert 'st.global.wb' not in ptx assert 'st.global.cg' not in ptx assert 'st.global.cs' in ptx assert 'st.global.wt' not in ptx if cache == '.wt': assert 'st.global.wb' not in ptx assert 'st.global.cg' not in ptx assert 'st.global.cs' not in ptx assert 'st.global.wt' in ptx # --------------- # test if # --------------- # --------------- # test for # --------------- # --------------- # test while # --------------- # --------------- # test default # --------------- # TODO: can't be local to test_default @triton.jit def _impl(value=10): return value def test_default(device): value = 5 ret0 = torch.zeros(1, dtype=torch.int32, device=device) ret1 = torch.zeros(1, dtype=torch.int32, device=device) @triton.jit def _kernel(ret0, ret1, value=3): tl.store(ret0, _impl()) tl.store(ret1, _impl(value)) _kernel[(1,)](ret0, ret1, value) assert ret0.item() == 10 assert ret1.item() == value _kernel[(1,)](ret0, ret1) assert ret0.item() == 10 assert ret1.item() == 3 # --------------- # test noop # ---------------- def test_noop(device): @triton.jit def kernel(x): pass x = to_triton(numpy_random((1,), dtype_str='int32'), device=device) kernel[(1, )](x) @pytest.mark.parametrize("device", ['cuda', 'cpu', 'cpu_pinned']) def test_pointer_arguments(device): @triton.jit def kernel(x): pass pin_memory = 'pinned' in device x = torch.empty(1024, device=device.split('_')[0], pin_memory=pin_memory) if device == "cpu": with pytest.raises(ValueError): kernel[(1,)](x) else: kernel[(1, )](x) @pytest.mark.parametrize("value, value_type", [ (-1, 'i32'), (0, 'i32'), (-2**31, 'i32'), (2**31 - 1, 'i32'), (2**31, 'i64'), (2**32 - 1, 'i64'), (2**32, 'i64'), (2**63 - 1, 'i64'), (-2**63, 'i64'), (2**63, 'u64'), (2**64 - 1, 'u64') ]) def test_value_specialization(value: int, value_type: str, device) -> None: spec_type = None def cache_hook(*args, **kwargs): nonlocal spec_type spec_type = kwargs["compile"]["signature"][0] JITFunction.cache_hook = cache_hook @triton.jit def kernel(VALUE, X): pass x = torch.tensor([3.14159], device=device) pgm = kernel[(1, )](value, x) JITFunction.cache_hook = None assert spec_type == value_type # -------------------- # value specialization # -------------------- @pytest.mark.parametrize( "value, overflow", [(2**64 - 1, False), (2**64, True), (-2**63, False), (-2**63 - 1, True)] ) def test_value_specialization_overflow(value: int, overflow: bool, device) -> None: @triton.jit def kernel(VALUE, X): pass x = torch.tensor([3.14159], device=device) if overflow: with pytest.raises(OverflowError): kernel[(1, )](value, x) else: kernel[(1, )](value, x) # ---------------- # test constexpr # ---------------- @pytest.mark.parametrize("op", ['+', '-', '*', '/', '%', '<', '>', '<<', '>>', '&', '^', '|']) @pytest.mark.parametrize("is_lhs_constexpr", [False, True]) @pytest.mark.parametrize("is_rhs_constexpr", [True, False]) def test_bin_op_constexpr(op, is_lhs_constexpr, is_rhs_constexpr, device): @triton.jit def kernel(Z, X, Y): x = tl.load(X) y = tl.load(Y) z = GENERATE_TEST_HERE tl.store(Z, z) if op in ['<<', '>>', '&', '^', '|']: # int op x_str = "3" if is_lhs_constexpr else "x" y_str = "4" if is_rhs_constexpr else "y" x = numpy_random((1,), dtype_str="int32") y = numpy_random((1,), dtype_str="int32") else: x_str = "3.14" if is_lhs_constexpr else "x" y_str = "4.13" if is_rhs_constexpr else "y" x = numpy_random((1,), dtype_str="float32") y = numpy_random((1,), dtype_str="float32") kernel = patch_kernel(kernel, {'GENERATE_TEST_HERE': f"{x_str} {op} {y_str}"}) z = np.array(eval(f"{x_str} {op} {y_str}")) x_tri = to_triton(x, device=device) y_tri = to_triton(y, device=device) z_tri = to_triton(np.empty((1,), dtype=z.dtype), device=device) kernel[(1,)](z_tri, x_tri, y_tri) np.testing.assert_allclose(z, to_numpy(z_tri)) def test_constexpr_shape(device): @triton.jit def kernel(X): off = tl.arange(0, 128 + 128) tl.store(X + off, off) x_tri = to_triton(np.empty((256, ), dtype=np.int32), device=device) kernel[(1,)](x_tri) np.testing.assert_equal(to_numpy(x_tri), np.arange(0, 256)) def test_constexpr_scalar_shape(device): @triton.jit def kernel(X, s): off = tl.arange(0, 256) val = off % (256 // s) tl.store(X + off, val) x_tri = to_triton(np.empty((256, ), dtype=np.int32), device=device) kernel[(1,)](x_tri, 32) np.testing.assert_equal(to_numpy(x_tri), np.arange(0, 256) % 8) # ------------- # test call # ------------- @triton.jit def val_multiplier(val, i): return val * i @triton.jit(noinline=True) def val_multiplier_noinline(val, i): return val * i @triton.jit def vecmul_kernel(ptr, n_elements, rep, type: tl.constexpr): pid = tl.program_id(axis=0) offsets = pid * 128 + tl.arange(0, 128) mask = offsets < n_elements vec = tl.load(ptr + offsets, mask=mask) for i in range(1, rep): if type == "inline": vec = val_multiplier(vec, i) else: vec = val_multiplier_noinline(vec, i) tl.store(ptr + offsets, vec, mask=mask) @pytest.mark.parametrize("type", ["inline", "noinline"]) def test_call(type, device): @triton.jit def kernel(ptr, n_elements, num1, num2, type: tl.constexpr): vecmul_kernel(ptr, n_elements, num1, type) vecmul_kernel(ptr, n_elements, num2, type) size = 1024 rand_val = numpy_random((size,), dtype_str="float32") rand_val_tri = to_triton(rand_val, device=device) err_msg = "" try: kernel[(size // 128,)](rand_val_tri, size, 3, 5, type) except Exception as e: err_msg = str(e) if type == "noinline": assert err_msg is not "" else: ans = rand_val * 1 * 2 * 1 * 2 * 3 * 4 np.testing.assert_equal(to_numpy(rand_val_tri), ans) # ------------- # test if # ------------- @pytest.mark.parametrize("if_type", ["if", "if_exp", "if_and_dynamic", "if_and_static"]) def test_if(if_type, device): @triton.jit def kernel(Cond, XTrue, XFalse, Ret, IfType: tl.constexpr, BoolVar: tl.constexpr, StaticVaue: tl.constexpr): pid = tl.program_id(0) cond = tl.load(Cond) if IfType == "if": if pid % 2 == 0: tl.store(Ret, tl.load(XTrue)) else: tl.store(Ret, tl.load(XFalse)) elif IfType == "if_exp": tl.store(Ret, tl.load(XTrue)) if pid % 2 else tl.store(Ret, tl.load(XFalse)) elif IfType == "if_and_dynamic": if BoolVar and pid % 2 == 0: tl.store(Ret, tl.load(XTrue)) else: tl.store(Ret, tl.load(XFalse)) elif IfType == "if_and_static": if StaticVaue != 0 and StaticVaue != 0: tl.store(Ret, tl.load(XTrue)) else: tl.store(Ret, tl.load(XFalse)) cond = torch.ones(1, dtype=torch.int32, device=device) x_true = torch.tensor([3.14], dtype=torch.float32, device=device) x_false = torch.tensor([1.51], dtype=torch.float32, device=device) ret = torch.empty(1, dtype=torch.float32, device=device) kernel[(1,)](cond, x_true, x_false, ret, if_type, True, 1) assert torch.equal(ret, x_true) def test_num_warps_pow2(device): dst = torch.empty(128, device=device) @triton.jit def _kernel(dst): pass with pytest.raises(AssertionError, match='must be a power of 2'): _kernel[(1,)](dst=dst, num_warps=3) _kernel[(1,)](dst=dst, num_warps=1) _kernel[(1,)](dst=dst, num_warps=2) _kernel[(1,)](dst=dst, num_warps=4) # ------------- # test extern # ------------- @pytest.mark.parametrize("dtype_str, expr, lib_path", [('int32', 'math.ffs', ''), ('float32', 'math.log2', ''), ('float32', 'math.scalbn', ''), ('float32', 'math.pow', tl.math.libdevice_path()), ('float64', 'math.pow_dtype', tl.math.libdevice_path()), ('float64', 'math.norm4d', '')]) def test_math_tensor(dtype_str, expr, lib_path, device): @triton.jit def kernel(X, Y, BLOCK: tl.constexpr): x = tl.load(X + tl.arange(0, BLOCK)) y = GENERATE_TEST_HERE tl.store(Y + tl.arange(0, BLOCK), y) shape = (128, ) rs = RandomState(17) # limit the range of integers so that the sum does not overflow x = numpy_random(shape, dtype_str=dtype_str, rs=rs) if expr == 'math.log2': kernel = patch_kernel(kernel, {'GENERATE_TEST_HERE': f'tl.broadcast_to(tl.{expr}(5.0), x.shape)'}) y_ref = np.log2(5.0) elif expr == 'math.ffs': kernel = patch_kernel(kernel, {'GENERATE_TEST_HERE': f'tl.{expr}(x)'}) y_ref = np.zeros(shape, dtype=x.dtype) for i in range(shape[0]): y_ref[i] = (int(x[i]) & int(-x[i])).bit_length() elif expr == 'math.scalbn': kernel = patch_kernel(kernel, {'GENERATE_TEST_HERE': f'tl.{expr}(x, 2)'}) y_ref = x * pow(2, 2) elif expr == 'math.pow_dtype': x = np.abs(x) kernel = patch_kernel(kernel, {'GENERATE_TEST_HERE': f'tl.math.pow(x, 0.5)'}) y_ref = np.power(x, 0.5) elif expr == 'math.pow': # numpy does not allow negative factors in power, so we use abs() x = np.abs(x) kernel = patch_kernel(kernel, {'GENERATE_TEST_HERE': f'tl.{expr}(x, x)'}) y_ref = np.power(x, x) elif expr == 'math.pow_dtype': x = np.abs(x) kernel = patch_kernel(kernel, {'GENERATE_TEST_HERE': 'tl.math.pow(x, 0.5)'}) y_ref = np.power(x, 0.5) elif expr == 'math.norm4d': kernel = patch_kernel(kernel, {'GENERATE_TEST_HERE': f'tl.{expr}(x, x, x, x)'}) y_ref = np.sqrt(4 * np.power(x, 2)) x_tri = to_triton(x, device=device) # triton result y_tri = to_triton(numpy_random((shape[0],), dtype_str=dtype_str, rs=rs), device=device) kernel[(1,)](x_tri, y_tri, BLOCK=shape[0], extern_libs={'libdevice': lib_path}) # compare if expr == 'math.ffs': np.testing.assert_equal(y_ref, to_numpy(y_tri)) else: np.testing.assert_allclose(y_ref, to_numpy(y_tri), rtol=0.01) @pytest.mark.parametrize("dtype_str, expr, lib_path", [('float32', 'math.pow', ''), ('float64', 'math.pow_dtype', ''), ('float64', 'math.pow', tl.math.libdevice_path())]) def test_math_scalar(dtype_str, expr, lib_path, device): @triton.jit def kernel(X, Y, BLOCK: tl.constexpr): x = X y = GENERATE_TEST_HERE tl.store(Y + tl.arange(0, BLOCK), y) shape = (128, ) rs = RandomState(17) # limit the range of integers so that the sum does not overflow x = numpy_random((1,), dtype_str=dtype_str, rs=rs) y_ref = np.zeros(shape, dtype=x.dtype) # numpy does not allow negative factors in power, so we use abs() if expr == 'math.pow': x = np.abs(x) kernel = patch_kernel(kernel, {'GENERATE_TEST_HERE': 'tl.math.pow(x, x)'}) y_ref[:] = np.power(x, x) elif expr == 'math.pow_dtype': x = np.abs(x) kernel = patch_kernel(kernel, {'GENERATE_TEST_HERE': 'tl.math.pow(x, 0.5)'}) y_ref[:] = np.power(x, 0.5) # triton result x_tri = to_triton(x, device=device)[0].item() y_tri = to_triton(numpy_random((shape[0],), dtype_str=dtype_str, rs=rs), device=device) kernel[(1,)](x_tri, y_tri, BLOCK=shape[0], extern_libs={'libdevice': lib_path}) # compare np.testing.assert_allclose(y_ref, to_numpy(y_tri), rtol=0.01) # ----------------------- # test control flow # ----------------------- @pytest.mark.parametrize("lo, hi, iv", [(2**35, 2**35 + 20, 1), (2**35, 2**35 + 20, 2), (2**35, 2**35 + 20, 3), (15, -16, -1), (15, -16, -2), (15, -16, -3), (-18, -22, -1), (22, 18, -1)]) def test_for_iv(lo, hi, iv, device): @triton.jit def kernel(Out, lo, hi, iv: tl.constexpr): acc = 0 acc = acc.to(tl.int64) for i in range(lo, hi, iv): acc += i tl.store(Out, acc) lo = 2**35 hi = 2**35 + 20 out = to_triton(np.zeros((1,), dtype=np.int64), device=device) kernel[(1,)](out, lo, hi, iv) assert out[0] == sum(range(lo, hi, iv)) def test_if_else(device): @triton.jit def kernel(Cond, TrueVal, FalseVal, Out): if tl.load(Cond): val = tl.load(TrueVal) else: val = tl.load(FalseVal) tl.store(Out, val) out = to_triton(np.zeros((1,), dtype=np.int32), device=device) true_val = to_triton(np.full((1,), 1, dtype=np.int32), device=device) false_val = to_triton(np.full((1,), 2, dtype=np.int32), device=device) cond = to_triton(np.zeros((1,), dtype=np.int32), device=device) # True cond[0] = True kernel[(1,)](cond, true_val, false_val, out) assert to_numpy(out)[0] == true_val[0] # False cond[0] = False kernel[(1,)](cond, true_val, false_val, out) assert to_numpy(out)[0] == false_val[0] @pytest.mark.parametrize("mode", ["dynamic", "static"]) def test_if_return(mode, device): @triton.jit def kernel(ExitEarly, Out, cond: tl.constexpr, mode: tl.constexpr): if mode == "dynamic": if tl.load(ExitEarly): tl.store(Out, 0) return else: if cond: tl.store(Out, 0) return tl.store(Out, 1) out = to_triton(np.zeros((1,), dtype=np.int32), device=device) exit_early = to_triton(np.zeros((1,), dtype=np.int32), device=device) # exit early path taken exit_early[0] = 1 kernel[(1,)](exit_early, out, True, mode) assert to_numpy(out)[0] == 0 # exit early path not taken exit_early[0] = 0 kernel[(1,)](exit_early, out, False, mode) assert to_numpy(out)[0] == 1 @triton.jit def add_fn(x): return x + 1 @triton.jit(noinline=True) def add_fn_noinline(x): return x + 1 @triton.jit def add_fn_return(x, pid): if pid == 0: return x + 1 else: return x + 2 @triton.jit def add_fn_expr(Out, x): tl.store(Out, x) @triton.jit def add_fn_static_cond(x, cond: tl.constexpr): if cond == "": return x else: return x + 1 @pytest.mark.parametrize("call_type", ["attribute", "attribute_jit", "jit", "jit_if", "jit_ifexp", "jit_expr", "jit_static_cond", "jit_noinline", "jit_extern"]) def test_if_call(call_type, device): @triton.jit def kernel(Out, call_type: tl.constexpr): pid = tl.program_id(0) o = tl.load(Out) if call_type == "attribute": # call attribute if pid == 0: a = o a = a.to(tl.int32).to(tl.int32) + 1 o = a elif call_type == "attribute_jit": # call attribute and jit function if pid == 0: a = o a = tl.load(Out + add_fn(a) - 1).to(tl.int32) + 1 o = a elif call_type == "jit": if pid == 0: # regular function call a = o a = add_fn(a) o = a elif call_type == "jit_if": # function without end_if block if pid == 0: a = o a = add_fn_return(a, pid) o = a elif call_type == "jit_ifexp": # ifexp expression if pid == 0: a = o a = add_fn(a) if pid == 0 else add_fn_return(a, pid) o = a elif call_type == "jit_expr": # call without return if pid == 0: a = o + 1 add_fn_expr(Out, a) o = a elif call_type == "jit_static_cond": if pid == 0: a = o + 1 add_fn_static_cond(o, call_type) o = a elif call_type == "jit_noinline": if pid == 0: a = o + 1 add_fn_noinline(a) o = a elif call_type == "jit_extern": if pid == 0: a = o + 1 tl.cdiv(a, a) o = a tl.store(Out, o) out = to_triton(np.zeros((1,), dtype=np.int32), device=device) kernel[(1,)](out, call_type) assert to_numpy(out)[0] == 1 @pytest.mark.parametrize("_cond1", [True, False]) @pytest.mark.parametrize("_cond2", [True, False]) @pytest.mark.parametrize("_cond3", [True, False]) def test_nested_if_else_return(_cond1, _cond2, _cond3, device): @triton.jit def kernel(Cond1, Cond2, Cond3, Val1, Val2, Val3, Out): val = 0 if tl.load(Cond1): if tl.load(Cond2): val = tl.load(Val1) else: return else: if tl.load(Cond3): val = tl.load(Val2) else: val = tl.load(Val3) tl.store(Out, val) out = to_triton(np.full((1,), -1, dtype=np.int32), device=device) cond1 = to_triton(np.full((1,), _cond1, dtype=np.int32), device=device) cond2 = to_triton(np.full((1,), _cond2, dtype=np.int32), device=device) cond3 = to_triton(np.full((1,), _cond3, dtype=np.int32), device=device) val1 = to_triton(np.full((1,), 1, dtype=np.int32), device=device) val2 = to_triton(np.full((1,), 2, dtype=np.int32), device=device) val3 = to_triton(np.full((1,), 3, dtype=np.int32), device=device) kernel[(1,)](cond1, cond2, cond3, val1, val2, val3, out) targets = { (True, True, True): val1[0], (True, True, False): val1[0], (True, False, True): out[0], (True, False, False): out[0], (False, True, True): val2[0], (False, True, False): val3[0], (False, False, True): val2[0], (False, False, False): val3[0], } assert out[0] == targets[(_cond1, _cond2, _cond3)] def test_while(device): @triton.jit def kernel(InitI, Bound, CutOff, OutI, OutInitI, OutJ): init_i = tl.load(InitI) curr_i = init_i j = 0 # Check that init_i is not updated by the loop while j < tl.load(Bound): curr_i = curr_i + (j == tl.load(CutOff)) j += 1 tl.store(OutInitI, init_i) tl.store(OutI, curr_i) tl.store(OutJ, j) out_i = to_triton(np.zeros((1,), dtype=np.int32), device=device) out_j = to_triton(np.zeros((1,), dtype=np.int32), device=device) init_i = to_triton(np.full((1,), 1, dtype=np.int32), device=device) out_init_i = to_triton(np.full((1,), 0, dtype=np.int32), device=device) bound = to_triton(np.full((1,), 10, dtype=np.int32), device=device) cut_off = to_triton(np.full((1,), 5, dtype=np.int32), device=device) kernel[(1,)](init_i, bound, cut_off, out_i, out_init_i, out_j) assert out_init_i[0] == init_i[0] assert out_i[0] == init_i[0] + 1 assert out_j[0] == bound[0] def test_while(device): @triton.jit def nested_while(data, countPtr): for i in range(10): count = tl.load(countPtr) while count > 0: tl.store(data, tl.load(data) + 1.0) count = count - 2 counter = torch.tensor([8], dtype=torch.int32, device=device) data = torch.zeros((1,), device=device, dtype=torch.float32) nested_while[(1,)](data, counter) assert data[0] == 40 # def test_for_if(device): # @triton.jit # def kernel(bound, cutoff, M, N): # m = 0 # n = 0 # for i in range(bound): # if i > cutoff: # m = m + 1 # else: # n = n + 1 # tl.store(M, m) # tl.store(N, n) # m = to_triton(np.zeros((1,), dtype=np.int32), device=device) # n = to_triton(np.zeros((1,), dtype=np.int32), device=device) # kernel[(1,)](10, 7, m, n) # print(m[0]) # print(n[0]) # ----------------------- # test extra # ----------------------- def test_globaltimer(device): check_cuda_only(device) @triton.jit def kernel(Out1, Out2): start = tl.extra.cuda.globaltimer() off = tl.arange(0, 128) for i in range(10000): tl.store(Out1 + off, tl.load(Out1 + off) + 1) end = tl.extra.cuda.globaltimer() tl.store(Out2, end - start) out1 = to_triton(np.zeros((128,), dtype=np.int64), device=device) out2 = to_triton(np.zeros((1,), dtype=np.int64), device=device) h = kernel[(1,)](out1, out2) assert out2[0] > 0 # 2 inlined globaltimers + one extra in the wrapper extern function assert h.asm["ptx"].count("%globaltimer") == 3 def test_smid(device): check_cuda_only(device) @triton.jit def kernel(Out): tl.store(Out + tl.program_id(0), tl.extra.cuda.smid()) out = to_triton(np.zeros((1024,), dtype=np.int32), device=device) h = kernel[(out.shape[0],)](out) assert out.sort()[0].unique().shape[0] > 0 assert h.asm["ptx"].count("%smid") == 2 # ----------------------- # test layout conversions # ----------------------- # TODO: backend should be tested separately layouts = [ # MmaLayout(version=1, warps_per_cta=[1, 4]), MmaLayout(version=(2, 0), warps_per_cta=[1, 4]), # MmaLayout(version=1, warps_per_cta=[4, 1]), MmaLayout(version=(2, 0), warps_per_cta=[4, 1]), BlockedLayout([1, 8], [2, 16], [4, 1], [1, 0]), BlockedLayout([1, 4], [4, 8], [2, 2], [1, 0]), BlockedLayout([1, 1], [1, 32], [2, 2], [1, 0]), BlockedLayout([8, 1], [16, 2], [1, 4], [0, 1]), BlockedLayout([4, 1], [8, 4], [2, 2], [0, 1]), BlockedLayout([1, 1], [32, 1], [2, 2], [0, 1]), BlockedLayout([4, 4], [1, 32], [4, 1], [1, 0]) ] intermediate_layouts = [ None, SharedLayout(1, 1, 1, [1, 0]), SharedLayout(4, 2, 4, [1, 0]), SharedLayout(2, 2, 4, [1, 0]), ] @pytest.mark.parametrize("shape", [(128, 128)]) @pytest.mark.parametrize("dtype", ['float16']) @pytest.mark.parametrize("src_layout", layouts) @pytest.mark.parametrize("interm_layout", intermediate_layouts) @pytest.mark.parametrize("dst_layout", layouts) def test_convert2d(dtype, shape, src_layout, interm_layout, dst_layout, device): if str(src_layout) == str(dst_layout): pytest.skip() if 'mma' in str(src_layout) and 'mma' in str(dst_layout): pytest.skip() layouts = f""" #src = {src_layout} #dst = {dst_layout} """ if interm_layout is None else f""" #src = {src_layout} #interm = {interm_layout} #dst = {dst_layout} """ conversion = f""" %12 = triton_gpu.convert_layout %9 : (tensor<128x128xi32, #src>) -> tensor<128x128xi32, #dst> %13 = triton_gpu.convert_layout %11 : (tensor<128x128xf16, #src>) -> tensor<128x128xf16, #dst> """ if interm_layout is None else f""" %15 = triton_gpu.convert_layout %9 : (tensor<128x128xi32, #src>) -> tensor<128x128xi32, #interm> %16 = triton_gpu.convert_layout %15 : (tensor<128x128xi32, #interm>) -> tensor<128x128xi32, #src> %17 = triton_gpu.convert_layout %11 : (tensor<128x128xf16, #src>) -> tensor<128x128xf16, #interm> %18 = triton_gpu.convert_layout %17 : (tensor<128x128xf16, #interm>) -> tensor<128x128xf16, #src> %12 = triton_gpu.convert_layout %16 : (tensor<128x128xi32, #src>) -> tensor<128x128xi32, #dst> %13 = triton_gpu.convert_layout %18 : (tensor<128x128xf16, #src>) -> tensor<128x128xf16, #dst> """ ir = layouts + """ module attributes {"triton_gpu.num-warps" = 4 : i32} { tt.func public @kernel_0d1d(%arg0: !tt.ptr<f16> {tt.divisibility = 16 : i32}, %arg1: !tt.ptr<f16> {tt.divisibility = 16 : i32}) { %cst = arith.constant dense<128> : tensor<128x1xi32, #src> %0 = tt.make_range {end = 128 : i32, start = 0 : i32} : tensor<128xi32, #triton_gpu.slice<{dim = 1, parent = #src}>> %1 = tt.make_range {end = 128 : i32, start = 0 : i32} : tensor<128xi32, #triton_gpu.slice<{dim = 0, parent = #src}>> %2 = tt.splat %arg0 : (!tt.ptr<f16>) -> tensor<128x128x!tt.ptr<f16>, #src> %4 = tt.expand_dims %0 {axis = 1 : i32} : (tensor<128xi32, #triton_gpu.slice<{dim = 1, parent = #src}>>) -> tensor<128x1xi32, #src> %5 = arith.muli %4, %cst : tensor<128x1xi32, #src> %6 = tt.expand_dims %1 {axis = 0 : i32} : (tensor<128xi32, #triton_gpu.slice<{dim = 0, parent = #src}>>) -> tensor<1x128xi32, #src> %7 = tt.broadcast %6 : (tensor<1x128xi32, #src>) -> tensor<128x128xi32, #src> %8 = tt.broadcast %5 : (tensor<128x1xi32, #src>) -> tensor<128x128xi32, #src> %9 = arith.addi %8, %7 : tensor<128x128xi32, #src> %10 = tt.addptr %2, %9 : tensor<128x128x!tt.ptr<f16>, #src>, tensor<128x128xi32, #src> %11 = tt.load %10 {cache = 1 : i32, evict = 1 : i32, isVolatile = false} : tensor<128x128xf16, #src> %3 = tt.splat %arg1 : (!tt.ptr<f16>) -> tensor<128x128x!tt.ptr<f16>, #dst> """ + conversion + """ %14 = tt.addptr %3, %12 : tensor<128x128x!tt.ptr<f16>, #dst>, tensor<128x128xi32, #dst> tt.store %14, %13 : tensor<128x128xf16, #dst> tt.return } } """ x = to_triton(numpy_random(shape, dtype_str=dtype), device=device) z = torch.empty_like(x) # write the IR to a temporary file using mkstemp import tempfile with tempfile.NamedTemporaryFile(mode='w', suffix='.ttgir') as f: f.write(ir) f.flush() kernel = triton.compile(f.name) kernel[(1, 1, 1)](x.data_ptr(), z.data_ptr()) assert torch.equal(z, x) def test_load_scalar_with_mask(device): @triton.jit def kernel(Input, Index, Out, N: int): index = tl.load(Index) scalar = tl.load(Input + index, mask=index < N, other=0) tl.store(Out, scalar, mask=index < N) Index = torch.tensor([0], dtype=torch.int32, device=device) Input = torch.tensor([0], dtype=torch.int32, device=device) Out = torch.empty_like(Index, device=device) kernel[(1,)](Input, Index, Out, Index.numel()) assert Out.data[0] == 0 # This test is used to test our own PTX codegen for float16 and int16 conversions # maybe delete it later after ptxas has been fixed @pytest.mark.parametrize("dtype_str", ['float16', 'int16']) def test_ptx_cast(dtype_str, device): @triton.jit def kernel(in_ptr0, out_ptr2, xnumel, rnumel, dtype: tl.constexpr, XBLOCK: tl.constexpr, RBLOCK: tl.constexpr): xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:, None] xmask = xindex < xnumel rbase = tl.arange(0, RBLOCK)[None, :] x0 = xindex _tmp4 = (tl.zeros([XBLOCK, RBLOCK], dtype) - 10000).to(dtype) for roffset in range(0, rnumel, RBLOCK): rindex = roffset + rbase rmask = rindex < rnumel r1 = rindex tmp0 = tl.load(in_ptr0 + (r1 + (197 * x0)), rmask & xmask).to(dtype) tmp1 = 2 tmp2 = tmp0 * tmp1 tmp3 = tmp2.to(dtype) tmp5 = _tmp4 < tmp3 _tmp4 = tl.where(rmask & xmask & tmp5, tmp3, _tmp4) tl.store(out_ptr2 + (r1 + (197 * x0) + tl.zeros([XBLOCK, RBLOCK], tl.int32)), _tmp4, rmask & xmask) torch.manual_seed(123) if dtype_str == 'int16': torch_dtype = torch.int16 triton_dtype = tl.int32 else: torch_dtype = torch.float16 triton_dtype = tl.float32 s0 = 4 buf11 = -torch.ones((6 * s0, 197, 197), device=device, dtype=torch_dtype) buf14 = -torch.ones((s0, 6, 197, 197), device=device, dtype=torch_dtype) kernel[(4728,)](buf11, buf14, 1182 * s0, 197, triton_dtype, 1, 256, num_warps=2) assert buf14.to(torch.float32).mean() == -2.0
125,776
37.033565
204
py
triton
triton-main/python/test/unit/language/assert_helper.py
import sys import torch from torch.testing import assert_close import triton import triton.language as tl @triton.jit def kernel_device_assert(X, Y, BLOCK: tl.constexpr): x = tl.load(X + tl.arange(0, BLOCK)) tl.device_assert(x == 0, "x != 0") tl.store(Y + tl.arange(0, BLOCK), x) @triton.jit def kernel_device_assert_scalar(X, Y, BLOCK: tl.constexpr): x = tl.load(X + tl.arange(0, BLOCK)) # Trivial assert tl.device_assert(0 == 0, "x != 0") tl.store(Y + tl.arange(0, BLOCK), x) @triton.jit(debug=False) def kernel_device_assert_no_debug(X, Y, BLOCK: tl.constexpr): x = tl.load(X + tl.arange(0, BLOCK)) tl.device_assert(x == 0, "x != 0") tl.store(Y + tl.arange(0, BLOCK), x) @triton.jit def kernel_assert(X, Y, BLOCK: tl.constexpr): x = tl.load(X + tl.arange(0, BLOCK)) assert x == 0, "x != 0" tl.store(Y + tl.arange(0, BLOCK), x) @triton.jit def kernel_static_assert(X, Y, BLOCK: tl.constexpr): x = tl.load(X + tl.arange(0, BLOCK)) tl.static_assert(BLOCK == 128, "BLOCK != 128") tl.store(Y + tl.arange(0, BLOCK), x) def test_assert(func: str): shape = (128, ) x = torch.arange(0, shape[0], dtype=torch.int32, device='cuda') y = torch.zeros(shape, dtype=x.dtype, device="cuda") if func == "device_assert": kernel_device_assert[(1,)](x, y, BLOCK=shape[0]) kernel_device_assert_scalar[(1,)](x, y, BLOCK=shape[0]) elif func == "no_debug": # TRITON_DEBUG=1 can override the debug flag kernel_device_assert_no_debug[(1,)](x, y, BLOCK=shape[0]) elif func == "assert": kernel_assert[(1,)](x, y, BLOCK=shape[0]) elif func == "static_assert": kernel_static_assert[(1,)](x, y, BLOCK=shape[0]) assert_close(y, x) @triton.jit def jit_device_assert_none(x): tl.device_assert(x == 0, "x != 0") @triton.jit(debug=True) def jit_device_assert_true(x): tl.device_assert(x == 0, "x != 0") @triton.jit(debug=False) def jit_device_assert_false(x): tl.device_assert(x == 0, "x != 0") @triton.jit def kernel_device_assert_nested(X, Y, BLOCK: tl.constexpr, jit_debug: tl.constexpr): x = tl.load(X + tl.arange(0, BLOCK)) if jit_debug == "true": jit_device_assert_true(x) elif jit_debug == "false": jit_device_assert_false(x) else: jit_device_assert_none(x) tl.store(Y + tl.arange(0, BLOCK), x) @triton.jit(debug=True) def kernel_device_assert_nested_true(X, Y, BLOCK: tl.constexpr, jit_debug: tl.constexpr): x = tl.load(X + tl.arange(0, BLOCK)) if jit_debug == "true": jit_device_assert_true(x) elif jit_debug == "false": jit_device_assert_false(x) else: jit_device_assert_none(x) tl.store(Y + tl.arange(0, BLOCK), x) @triton.jit(debug=False) def kernel_device_assert_nested_false(X, Y, BLOCK: tl.constexpr, jit_debug: tl.constexpr): x = tl.load(X + tl.arange(0, BLOCK)) if jit_debug == "true": jit_device_assert_true(x) elif jit_debug == "false": jit_device_assert_false(x) else: jit_device_assert_none(x) tl.store(Y + tl.arange(0, BLOCK), x) def test_assert_nested(caller: str, callee: str): shape = (128, ) x = torch.arange(0, shape[0], dtype=torch.int32, device='cuda') y = torch.zeros(shape, dtype=x.dtype, device="cuda") if caller == "none": kernel_device_assert_nested[(1,)](x, y, BLOCK=shape[0], jit_debug=callee) elif caller == "true": kernel_device_assert_nested_true[(1,)](x, y, BLOCK=shape[0], jit_debug=callee) elif caller == "false": kernel_device_assert_nested_false[(1,)](x, y, BLOCK=shape[0], jit_debug=callee) assert_close(y, x) if __name__ == "__main__": if len(sys.argv) == 3: test_assert_nested(sys.argv[1], sys.argv[2]) else: test_assert(sys.argv[1])
3,841
28.106061
90
py
triton
triton-main/python/test/unit/language/test_random.py
import numpy as np import pytest import scipy.stats import torch import triton import triton.language as tl ##################################### # Reference Philox Implementation ##################################### class PhiloxConfig: def __init__(self, PHILOX_ROUND_A, PHILOX_ROUND_B, PHILOX_KEY_A, PHILOX_KEY_B, DTYPE): self.PHILOX_ROUND_A = np.array(PHILOX_ROUND_A, dtype=DTYPE) self.PHILOX_ROUND_B = np.array(PHILOX_ROUND_B, dtype=DTYPE) self.PHILOX_KEY_A = np.array(PHILOX_KEY_A, dtype=DTYPE) self.PHILOX_KEY_B = np.array(PHILOX_KEY_B, dtype=DTYPE) self.DTYPE = DTYPE # This is better for GPU PHILOX_32 = PhiloxConfig( PHILOX_KEY_A=0x9E3779B9, PHILOX_KEY_B=0xBB67AE85, PHILOX_ROUND_A=0xD2511F53, PHILOX_ROUND_B=0xCD9E8D57, DTYPE=np.uint32, ) # This is what numpy implements PHILOX_64 = PhiloxConfig( PHILOX_KEY_A=0x9E3779B97F4A7C15, PHILOX_KEY_B=0xBB67AE8584CAA73B, PHILOX_ROUND_A=0xD2E7470EE14C6C93, PHILOX_ROUND_B=0xCA5A826395121157, DTYPE=np.uint64, ) class CustomPhilox4x: def __init__(self, seed, config): self._config = config seed = self._into_pieces(seed) self._key = np.array(seed[:2], dtype=self._dtype) self._counter = np.array((0, 0) + seed[2:], dtype=self._dtype) @property def _dtype(self): return self._config.DTYPE def _into_pieces(self, n, pad=4): res = [] while len(res) < pad: res.append(np.array(n, dtype=self._dtype)) n >>= (np.dtype(self._dtype).itemsize * 8) assert n == 0 return tuple(res) def _multiply_low_high(self, a, b): low = a * b high = int(a) * int(b) high = np.array(high >> (np.dtype(self._dtype).itemsize * 8), dtype=self._dtype) return low, high def _single_round(self, counter, key): lo0, hi0 = self._multiply_low_high(self._config.PHILOX_ROUND_A, counter[0]) lo1, hi1 = self._multiply_low_high(self._config.PHILOX_ROUND_B, counter[2]) ret0 = hi1 ^ counter[1] ^ key[0] ret1 = lo1 ret2 = hi0 ^ counter[3] ^ key[1] ret3 = lo0 return np.array([ret0, ret1, ret2, ret3], dtype=self._dtype) def _raise_key(self, key): pk = [self._config.PHILOX_KEY_A, self._config.PHILOX_KEY_B] return key + np.array(pk, dtype=self._dtype) def random_raw(self): counter = self._counter key = self._key for _ in range(10): counter = self._single_round(counter, key) key = self._raise_key(key) self.advance(1) return counter def advance(self, n_steps): self._counter[0] += n_steps assert self._counter[0] < 2**32, "FIXME: doesn't work for large offsets" class CustomPhilox(CustomPhilox4x): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.buffer = [] def random_raw(self): if len(self.buffer) == 0: self.buffer = list(super().random_raw())[::-1] return int(self.buffer.pop()) ##################################### # Unit Tests ##################################### BLOCK = 1024 # test generation of random uint32 @pytest.mark.parametrize('size, seed', [(size, seed) for size in ['10', '4,53', '10000'] for seed in [0, 42, 124, 54, 0xffffffff, 0xdeadbeefcafeb0ba]] ) def test_randint(size, seed, device): size = list(map(int, size.split(','))) @triton.jit def kernel(X, N, seed): offset = tl.program_id(0) * BLOCK + tl.arange(0, BLOCK) rand = tl.randint(seed, offset) tl.store(X + offset, rand, mask=offset < N) # triton result x = torch.empty(size, dtype=torch.int32, device=device) N = x.numel() grid = (triton.cdiv(N, BLOCK),) kernel[grid](x, N, seed) out_tri = x.cpu().numpy().astype(np.uint32).flatten().tolist() # reference result gen = CustomPhilox4x(seed, config=PHILOX_32) out_ref = [gen.random_raw()[0] for _ in out_tri] assert out_tri == out_ref # test uniform PRNG @pytest.mark.parametrize('size, seed', [(size, seed) for size in [1000000] for seed in [0, 42, 124, 54]] ) def test_rand(size, seed, device): @triton.jit def kernel(X, N, seed): offset = tl.program_id(0) * BLOCK + tl.arange(0, BLOCK) rand = tl.rand(seed, offset) tl.store(X + offset, rand, mask=offset < N) # triton result x = torch.empty(size, dtype=torch.float32, device=device) N = x.numel() grid = (triton.cdiv(N, BLOCK),) kernel[grid](x, N, seed) assert all((x >= 0) & (x <= 1)) assert scipy.stats.kstest(x.tolist(), 'uniform', args=(0, 1)).statistic < 0.01 # test normal PRNG @pytest.mark.parametrize('size, seed', [(size, seed) for size in [1000000] for seed in [0, 42, 124, 54]] ) def test_randn(size, seed, device): @triton.jit def kernel(X, N, seed): offset = tl.program_id(0) * BLOCK + tl.arange(0, BLOCK) rand = tl.randn(seed, offset) tl.store(X + offset, rand, mask=offset < N) # triton result x = torch.empty(size, dtype=torch.float32, device=device) N = x.numel() grid = (triton.cdiv(N, BLOCK),) kernel[grid](x, N, seed) assert abs(x.mean()) < 1e-2 assert abs(x.std() - 1) < 1e-2 # tl.rand() should never produce >=1.0 def test_rand_limits(device): @triton.jit def kernel(input, output, n: tl.constexpr): idx = tl.arange(0, n) x = tl.load(input + idx) y = tl.random.uint32_to_uniform_float(x) tl.store(output + idx, y) min_max_int32 = torch.tensor([ torch.iinfo(torch.int32).min, torch.iinfo(torch.int32).max, ], dtype=torch.int32, device=device) output = torch.empty(2, dtype=torch.float32, device=device) kernel[(1,)](min_max_int32, output, 2) assert output[0] == output[1] assert 1.0 - torch.finfo(torch.float32).eps <= output[0].item() < 1.0
6,178
30.050251
90
py
triton
triton-main/python/test/unit/language/print_helper.py
import sys import torch from torch.testing import assert_close import triton import triton.language as tl @triton.jit def kernel_device_print(X, Y, BLOCK: tl.constexpr): x = tl.load(X + tl.arange(0, BLOCK)) tl.device_print("", x) tl.store(Y + tl.arange(0, BLOCK), x) @triton.jit def kernel_print(X, Y, BLOCK: tl.constexpr): x = tl.load(X + tl.arange(0, BLOCK)) print("", x) tl.store(Y + tl.arange(0, BLOCK), x) @triton.jit def kernel_static_print(X, Y, BLOCK: tl.constexpr): x = tl.load(X + tl.arange(0, BLOCK)) tl.static_print(x) tl.store(Y + tl.arange(0, BLOCK), x) def test_print(func: str, data_type: str): shape = (128, ) # limit the range of integers so that the sum does not overflow x = torch.arange(0, shape[0], dtype=torch.int32, device='cuda').to(getattr(torch, data_type)) y = torch.zeros(shape, dtype=x.dtype, device="cuda") if func == "device_print": kernel_device_print[(1,)](x, y, BLOCK=shape[0]) elif func == "print": kernel_print[(1,)](x, y, BLOCK=shape[0]) elif func == "static_print": kernel_static_print[(1,)](x, y, BLOCK=shape[0]) assert_close(y, x) if __name__ == "__main__": test_print(sys.argv[1], sys.argv[2])
1,244
25.489362
97
py
triton
triton-main/python/test/unit/language/test_subprocess.py
import os import subprocess import sys import pytest dir_path = os.path.dirname(os.path.realpath(__file__)) print_path = os.path.join(dir_path, "print_helper.py") assert_path = os.path.join(dir_path, "assert_helper.py") # TODO: bfloat16 after LLVM-15 assert_types = ["device_assert", "assert", "static_assert", "no_debug"] nested_types = [(caller, callee) for caller in ["true", "false", "none"] for callee in ["true", "false", "none"]] torch_types = ["int8", "uint8", "int16", "int32", "long", "float16", "float32", "float64"] @pytest.mark.parametrize("func_type, data_type", [("device_print", data_type) for data_type in torch_types] + [("print", "int32"), ("static_print", "int32")]) def test_print(func_type: str, data_type: str): proc = subprocess.Popen([sys.executable, print_path, func_type, data_type], stdout=subprocess.PIPE, shell=False) outs, _ = proc.communicate() outs = outs.split() new_lines = set() for line in outs: try: value = line if func_type != "static_print": value = int(float(line)) new_lines.add(value) except Exception as e: print(e) if func_type != "static_print": for i in range(128): assert i in new_lines assert len(new_lines) == 128 else: assert len(new_lines) == 1 @pytest.mark.parametrize("func_type", assert_types) def test_assert(func_type: str): os.environ["TRITON_DEBUG"] = "1" proc = subprocess.Popen([sys.executable, assert_path, func_type], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False) _, errs = proc.communicate() errs = errs.splitlines() num_errs = 0 for err in errs: if "x != 0" in err.decode("utf-8"): num_errs += 1 os.environ["TRITON_DEBUG"] = "0" if func_type != "static_assert": assert num_errs == 127 else: assert num_errs == 0 @pytest.mark.parametrize("caller_type, callee_type", nested_types) def test_assert_nested(caller_type, callee_type): proc = subprocess.Popen([sys.executable, assert_path, caller_type, callee_type], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False) _, errs = proc.communicate() errs = errs.splitlines() num_errs = 0 for err in errs: if "x != 0" in err.decode("utf-8"): num_errs += 1 if caller_type == "none": if callee_type == "true": assert num_errs == 127 else: assert num_errs == 0 elif caller_type == "true": if callee_type == "false": assert num_errs == 0 else: assert num_errs == 127 elif caller_type == "false": if callee_type == "true": assert num_errs == 127 else: assert num_errs == 0
2,820
33.82716
145
py
triton
triton-main/python/test/unit/language/test_block_pointer.py
import pytest import torch import triton import triton.language as tl @triton.jit def block_copy_kernel(a_ptr, b_ptr, N, BLOCK_SIZE: tl.constexpr, padding_option: tl.constexpr): pid = tl.program_id(0) # We only copy half of the data to see if the padding works a_block_ptr = tl.make_block_ptr(base=a_ptr, shape=(N // 2, ), strides=(1, ), offsets=(pid * BLOCK_SIZE, ), block_shape=(BLOCK_SIZE, ), order=(0, )) b_block_ptr = tl.make_block_ptr(base=b_ptr, shape=(N, ), strides=(1, ), offsets=(pid * BLOCK_SIZE, ), block_shape=(BLOCK_SIZE, ), order=(0, )) a = tl.load(a_block_ptr, boundary_check=(0, ), padding_option=padding_option) tl.store(b_block_ptr, a, boundary_check=(0, )) @pytest.mark.parametrize("dtype_str, n, padding_option", [(dtype_str, n, padding) for dtype_str in ("bool", "int16", "float16") for n in (64, 128, 256, 512, 1024) for padding in ("zero", "nan")]) def test_block_copy(dtype_str, n, padding_option): capability = torch.cuda.get_device_capability() if capability[0] >= 9: pytest.skip("Hopper support is working in progress") dtype = getattr(torch, dtype_str) if dtype_str in ("bool", "int16"): if padding_option == "nan": pytest.skip("Padding with NaN is not supported for integer types") a = torch.randint(0, 2, (n, ), device="cuda", dtype=dtype) else: a = torch.randn((n, ), device="cuda", dtype=dtype) b = torch.zeros((n, ), device="cuda", dtype=dtype) grid = lambda meta: (triton.cdiv(n, meta["BLOCK_SIZE"]),) block_copy_kernel[grid](a_ptr=a, b_ptr=b, N=n, BLOCK_SIZE=64, padding_option=padding_option) assert torch.all(a[0: n // 2] == b[0: n // 2]) if padding_option == "zero": assert torch.all(b[n // 2: n] == 0) else: assert torch.all(torch.isnan(b[n // 2: n])) @triton.jit def matmul_no_scf_with_advance_kernel( a_ptr, b_ptr, c_ptr, M, N, K, stride_am, stride_ak, stride_bk, stride_bn, stride_cm, stride_cn, BLOCK_M: tl.constexpr, BLOCK_N: tl.constexpr, BLOCK_K: tl.constexpr ): offs_m = tl.arange(0, BLOCK_M) offs_n = tl.arange(0, BLOCK_N) a_block_ptr = tl.make_block_ptr(base=a_ptr, shape=(M, K), strides=(stride_am, stride_ak), offsets=(0, 0), block_shape=(BLOCK_M, BLOCK_K), order=(1, 0)) b_block_ptr = tl.make_block_ptr(base=b_ptr, shape=(K, N), strides=(stride_bk, stride_bn), offsets=(0, 0), block_shape=(BLOCK_K, BLOCK_N), order=(1, 0)) # Below two lines are just for testing negative offsets for the `advance` API, which could be removed a_block_ptr = tl.advance(a_block_ptr, (BLOCK_M, -BLOCK_K)) a_block_ptr = tl.advance(a_block_ptr, (-BLOCK_M, BLOCK_K)) a = tl.load(a_block_ptr, boundary_check=(1, ), padding_option="zero") b = tl.load(b_block_ptr, boundary_check=(0, ), padding_option="zero") c = tl.dot(a, b) c_ptrs = c_ptr + offs_m[:, None] * stride_cm + offs_n[None, :] * stride_cn tl.store(c_ptrs, c) @pytest.mark.parametrize("shape, num_warps", [ (shape, num_warps) for shape in [ [64, 64, 16], [64, 64, 32], [64, 64, 64], ] for num_warps in [4, 8] ]) def test_block_ptr_matmul_no_scf(shape, num_warps): capability = torch.cuda.get_device_capability() if capability[0] >= 9: pytest.skip("Hopper support is working in progress") m, n, k = shape a = torch.randn((m, k), device="cuda", dtype=torch.float16) b = torch.randn((k, n), device="cuda", dtype=torch.float16) c = torch.empty((m, n), device="cuda", dtype=torch.float32) grid = lambda META: (1, ) matmul_no_scf_with_advance_kernel[grid](a_ptr=a, b_ptr=b, c_ptr=c, M=m, N=n, K=k, stride_am=a.stride(0), stride_ak=a.stride(1), stride_bk=b.stride(0), stride_bn=b.stride(1), stride_cm=c.stride(0), stride_cn=c.stride(1), BLOCK_M=m, BLOCK_N=n, BLOCK_K=k, num_warps=num_warps) golden = torch.matmul(a, b) torch.testing.assert_allclose(c, golden)
4,453
42.242718
110
py
triton
triton-main/python/test/unit/language/test_line_info.py
import subprocess import tempfile import pytest import torch import triton import triton.language as tl @triton.jit def kernel_single(X, Y, BLOCK: tl.constexpr): x = tl.load(X + tl.arange(0, BLOCK)) tl.store(Y + tl.arange(0, BLOCK), x) @triton.jit def device_inline(x): return x + x @triton.jit def kernel_call(X, Y, BLOCK: tl.constexpr): x = tl.load(X + tl.arange(0, BLOCK)) y = device_inline(x) tl.store(Y + tl.arange(0, BLOCK), y) @triton.jit(noinline=True) def device_noinline(X, Y, BLOCK: tl.constexpr): x = tl.load(X + tl.arange(0, BLOCK)) y = x + x tl.store(Y + tl.arange(0, BLOCK), y) @triton.jit def kernel_call_noinline(X, Y, BLOCK: tl.constexpr): device_noinline(X, Y, BLOCK) @triton.jit def kernel_multi_files(X, Y, BLOCK: tl.constexpr): x = tl.load(X + tl.arange(0, BLOCK)) y = tl.softmax(x) tl.store(Y + tl.arange(0, BLOCK), y) def extract_file_lines(asm): fd, path = tempfile.mkstemp() with open(fd, 'wb') as cubin: cubin.write(asm) asm = subprocess.check_output(["nvdisasm", "-g", path]).decode("utf-8") file_lines = [] lines = asm.splitlines() for line in lines: if "## File" in line: entries = line[line.index("## File"):].split(",") file_lines.append((entries[0].strip(), entries[1].strip())) return file_lines def check_file_lines(file_lines, file_name, lineno): for file, line in file_lines: # -1 means do not check line number if lineno == -1: if file_name in file: return True if file_name in file and str(lineno) in line: return True return False func_types = ["single", "call", "call_noinline", "multi_files"] @pytest.mark.parametrize("func", func_types) def test_line_info(func: str): try: subprocess.check_output(["nvdisasm", "-h"]) except BaseException: pytest.skip("nvdisasm is not available") shape = (128, ) x = torch.arange(0, shape[0], dtype=torch.float32, device='cuda') y = torch.zeros(shape, dtype=x.dtype, device="cuda") kernel_info = {} if func == "single": kernel_info = kernel_single[(1,)](x, y, BLOCK=shape[0]) elif func == "call": kernel_info = kernel_call[(1,)](x, y, BLOCK=shape[0]) elif func == "call_noinline": kernel_info = kernel_call_noinline[(1,)](x, y, BLOCK=shape[0]) elif func == "multi_files": kernel_info = kernel_multi_files[(1,)](x, y, BLOCK=shape[0]) file_lines = extract_file_lines(kernel_info.asm["cubin"]) if func == "single": assert (check_file_lines(file_lines, "test_line_info.py", 15)) assert (check_file_lines(file_lines, "test_line_info.py", 16)) elif func == "call": assert (check_file_lines(file_lines, "test_line_info.py", 28)) assert (check_file_lines(file_lines, "test_line_info.py", 21)) assert (check_file_lines(file_lines, "test_line_info.py", 30)) elif func == "call_noinline": assert (check_file_lines(file_lines, "test_line_info.py", 42)) assert (check_file_lines(file_lines, "test_line_info.py", 35)) assert (check_file_lines(file_lines, "test_line_info.py", 36)) assert (check_file_lines(file_lines, "test_line_info.py", 37)) elif func == "multi_files": assert (check_file_lines(file_lines, "test_line_info.py", 47)) assert (check_file_lines(file_lines, "test_line_info.py", 49)) assert (check_file_lines(file_lines, "standard.py", 33)) assert (check_file_lines(file_lines, "standard.py", 34)) assert (check_file_lines(file_lines, "standard.py", 36)) # core.py is changed frequently, so we only check if it exists assert (check_file_lines(file_lines, "core.py", -1))
3,867
30.966942
75
py
triton
triton-main/python/test/unit/language/test_annotations.py
from __future__ import annotations import torch import triton import triton.language as tl def test_annotations(device): @triton.jit def _kernel(X: torch.Tensor, N: int, BLOCK_SIZE: tl.constexpr): pass x = torch.empty(1, device=device) _kernel[(1,)](x, x.shape[0], 32) try: _kernel[(1,)](x.shape[0], x.shape[0], 32) except AttributeError: pass
399
17.181818
67
py
triton
triton-main/python/test/backend/test_device_backend.py
import functools import hashlib import importlib import os import shutil import subprocess import sysconfig import tempfile from pathlib import Path import setuptools import torch import triton import triton.language as tl from triton.common.backend import BaseBackend, register_backend from triton.common.build import quiet from triton.compiler.make_launcher import make_so_cache_key from triton.runtime.cache import get_cache_manager from triton.runtime.driver import DriverBase from triton.runtime.jit import version_key def build_for_backend(name, src, srcdir): suffix = sysconfig.get_config_var('EXT_SUFFIX') so = os.path.join(srcdir, '{name}{suffix}'.format(name=name, suffix=suffix)) # try to avoid setuptools if possible cc = os.environ.get("CC") if cc is None: # TODO: support more things here. clang = shutil.which("clang") gcc = shutil.which("gcc") cc = gcc if gcc is not None else clang if cc is None: raise RuntimeError("Failed to find C compiler. Please specify via CC environment variable.") # This function was renamed and made public in Python 3.10 if hasattr(sysconfig, 'get_default_scheme'): scheme = sysconfig.get_default_scheme() else: scheme = sysconfig._get_default_scheme() # 'posix_local' is a custom scheme on Debian. However, starting Python 3.10, the default install # path changes to include 'local'. This change is required to use triton with system-wide python. if scheme == 'posix_local': scheme = 'posix_prefix' py_include_dir = sysconfig.get_paths(scheme=scheme)["include"] ret = subprocess.check_call([cc, src, f"-I{py_include_dir}", f"-I{srcdir}", "-shared", "-fPIC", "-o", so]) if ret == 0: return so # fallback on setuptools extra_compile_args = [] library_dirs = [] include_dirs = [srcdir] libraries = [] # extra arguments extra_link_args = [] # create extension module ext = setuptools.Extension( name=name, language='c', sources=[src], include_dirs=include_dirs, extra_compile_args=extra_compile_args + ['-O3'], extra_link_args=extra_link_args, library_dirs=library_dirs, libraries=libraries, ) # build extension module args = ['build_ext'] args.append('--build-temp=' + srcdir) args.append('--build-lib=' + srcdir) args.append('-q') args = dict( name=name, ext_modules=[ext], script_args=args, ) with quiet(): setuptools.setup(**args) return so class ExtensionUtils: def __new__(cls): if not hasattr(cls, 'instance'): cls.instance = super(ExtensionUtils, cls).__new__(cls) return cls.instance def __init__(self): dirname = os.path.dirname(os.path.realpath(__file__)) src = Path(os.path.join(dirname, "extension_backend.c")).read_text() key = hashlib.md5(src.encode("utf-8")).hexdigest() cache = get_cache_manager(key) fname = "ext_utils.so" cache_path = cache.get_file(fname) if cache_path is None: with tempfile.TemporaryDirectory() as tmpdir: src_path = os.path.join(tmpdir, "main.c") with open(src_path, "w") as f: f.write(src) so = build_for_backend("ext_utils", src_path, tmpdir) with open(so, "rb") as f: cache_path = cache.put(f.read(), fname, binary=True) import importlib.util spec = importlib.util.spec_from_file_location("ext_utils", cache_path) mod = importlib.util.module_from_spec(spec) spec.loader.exec_module(mod) self.load_binary = mod.load_binary self.get_device_properties = mod.get_device_properties class ExtensionDriver(DriverBase): def __new__(cls): if not hasattr(cls, 'instance'): cls.instance = super(ExtensionDriver, cls).__new__(cls) return cls.instance def __init__(self): self.utils = ExtensionUtils() class ExtensionBackend(BaseBackend): stub_so_path = "" def __init__(self, device_type: str) -> None: super(ExtensionBackend, self).__init__(device_type) self.driver = ExtensionDriver() def add_stages(self, arch, extern_libs, stages): filter_in_stages = ["ast", "ttir", "ttgir"] filter_out_stages = [] for key, _ in stages.items(): if key not in filter_in_stages: filter_out_stages.append(key) for filter_out_key in filter_out_stages: stages.pop(filter_out_key) def add_meta_info(self, ir, cur_module, next_module, metadata, asm): metadata["name"] = "extension_backend_name" def get_driver(self): return self.driver def get_stream(self): return "" @functools.lru_cache(None) def get_device_properties(self, device): return self.driver.utils.get_device_properties() def get_current_device(self): return torch.device("cpu") def set_current_device(self, device): pass def get_load_binary_fn(self): return self.driver.utils.load_binary def get_kernel_bin(self): return "ttgir" def get_architecture_descriptor(self, **kwargs): return "" def make_launcher_stub(self, name, signature, constants): # name of files that are cached so_cache_key = make_so_cache_key(version_key(), signature, constants) so_cache_manager = get_cache_manager(so_cache_key) so_name = f"{name}.so" # retrieve stub from cache if it exists cache_path = so_cache_manager.get_file(so_name) if cache_path is None: with tempfile.TemporaryDirectory() as tmpdir: src = self._generate_launcher(constants, signature) src_path = os.path.join(tmpdir, "main.c") with open(src_path, "w") as f: f.write(src) so = build_for_backend(name, src_path, tmpdir) with open(so, "rb") as f: so_path = so_cache_manager.put(f.read(), so_name, binary=True) type(self).stub_so_path = so_path return so_path else: type(self).stub_so_path = cache_path return cache_path def _generate_launcher(self, constants, signature): # generate glue code src = """ #define __EXTENSION_BACKEND__ #include <Python.h> #include <stdio.h> static PyObject* launch_counter(PyObject* self, PyObject* args) { static int64_t launch_counter = 0; launch_counter += 1; return PyLong_FromLong(launch_counter); } static PyObject* launch(PyObject* self, PyObject* args) { if (PyErr_Occurred()) { return NULL; } launch_counter(self, args); // return None Py_INCREF(Py_None); return Py_None; } static PyMethodDef ModuleMethods[] = { {"launch", launch, METH_VARARGS, "Entry point for all kernels with this signature"}, {"launch_counter", launch_counter, METH_VARARGS, "Entry point to get launch counter"}, {NULL, NULL, 0, NULL} // sentinel }; static struct PyModuleDef ModuleDef = { PyModuleDef_HEAD_INIT, \"__triton_launcher\", NULL, //documentation -1, //size ModuleMethods }; PyMODINIT_FUNC PyInit___triton_launcher(void) { PyObject *m = PyModule_Create(&ModuleDef); if(m == NULL) { return NULL; } PyModule_AddFunctions(m, ModuleMethods); return m; } """ return src def test_dummy_backend(): register_backend("cpu", ExtensionBackend) @triton.jit def kernel(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr): xnumel = 10 xoffset = tl.program_id(0) * XBLOCK xindex = xoffset + tl.arange(0, XBLOCK)[:] xmask = xindex < xnumel x0 = xindex tmp0 = tl.load(in_ptr0 + (x0), xmask) tl.store(out_ptr0 + (x0 + tl.zeros([XBLOCK], tl.int32)), tmp0, xmask) inp = torch.randn(10) out = torch.randn(10) kernel[(10,)](inp, out, 10, XBLOCK=16) spec = importlib.util.spec_from_file_location("__triton_launcher", ExtensionBackend.stub_so_path) mod = importlib.util.module_from_spec(spec) spec.loader.exec_module(mod) launch_counter = getattr(mod, "launch_counter") for _ in range(100): kernel[(10,)](inp, out, 10, XBLOCK=16) assert launch_counter() > 0
8,692
32.053232
110
py
triton
triton-main/python/test/backend/third_party_backends/test_xpu_backend.py
import torch import triton import triton.language as tl def test_xpu_backend(cmdopt): if cmdopt == "xpu": has_ipex = False try: # Import IPEX to provide Intel GPU runtime import intel_extension_for_pytorch # type: ignore # noqa: F401 has_ipex = True if hasattr(torch, "xpu") else False except Exception: has_ipex = False @triton.jit() def kernel(x_ptr, y_ptr, out_ptr): pid = tl.program_id(axis=0) x = tl.load(x_ptr + pid) y = tl.load(y_ptr + pid) out = x + y tl.store(out_ptr + pid, out) if has_ipex: for _ in range(1000): x = torch.randn((65536,), device="xpu", dtype=torch.float32) y = torch.randn((65536,), device="xpu", dtype=torch.float32) z = torch.zeros((65536,), device="xpu", dtype=torch.float32) kernel[(65536,)](x, y, z, num_warps=32) assert torch.all(x + y == z) else: return
1,059
30.176471
76
py
triton
triton-main/python/test/regression/test_functional_regressions.py
import numpy as np import pytest import torch from numpy.random import RandomState import triton import triton.language as tl def test_chained_matmul(): # Regression test for issue #1601 def chained_matmul_reference(a, b, c): intermediate = torch.einsum('MK,NK->MN', a, b) return torch.einsum('MN,NK->MK', intermediate, c) @triton.jit def chained_matmul_kernel( A, # shape: (m, k) B, # shape: (n, k) C, # shape: (n, k) out, # shape: (m, k) m, n, k: tl.constexpr, block_m: tl.constexpr, block_n: tl.constexpr, block_k: tl.constexpr): tl.static_assert(block_k == k, f"expected block_k == k but got {block_k} != {k}") block_ix = tl.program_id(0) a_tile = (block_ix * block_m + tl.arange(0, block_m))[:, None] * block_k \ + tl.arange(0, block_k)[None, :] a = tl.load(A + a_tile, mask=a_tile < m * k, other=0.0) acc = tl.zeros([block_m, block_k], dtype=tl.float32) for loop_block_start in range(0, n, block_n): bc_tile = (loop_block_start + tl.arange(0, block_n))[:, None] * block_k \ + tl.arange(0, block_k)[None, :] b = tl.load(B + bc_tile, mask=bc_tile < n * k, other=0.0) intermediate = tl.dot(a, tl.trans(b)) intermediate_mask = ((loop_block_start + tl.arange(0, block_n)) < n)[None, :] \ * (tl.arange(0, block_m) < m)[:, None] intermediate = tl.where(intermediate_mask, intermediate, 0.0) c = tl.load(C + bc_tile, mask=bc_tile < n * k) acc += tl.dot(intermediate.to(A.dtype.element_ty), c) tl.store(out + a_tile, acc.to(A.dtype.element_ty), mask=a_tile < m * k) m, n, k = 32, 64, 128 block_m, block_n, block_k = 16, 32, k grid = (triton.cdiv(m, block_m),) a = torch.randint(low=0, high=2, size=(m, k), dtype=torch.float16, device='cuda') b = torch.randint(low=0, high=2, size=(n, k), dtype=torch.float16, device='cuda') c = torch.randint_like(b, low=0, high=2) triton_result = torch.zeros_like(a) torch_result = chained_matmul_reference(a, b, c) chained_matmul_kernel[grid](a, b, c, triton_result, m, n, k, block_m=block_m, block_n=block_n, block_k=block_k) assert (torch_result == triton_result).all() def test_vecmat(): @triton.jit def batched_vecmat( # inputs A, # shape: [dim_m, dim_k] B, # shape: [dim_m, dim_n, dim_k] # dimensions dim_m, dim_n, dim_k, # outputs output, # block information block_m: tl.constexpr, block_n: tl.constexpr, block_k: tl.constexpr ): m_index = tl.program_id(0) n_index = tl.program_id(1) # Output tile output_tile = (m_index * block_m + tl.arange(0, block_m))[:, None] * dim_n \ + (n_index * block_n + tl.arange(0, block_n))[None, :] vecmat = tl.zeros([block_m, block_n], dtype=A.dtype.element_ty) k_blocks = dim_k // block_k for k_index in range(k_blocks): # Load A tile a_tile = (m_index * block_m + tl.arange(0, block_m))[:, None] * dim_k \ + (k_index * block_k + tl.arange(0, block_k))[None, :] a = tl.load(A + a_tile) # Load B tile, transposed to [n, m, k] in order to broadcast A on a # leading dimension. b_tile = (m_index * block_m + tl.arange(0, block_m))[None, :, None] * dim_n * dim_k \ + (n_index * block_n + tl.arange(0, block_n))[:, None, None] * dim_k \ + (k_index * block_k + tl.arange(0, block_k))[None, None, :] b = tl.load(B + b_tile) expanded_a, _ = tl.broadcast(a, b) vecmat += tl.trans(tl.sum(expanded_a * b, axis=2)) tl.store(output + output_tile, vecmat) M, N, K = 128, 128, 128 block_m, block_n, block_k = 16, 32, 64 rs = RandomState(17) A_vec = rs.randint(0, 4, (M, K)).astype('float32') B_vec = rs.randint(0, 4, (M, N, K)).astype('float32') A = A_vec B = B_vec A_tri = torch.tensor(A, device='cuda') B_tri = torch.tensor(B, device='cuda') C_tri = torch.zeros((M, N), dtype=torch.float32, device='cuda') grid = (M // block_m, N // block_n) batched_vecmat[grid](A_tri, B_tri, M, N, K, C_tri, block_m=block_m, block_n=block_n, block_k=block_k, num_warps=4, num_stages=1) A_expanded = A[:, np.newaxis, :] A_broadcasted = np.broadcast_to(A_expanded, (M, N, K)) AB = A_broadcasted * B C_ref = np.sum(AB, axis=2) np.testing.assert_allclose(C_ref, C_tri.cpu().numpy(), rtol=0.01, atol=1e-3) @pytest.mark.parametrize("type", ["pre_load", "post_load", "post_pre_mixed", "post_load_two_iters", "post_load_three_iters"]) def test_iv_dependent_matmul(type): @triton.jit def kernel( a_ptr, b_ptr, c_ptr, M, N, K, stride_am, stride_ak, stride_bk, stride_bn, stride_cm, stride_cn, BLOCK_SIZE_M: tl.constexpr, BLOCK_SIZE_N: tl.constexpr, BLOCK_SIZE_K: tl.constexpr, type: tl.constexpr ): pid = tl.program_id(axis=0) num_pid_n = tl.cdiv(N, BLOCK_SIZE_N) pid_m = pid // num_pid_n pid_n = pid % num_pid_n offs_am = (pid_m * BLOCK_SIZE_M + tl.arange(0, BLOCK_SIZE_M)) % M offs_bn = (pid_n * BLOCK_SIZE_N + tl.arange(0, BLOCK_SIZE_N)) % N offs_k = tl.arange(0, BLOCK_SIZE_K) a_ptr = a_ptr + (offs_am[:, None] * stride_am + offs_k[None, :] * stride_ak) b_ptr = b_ptr + (offs_k[:, None] * stride_bk + offs_bn[None, :] * stride_bn) a_ptrs = a_ptr b_ptrs = b_ptr if type == "post_load_two_iters": a_ptrs_next = a_ptr + BLOCK_SIZE_K * stride_ak b_ptrs_next = b_ptr + BLOCK_SIZE_K * stride_bk elif type == "post_load_three_iters": a_ptrs_next = a_ptr + BLOCK_SIZE_K * stride_ak b_ptrs_next = b_ptr + BLOCK_SIZE_K * stride_bk a_ptrs_next_next = a_ptr + 2 * BLOCK_SIZE_K * stride_ak b_ptrs_next_next = b_ptr + 2 * BLOCK_SIZE_K * stride_bk accumulator = tl.zeros((BLOCK_SIZE_M, BLOCK_SIZE_N), dtype=tl.float32) for k in range(0, tl.cdiv(K, BLOCK_SIZE_K)): if type == "pre_load": a_ptrs = a_ptr + k * BLOCK_SIZE_K * stride_ak b_ptrs = b_ptr + k * BLOCK_SIZE_K * stride_bk elif type == "post_pre_mixed": a_ptrs = a_ptr + k * BLOCK_SIZE_K * stride_ak a = tl.load(a_ptrs, mask=offs_k[None, :] < K - k * BLOCK_SIZE_K, other=0.0) b = tl.load(b_ptrs, mask=offs_k[:, None] < K - k * BLOCK_SIZE_K, other=0.0) accumulator += tl.dot(a, b) if type == "post_load": a_ptrs = a_ptr + (k + 1) * BLOCK_SIZE_K * stride_ak b_ptrs = b_ptr + (k + 1) * BLOCK_SIZE_K * stride_bk elif type == "post_pre_mixed": b_ptrs = b_ptr + (k + 1) * BLOCK_SIZE_K * stride_bk elif type == "post_load_two_iters": a_ptrs = a_ptrs_next b_ptrs = b_ptrs_next a_ptrs_next = a_ptr + (k + 2) * BLOCK_SIZE_K * stride_ak b_ptrs_next = b_ptr + (k + 2) * BLOCK_SIZE_K * stride_bk elif type == "post_load_three_iters": a_ptrs = a_ptrs_next b_ptrs = b_ptrs_next a_ptrs_next = a_ptrs_next_next b_ptrs_next = b_ptrs_next_next a_ptrs_next_next = a_ptr + (k + 3) * BLOCK_SIZE_K * stride_ak b_ptrs_next_next = b_ptr + (k + 3) * BLOCK_SIZE_K * stride_bk c = accumulator.to(tl.float16) offs_cm = pid_m * BLOCK_SIZE_M + tl.arange(0, BLOCK_SIZE_M) offs_cn = pid_n * BLOCK_SIZE_N + tl.arange(0, BLOCK_SIZE_N) c_ptrs = c_ptr + stride_cm * offs_cm[:, None] + stride_cn * offs_cn[None, :] c_mask = (offs_cm[:, None] < M) & (offs_cn[None, :] < N) tl.store(c_ptrs, c, mask=c_mask) M = 256 K = 256 N = 256 BLOCK_SIZE_K = 32 BLOCK_SIZE_N = 32 BLOCK_SIZE_M = 32 a = torch.rand((M, K), device='cuda') b = torch.rand((K, N), device='cuda') torch_output = torch.mm(a, b) triton_output = torch.empty_like( torch_output, device=torch_output.device) def grid(META): return (triton.cdiv(M, META['BLOCK_SIZE_M']) * triton.cdiv(N, META['BLOCK_SIZE_N']),) num_stages = 4 if type == "post_load_three_iters" else 3 kernel[grid](a, b, triton_output, M, N, K, a.stride(0), a.stride(1), b.stride(0), b.stride(1), triton_output.stride(0), triton_output.stride(1), BLOCK_SIZE_M=BLOCK_SIZE_M, BLOCK_SIZE_N=BLOCK_SIZE_N, BLOCK_SIZE_K=BLOCK_SIZE_K, type=type, num_stages=num_stages) torch.testing.assert_allclose(torch_output, triton_output, rtol=1e-2, atol=1e-2)
9,166
38.683983
125
py
triton
triton-main/python/test/regression/test_performance.py
import subprocess import sys import pytest import torch import triton import triton.language as tl import triton.ops from triton.testing import get_dram_gbps, get_max_tensorcore_tflops DEVICE_NAME = {7: 'v100', 8: 'a100'}[torch.cuda.get_device_capability()[0]] ####################### # Utilities ####################### def print_perf(cur_ms, cur_util, ref_util): # print on the same line cur_ms, cur_util and ref_util with 3 decimal places print(f'{cur_ms:.3f} ms \t cur: {cur_util:.3f} \t ref: {ref_util:.3f} \t dif={cur_util - ref_util:.3f}', end='\t') def nvsmi(attrs): attrs = ','.join(attrs) cmd = ['nvidia-smi', '-i', '0', '--query-gpu=' + attrs, '--format=csv,noheader,nounits'] out = subprocess.check_output(cmd) ret = out.decode(sys.stdout.encoding).split(',') ret = [int(x) for x in ret] return ret ####################### # Matrix Multiplication ####################### sm_clocks = {'v100': 1350, 'a100': 1350} mem_clocks = {'v100': 877, 'a100': 1215} matmul_data = { # NOTE: 'a100': { # square (512, 512, 512): {'float16': 0.061, 'float32': 0.097, 'int8': 0.05}, (1024, 1024, 1024): {'float16': 0.283, 'float32': 0.313, 'int8': 0.169}, (2048, 2048, 2048): {'float16': 0.618, 'float32': 0.532, 'int8': 0.34}, (8192, 8192, 8192): {'float16': 0.786, 'float32': 0.754, 'int8': 0.51}, # tall-skinny (16, 1024, 1024): {'float16': 0.006, 'float32': 0.009, 'int8': 0.005}, (16, 4096, 4096): {'float16': 0.057, 'float32': 0.051, 'int8': 0.026}, (16, 8192, 8192): {'float16': 0.077, 'float32': 0.077, 'int8': 0.043}, (64, 1024, 1024): {'float16': 0.018, 'float32': 0.023, 'int8': 0.017}, (64, 4096, 4096): {'float16': 0.150, 'float32': 0.000, 'int8': 0.097}, (64, 8192, 8192): {'float16': 0.338, 'float32': 0.000, 'int8': 0.174}, (1024, 64, 1024): {'float16': 0.029, 'float32': 0.046, 'int8': 0.017}, (4096, 64, 4096): {'float16': 0.179, 'float32': 0.214, 'int8': 0.102}, (8192, 64, 8192): {'float16': 0.278, 'float32': 0.000, 'int8': 0.177}, # test EVEN_K==False (8192, 8192, 8176): {'float16': 0.786, 'float32': 0.696, 'int8': 0.51}, } } @pytest.mark.parametrize('M, N, K, dtype_str', [(M, N, K, dtype_str) for M, N, K in matmul_data[DEVICE_NAME].keys() for dtype_str in ['float16', 'float32']]) def test_matmul(M, N, K, dtype_str): stream = torch.cuda.Stream() torch.cuda.set_stream(stream) if dtype_str in ['float32', 'int8'] and DEVICE_NAME != 'a100': pytest.skip('Only test float32 & int8 on a100') if (M, N, K) in [(64, 4096, 4096), (64, 8192, 8192), (8192, 64, 8192)] and dtype_str == 'float32': pytest.skip('Out of shared memory in float32') dtype = {'float16': torch.float16, 'float32': torch.float32, 'int8': torch.int8}[dtype_str] torch.manual_seed(0) ref_gpu_util = matmul_data[DEVICE_NAME][(M, N, K)][dtype_str] cur_sm_clock = nvsmi(['clocks.current.sm'])[0] max_gpu_perf = get_max_tensorcore_tflops(dtype, clock_rate=cur_sm_clock * 1e3) if dtype == torch.int8: a = torch.randint(-128, 127, (M, K), dtype=dtype, device='cuda') b = torch.randint(-128, 127, (N, K), dtype=dtype, device='cuda') b = b.t() # only test row-col layout else: a = torch.randn((M, K), dtype=dtype, device='cuda') b = torch.randn((K, N), dtype=dtype, device='cuda') fn = lambda: triton.ops.matmul(a, b) ms = triton.testing.do_bench_cudagraph(fn) cur_gpu_perf = 2. * M * N * K / ms * 1e-9 cur_gpu_util = cur_gpu_perf / max_gpu_perf print_perf(ms, cur_gpu_util, ref_gpu_util) triton.testing.assert_close(cur_gpu_util, ref_gpu_util, atol=0.02, rtol=0.01) ####################### # Element-Wise ####################### @triton.jit def _add(x_ptr, y_ptr, output_ptr, n_elements, BLOCK_SIZE: tl.constexpr): pid = tl.program_id(axis=0) block_start = pid * BLOCK_SIZE offsets = block_start + tl.arange(0, BLOCK_SIZE) mask = offsets < n_elements x = tl.load(x_ptr + offsets, mask=mask) y = tl.load(y_ptr + offsets, mask=mask) output = x + y tl.store(output_ptr + offsets, output, mask=mask) elementwise_data = { 'a100': { 1024 * 16: {'float16': 0.003, 'float32': 0.007}, 1024 * 64: {'float16': 0.013, 'float32': 0.026}, 1024 * 256: {'float16': 0.053, 'float32': 0.105}, 1024 * 1024: {'float16': 0.212, 'float32': 0.420}, 1024 * 16384: {'float16': 0.762, 'float32': 0.812}, 1024 * 65536: {'float16': 0.846, 'float32': 0.869}, # Non pow 2 1020 * 100: {'float16': 0.020, 'float32': 0.041}, 10003 * 7007: {'float16': 0.513, 'float32': 0.861}, } } @pytest.mark.parametrize('N', elementwise_data[DEVICE_NAME].keys()) @pytest.mark.parametrize("dtype_str", ['float16', 'bfloat16', 'float32']) def test_elementwise(N, dtype_str): stream = torch.cuda.Stream() torch.cuda.set_stream(stream) torch.manual_seed(0) if dtype_str in ['bfloat16'] and DEVICE_NAME != 'a100': pytest.skip('Only test bfloat16 on a100') dtype = {'float16': torch.float16, 'bfloat16': torch.bfloat16, 'float32': torch.float32}[dtype_str] ref_dtype_str = 'float16' if dtype_str == 'bfloat16' else dtype_str ref_gpu_util = elementwise_data[DEVICE_NAME][N][ref_dtype_str] max_gpu_perf = get_dram_gbps() z = torch.empty((N, ), dtype=dtype, device='cuda') x = torch.randn_like(z) y = torch.randn_like(z) grid = lambda args: (triton.cdiv(N, args['BLOCK_SIZE']), ) fn = lambda: _add[grid](x, y, z, N, BLOCK_SIZE=1024) ms = triton.testing.do_bench_cudagraph(fn) cur_gpu_perf = 3. * N * z.element_size() / ms * 1e-6 cur_gpu_util = cur_gpu_perf / max_gpu_perf print_perf(ms, cur_gpu_util, ref_gpu_util) triton.testing.assert_close(cur_gpu_util, ref_gpu_util, atol=0.02, rtol=0.01) ####################### # Flash-Attention ####################### flash_attention_data = { "a100": { (4, 48, 4096, 64, True, True, 'forward', 'float16'): 0.433, (4, 48, 4096, 64, True, True, 'forward', 'bfloat16'): 0.392, (4, 48, 1024, 16, True, True, 'forward', 'float32'): 0.106, (4, 48, 4096, 64, True, True, 'backward', 'float16'): 0.204, (4, 48, 4096, 64, True, True, 'backward', 'bfloat16'): 0.202, (4, 48, 1024, 16, True, True, 'backward', 'float32'): 0.089, (4, 48, 4096, 64, True, False, 'forward', 'float16'): 0.242, (4, 48, 4096, 64, True, False, 'forward', 'bfloat16'): 0.220, (4, 48, 1024, 16, True, False, 'forward', 'float32'): 0.069, (4, 48, 4096, 64, True, False, 'backward', 'float16'): 0.136, (4, 48, 4096, 64, True, False, 'backward', 'bfloat16'): 0.135, (4, 48, 1024, 16, True, False, 'backward', 'float32'): 0.052, (4, 48, 4096, 64, False, True, 'forward', 'float16'): 0.432, (4, 48, 4096, 64, False, True, 'forward', 'bfloat16'): 0.392, (4, 48, 1024, 16, False, True, 'forward', 'float32'): 0.107, (4, 48, 4096, 64, False, True, 'backward', 'float16'): 0.265, (4, 48, 4096, 64, False, True, 'backward', 'bfloat16'): 0.257, (4, 48, 1024, 16, False, True, 'backward', 'float32'): 0.128, (4, 48, 4096, 64, False, False, 'forward', 'float16'): 0.251, (4, 48, 4096, 64, False, False, 'forward', 'bfloat16'): 0.220, (4, 48, 1024, 16, False, False, 'forward', 'float32'): 0.069, (4, 48, 4096, 64, False, False, 'backward', 'float16'): 0.159, (4, 48, 4096, 64, False, False, 'backward', 'bfloat16'): 0.138, (4, 48, 1024, 16, False, False, 'backward', 'float32'): 0.076, } } @pytest.mark.parametrize("dtype_str", ['float16', 'bfloat16', 'float32']) @pytest.mark.parametrize("mode", ['forward', 'backward']) @pytest.mark.parametrize("causal", [True, False]) @pytest.mark.parametrize("seq_par", [True, False]) @pytest.mark.parametrize("Z, H, N_CTX, D_HEAD", [[4, 48, 4096, 64]]) def test_flash_attention(Z, H, N_CTX, D_HEAD, seq_par, causal, mode, dtype_str): stream = torch.cuda.Stream() torch.cuda.set_stream(stream) is_backward = mode == 'backward' capability = torch.cuda.get_device_capability() if capability[0] < 8: pytest.skip("Flash attention only supported for compute capability < 80") torch.manual_seed(20) dtype = {'float16': torch.float16, 'bfloat16': torch.bfloat16, 'float32': torch.float32}[dtype_str] # init data if dtype_str == 'float32': N_CTX = 1024 D_HEAD = 16 q = torch.empty((Z, H, N_CTX, D_HEAD), dtype=dtype, device="cuda").normal_(mean=0.1, std=0.2).requires_grad_() k = torch.empty((Z, H, N_CTX, D_HEAD), dtype=dtype, device="cuda").normal_(mean=0.4, std=0.2).requires_grad_() v = torch.empty((Z, H, N_CTX, D_HEAD), dtype=dtype, device="cuda").normal_(mean=0.3, std=0.2).requires_grad_() sm_scale = 0.2 # benchmark fn = lambda: triton.ops.attention(q, k, v, causal, sm_scale, seq_par) if is_backward: o = fn() do = torch.randn_like(o) fn = lambda: o.backward(do, retain_graph=True) ms = triton.testing.do_bench_cudagraph(fn) # compute flops flops_per_matmul = 2. * Z * H * N_CTX * N_CTX * D_HEAD * 0.5 total_flops = 2 * flops_per_matmul if is_backward: total_flops *= 2.5 # 2.0(bwd) + 0.5(recompute) cur_gpu_perf = total_flops / ms * 1e-9 # maximum flops cur_sm_clock = nvsmi(['clocks.current.sm'])[0] max_gpu_perf = get_max_tensorcore_tflops(dtype, clock_rate=cur_sm_clock * 1e3) cur_gpu_util = cur_gpu_perf / max_gpu_perf ref_gpu_util = flash_attention_data[DEVICE_NAME][(Z, H, N_CTX, D_HEAD, seq_par, causal, mode, dtype_str)] print_perf(ms, cur_gpu_util, ref_gpu_util) triton.testing.assert_close(cur_gpu_util, ref_gpu_util, atol=0.02, rtol=0.01)
9,987
42.807018
118
py
triton
triton-main/python/triton/testing.py
import functools import os import subprocess import sys from contextlib import contextmanager from ._C.libtriton.triton import runtime def nvsmi(attrs): attrs = ','.join(attrs) cmd = ['nvidia-smi', '-i', '0', '--query-gpu=' + attrs, '--format=csv,noheader,nounits'] out = subprocess.check_output(cmd) ret = out.decode(sys.stdout.encoding).split(',') ret = [int(x) for x in ret] return ret def do_bench_cudagraph(fn, rep=20, grad_to_none=None): import torch """ Benchmark the runtime of the provided function. :param fn: Function to benchmark :type fn: Callable :param rep: Repetition time (in ms) :type rep: int :param grad_to_none: Reset the gradient of the provided tensor to None :type grad_to_none: torch.tensor, optional """ if torch.cuda.current_stream() == torch.cuda.default_stream(): raise RuntimeError("Cannot capture graph in default stream. Please use side stream in benchmark code.") # record CUDAGraph fn() if grad_to_none is not None: for x in grad_to_none: x.detach_() x.requires_grad_(True) x.grad = None g = torch.cuda.CUDAGraph() with torch.cuda.graph(g): fn() torch.cuda.synchronize() fn = lambda: g.replay() # Estimate the runtime of the function start_event = torch.cuda.Event(enable_timing=True) end_event = torch.cuda.Event(enable_timing=True) start_event.record() fn() end_event.record() torch.cuda.synchronize() estimate_ms = start_event.elapsed_time(end_event) # compute number of repetition to last `rep` ms n_repeat = max(1, int(rep / estimate_ms)) # compute number of repetition to last `rep` ms start_event = [torch.cuda.Event(enable_timing=True) for i in range(n_repeat)] end_event = [torch.cuda.Event(enable_timing=True) for i in range(n_repeat)] ret = [] n_retries = 50 for _ in range(n_retries): # Benchmark torch.cuda.synchronize() for i in range(n_repeat): # we don't want `fn` to accumulate gradient values # if it contains a backward pass. So we clear the # provided gradients if grad_to_none is not None: for x in grad_to_none: x.grad = None # record time of `fn` start_event[i].record() fn() end_event[i].record() torch.cuda.synchronize() times = torch.tensor([s.elapsed_time(e) for s, e in zip(start_event, end_event)]) ret.append(torch.min(times)) return torch.mean(torch.tensor(ret)).item() def do_bench(fn, warmup=25, rep=100, grad_to_none=None, quantiles=None, fast_flush=True, return_mode="mean"): assert return_mode in ["min", "max", "mean", "median"] import torch """ Benchmark the runtime of the provided function. By default, return the median runtime of :code:`fn` along with the 20-th and 80-th performance percentile. :param fn: Function to benchmark :type fn: Callable :param warmup: Warmup time (in ms) :type warmup: int :param rep: Repetition time (in ms) :type rep: int :param grad_to_none: Reset the gradient of the provided tensor to None :type grad_to_none: torch.tensor, optional :param quantiles: Performance percentile to return in addition to the median. :type quantiles: list[float] :param fast_flush: Use faster kernel to flush L2 between measurements :type fast_flush: bool """ fn() torch.cuda.synchronize() # We maintain a buffer of 256 MB that we clear # before each kernel call to make sure that the L2 # doesn't contain any input data before the run if fast_flush: cache = torch.empty(int(256e6 // 4), dtype=torch.int, device='cuda') else: cache = torch.empty(int(256e6), dtype=torch.int8, device='cuda') # Estimate the runtime of the function start_event = torch.cuda.Event(enable_timing=True) end_event = torch.cuda.Event(enable_timing=True) start_event.record() for _ in range(5): cache.zero_() fn() end_event.record() torch.cuda.synchronize() estimate_ms = start_event.elapsed_time(end_event) / 5 # compute number of warmup and repeat n_warmup = max(1, int(warmup / estimate_ms)) n_repeat = max(1, int(rep / estimate_ms)) start_event = [torch.cuda.Event(enable_timing=True) for i in range(n_repeat)] end_event = [torch.cuda.Event(enable_timing=True) for i in range(n_repeat)] # Warm-up for _ in range(n_warmup): fn() # Benchmark for i in range(n_repeat): # we don't want `fn` to accumulate gradient values # if it contains a backward pass. So we clear the # provided gradients if grad_to_none is not None: for x in grad_to_none: x.grad = None # we clear the L2 cache before each run cache.zero_() # record time of `fn` start_event[i].record() fn() end_event[i].record() # Record clocks torch.cuda.synchronize() times = torch.tensor([s.elapsed_time(e) for s, e in zip(start_event, end_event)], dtype=torch.float) if quantiles is not None: ret = torch.quantile(times, torch.tensor(quantiles, dtype=torch.float)).tolist() if len(ret) == 1: ret = ret[0] return ret return getattr(torch, return_mode)(times).item() def assert_close(x, y, atol=None, rtol=None, err_msg=''): import numpy as np import torch # canonicalize arguments to be tensors if not isinstance(x, torch.Tensor): x = torch.tensor(x) if not isinstance(y, torch.Tensor): y = torch.tensor(y) # absolute tolerance if atol is None: atol = 1e-2 atol = atol(x.dtype) if callable(atol) else atol # relative tolerance hook if rtol is None: rtol = 0. rtol = rtol(x.dtype) if callable(rtol) else rtol # we use numpy instead of pytorch # as it seems more memory efficient # pytorch tends to oom on large tensors if isinstance(x, torch.Tensor): if x.dtype == torch.bfloat16: x = x.float() x = x.cpu().detach().numpy() if isinstance(y, torch.Tensor): if y.dtype == torch.bfloat16: y = y.float() y = y.cpu().detach().numpy() # we handle size==1 case separately as we can # provide better error message there if x.size > 1 or y.size > 1: np.testing.assert_allclose(x, y, atol=atol, rtol=rtol, equal_nan=True) return if not np.allclose(x, y, atol=atol, rtol=rtol): raise AssertionError(f'{err_msg} {x} is not close to {y} (atol={atol}, rtol={rtol})') class Benchmark: """ This class is used by the :code:`perf_report` function to generate line plots with a concise API. """ def __init__( self, x_names, x_vals, line_arg, line_vals, line_names, plot_name, args, xlabel='', ylabel='', x_log=False, y_log=False, color=None, styles=None, ): """ Constructor :param x_names: Name of the arguments that should appear on the x axis of the plot. If the list contains more than one element, all the arguments are assumed to have the same value. :type x_names: List[str] :param x_vals: List of values to use for the arguments in :code:`x_names`. :type x_vals: List[Any] :param line_arg: Argument name for which different values correspond to different lines in the plot. :type line_arg: str :param line_vals: List of values to use for the arguments in :code:`line_arg`. :type line_vals: List[str] :param line_names: Label names for the different lines. :type line_names: List[str] :param plot_name: Name of the plot. :type plot_name: str :param args: List of arguments to remain fixed throughout the benchmark. :type args: List[str] :param xlabel: Label for the x axis of the plot. :type xlabel: str, optional :param ylabel: Label for the y axis of the plot. :type ylabel: str, optional :param x_log: Whether the x axis should be log scale. :type x_log: bool, optional :param y_log: Whether the y axis should be log scale. :type y_log: bool, optional """ self.x_names = x_names self.x_vals = x_vals self.x_log = x_log self.line_arg = line_arg self.line_vals = line_vals self.line_names = line_names self.y_log = y_log self.styles = styles # plot info self.xlabel = xlabel self.ylabel = ylabel self.plot_name = plot_name self.args = args class Mark: def __init__(self, fn, benchmarks): self.fn = fn self.benchmarks = benchmarks def _run(self, bench, save_path, show_plots, print_data): import os import matplotlib.pyplot as plt import pandas as pd y_mean = bench.line_names y_min = [f'{x}-min' for x in bench.line_names] y_max = [f'{x}-max' for x in bench.line_names] df = pd.DataFrame(columns=[bench.x_names[0]] + y_mean + y_min + y_max) for x in bench.x_vals: x_args = {x_name: x for x_name in bench.x_names} row_mean, row_min, row_max = [], [], [] for y in bench.line_vals: ret = self.fn(**x_args, **{bench.line_arg: y}, **bench.args) try: y_mean, y_min, y_max = ret except TypeError: y_mean, y_min, y_max = ret, None, None row_mean += [y_mean] row_min += [y_min] row_max += [y_max] df.loc[len(df)] = [x] + row_mean + row_min + row_max if bench.plot_name: plt.figure() ax = plt.subplot() x = bench.x_names[0] for i, y in enumerate(bench.line_names): y_min, y_max = df[y + '-min'], df[y + '-max'] col = bench.styles[i][0] if bench.styles else None sty = bench.styles[i][1] if bench.styles else None ax.plot(df[x], df[y], label=y, color=col, ls=sty) if y_min is not None and y_max is not None: ax.fill_between(df[x], y_min, y_max, alpha=0.15, color=col) ax.legend() xlabel = bench.xlabel if bench.xlabel else " = ".join(bench.x_names) ax.set_xlabel(xlabel) ax.set_ylabel(bench.ylabel) # ax.set_title(bench.plot_name) ax.set_xscale("log" if bench.x_log else "linear") ax.set_yscale("log" if bench.y_log else "linear") if show_plots: plt.show() if save_path: plt.savefig(os.path.join(save_path, f"{bench.plot_name}.png")) df = df[[bench.x_names[0]] + bench.line_names] if print_data: print(bench.plot_name + ':') print(df) if save_path: df.to_csv(os.path.join(save_path, f"{bench.plot_name}.csv"), float_format='%.1f', index=False) def run(self, show_plots=False, print_data=False, save_path=''): has_single_bench = isinstance(self.benchmarks, Benchmark) benchmarks = [self.benchmarks] if has_single_bench else self.benchmarks if save_path: html = open(os.path.join(save_path, "results.html"), "w") html.write("<html><body>\n") for bench in benchmarks: self._run(bench, save_path, show_plots, print_data) if save_path: html.write(f"<image src=\"{bench.plot_name}.png\"/>\n") if save_path: html.write("</body></html>\n") def perf_report(benchmarks): """ Mark a function for benchmarking. The benchmark can then be executed by using the :code:`.run` method on the return value. :param benchmarks: Benchmarking configurations. :type benchmarks: List of :class:`Benchmark` """ wrapper = lambda fn: Mark(fn, benchmarks) return wrapper def get_dram_gbps(backend=None, device=None): ''' return DRAM bandwidth in GB/s ''' import torch from .runtime import driver if not backend: backend = runtime.backend.CUDA if not device: device = torch.cuda.current_device() mem_clock_khz = driver.utils.get_device_properties(device)["mem_clock_rate"] # in kHz bus_width = driver.utils.get_device_properties(device)["mem_bus_width"] bw_gbps = mem_clock_khz * bus_width * 2 / 1e6 / 8 # In GB/s return bw_gbps def get_max_tensorcore_tflops(dtype, backend=None, device=None, clock_rate=None): import torch from .runtime import driver if not backend: backend = runtime.backend.CUDA if not device: device = torch.cuda.current_device() num_subcores = driver.utils.get_device_properties(device)["multiprocessor_count"] * 4 if not clock_rate: clock_rate = driver.utils.get_device_properties(device)["sm_clock_rate"] # in kHz capability = torch.cuda.get_device_capability(device) if capability[0] < 8: assert dtype == torch.float16 ops_per_sub_core = 256 # 2 4x4x4 Tensor Cores else: if dtype == torch.float32: ops_per_sub_core = 256 elif dtype in [torch.float16, torch.bfloat16]: ops_per_sub_core = 512 elif dtype == torch.int8: ops_per_sub_core = 1024 else: raise RuntimeError("dtype not supported") tflops = num_subcores * clock_rate * ops_per_sub_core * 1e-9 return tflops # create decorator that wraps test function into # a cuda-memcheck system call def cuda_memcheck(**target_kwargs): def decorator(test_fn): @functools.wraps(test_fn) def wrapper(*args, **kwargs): import psutil ppid_name = psutil.Process(os.getppid()).name() run_cuda_memcheck = target_kwargs.items() <= kwargs.items() if run_cuda_memcheck and ppid_name != "cuda-memcheck": path = os.path.realpath(test_fn.__globals__["__file__"]) # get path of current file env = {"PATH": os.environ["PATH"], "PYTORCH_NO_CUDA_MEMORY_CACHING": "1"} assert 'request' in kwargs, "memcheck'ed test must have a (possibly unused) `request` fixture" test_id = kwargs['request'].node.callspec.id cmd = f"{path}::{test_fn.__name__}[{test_id}]" out = subprocess.run(["cuda-memcheck", "pytest", "-vs", cmd], capture_output=True, env=env) assert out.returncode == 0, "cuda-memcheck returned an error: bounds checking failed" assert "ERROR SUMMARY: 0 errors" in str(out.stdout) else: test_fn(*args, **kwargs) return wrapper return decorator def nvsmi_attr(attrs): attrs = ",".join(attrs) cmd = [ "nvidia-smi", "-i", "0", "--query-gpu=" + attrs, "--format=csv,noheader,nounits", ] out = subprocess.check_output(cmd) ret = out.decode(sys.stdout.encoding).split(",") ret = [int(x) for x in ret] return ret @contextmanager def set_gpu_clock(ref_sm_clock=1350, ref_mem_clock=1215): try: subprocess.check_output(["nvidia-smi", "-i", "0", "-pm", "1"]) subprocess.check_output( [ "nvidia-smi", "-i", "0", f"--lock-gpu-clocks={ref_sm_clock},{ref_sm_clock}", ] ) subprocess.check_output( [ "nvidia-smi", "-i", "0", f"--lock-memory-clocks={ref_mem_clock},{ref_mem_clock}", ] ) cur_sm_clock = nvsmi_attr(["clocks.current.sm"])[0] cur_mem_clock = nvsmi_attr(["clocks.current.memory"])[0] assert abs(cur_sm_clock - ref_sm_clock) < 10, f"GPU SMs must run at {ref_sm_clock} MHz" assert abs(cur_mem_clock - ref_mem_clock) < 10, f"GPU SMs must run at {ref_mem_clock} MHz" tflops = 1e-6 * 2 * 108 * 4 * 256 * ref_sm_clock gbps = 640 * 2 * ref_mem_clock * 1e-3 yield tflops, gbps finally: subprocess.check_output(["nvidia-smi", "-i", "0", "-pm", "0"]) subprocess.check_output(["nvidia-smi", "-i", "0", "-rgc"]) subprocess.check_output(["nvidia-smi", "-i", "0", "-rmc"]) def get_max_simd_tflops(dtype, backend=None, device=None): import torch from .runtime import driver if not backend: backend = runtime.backend.CUDA if not device: device = torch.cuda.current_device() num_subcores = driver.utils.get_device_properties(device)["multiprocessor_count"] * 4 clock_rate = driver.utils.get_device_properties(device)["sm_clock_rate"] # in kHz capability = torch.cuda.get_device_capability() if capability[0] < 8: if dtype == torch.float32: ops_per_sub_core = 32 # 2*16 elif dtype == torch.float16: ops_per_sub_core = 64 else: raise RuntimeError("dtype not supported") else: if dtype == torch.float32: ops_per_sub_core = 32 elif dtype in [torch.float16, torch.bfloat16]: ops_per_sub_core = 64 else: raise RuntimeError("dtype not supported") tflops = num_subcores * clock_rate * ops_per_sub_core * 1e-9 return tflops
17,704
35.505155
189
py
triton
triton-main/python/triton/tools/build_extern.py
import argparse import subprocess from abc import ABC, abstractmethod from typing import Dict, List, Optional class Symbol: _name: str _op_name: str _ret_type: str _arg_names: List[str] _arg_types: List[str] def __init__( self, name: str, op_name: str, ret_type: str, arg_names: List[str], arg_types: List[str], ) -> None: ''' A symbol is a function declaration. :param name: name of the symbol :param op_name: name of the operation :param ret_type: return type of the operation :param arg_names: names of the arguments :param arg_types: types of the arguments ''' self._name = name self._op_name = op_name self._ret_type = ret_type self._arg_names = list(arg_names) self._arg_types = list(arg_types) @property def name(self) -> str: return self._name @property def op_name(self) -> str: return self._op_name @property def ret_type(self) -> str: return self._ret_type @property def arg_names(self) -> List[str]: return self._arg_names @property def arg_types(self) -> List[str]: return self._arg_types def convert_type(type_str) -> Optional[str]: if type_str == "i32": return "int32" elif type_str == "u32": return "uint32" elif type_str == "i64": return "int64" elif type_str == "u64": return "uint64" elif type_str == "float": return "fp32" elif type_str == "double": return "fp64" else: # ignore other types, such as pointer types return None def to_unsigned(type_str) -> str: if type_str == "int32": return "uint32" elif type_str == "int64": return "uint64" else: return type_str class ExternLibrary(ABC): _name: str _path: str _symbols: Dict[str, Symbol] _format: bool _grouping: bool def __init__( self, name: str, path: str, format: bool = True, grouping: bool = True, ) -> None: ''' Abstract class for extern library. :param name: name of the library :param path: path of the library :param format: whether to format the generated stub file ''' self._name = name self._path = path self._symbols = {} self._format = format self._grouping = grouping @property def name(self) -> str: return self._name @property def path(self) -> str: return self._path @property def symbols(self) -> Dict[str, Symbol]: return self._symbols @property def grouping(self) -> bool: return self._grouping @abstractmethod def parse_symbols(self, input_file) -> None: pass @abstractmethod def _output_stubs(self) -> str: pass def generate_stub_file(self, output_dir) -> None: file_str = self._output_stubs() if file_str is None or len(file_str) == 0: raise Exception("file_str is empty") output_file = f"{output_dir}/{self._name}.py" with open(output_file, "w") as f: f.write(file_str) f.close() if self._format: subprocess.Popen(["autopep8", "-a", "-r", "-i", output_file], stdout=subprocess.PIPE).communicate() subprocess.Popen(["isort", output_file], stdout=subprocess.PIPE).communicate() class Libdevice(ExternLibrary): _symbol_groups: Dict[str, List[Symbol]] def __init__(self, path) -> None: ''' Constructor for Libdevice. :param path: path of the libdevice library ''' super().__init__("libdevice", path) self._symbol_groups = {} self.is_pure = True @staticmethod def _extract_symbol(line) -> Optional[Symbol]: # Extract symbols from line in the following format: # "define [internal] <ret_type> @<name>(<arg_types>,)" entries = line.split("@") ret_str = entries[0] func_str = entries[1] # Get ret_type, skip internal symbols ret_strs = ret_str.split() if ret_strs[1] == "internal": return None ret_type = convert_type(ret_strs[1]) if ret_type is None: return None # Get function name func_strs = func_str.split("(") func_name = func_strs[0].replace("@", "") op_name = func_name.replace("__nv_", "") if 'ieee' in op_name: return None # Get arg_types arg_strs = func_strs[1].split(",") arg_types = [] arg_names = [] for i, arg_str in enumerate(arg_strs): arg_type = convert_type(arg_str.split()[0]) if arg_type is None: return None arg_name = 'arg' + str(i) arg_types.append(arg_type) arg_names.append(arg_name) if op_name == "sad": # Special case for sad, where the last argument is an unsigned int arg_types[-1] = to_unsigned(arg_types[-1]) elif op_name.startswith("u"): # LLVM does not differentiate between signed and unsigned integer type. # We have to convert the types to unsigned ret_type = to_unsigned(ret_type) for i, arg_type in enumerate(arg_types): arg_types[i] = to_unsigned(arg_type) return Symbol(func_name, op_name, ret_type, arg_names, arg_types) def _group_symbols(self) -> None: symbol_set = {} for symbol in self._symbols.values(): op_name = symbol.op_name symbol_set[op_name] = symbol # Group functions together by renaming. renaming = { 'llabs': 'abs', 'acosf': 'acos', 'acoshf': 'acosh', 'dadd_rd': 'add_rd', 'fadd_rd': 'add_rd', 'dadd_rn': 'add_rn', 'fadd_rn': 'add_rn', 'dadd_ru': 'add_ru', 'fadd_ru': 'add_ru', 'dadd_rz': 'add_rz', 'fadd_rz': 'add_rz', 'asinf': 'asin', 'asinhf': 'asinh', 'atanf': 'atan', 'atan2f': 'atan2', 'atanhf': 'atanh', 'brevll': 'brev', 'cbrtf': 'cbrt', 'ceilf': 'ceil', 'clzll': 'clz', 'copysignf': 'copysign', 'cosf': 'cos', 'coshf': 'cosh', 'cospif': 'cospi', 'cyl_bessel_i0f': 'cyl_bessel_i0', 'cyl_bessel_i1f': 'cyl_bessel_i1', 'fdiv_rd': 'div_rd', 'ddiv_rd': 'div_rd', 'fdiv_rn': 'div_rn', 'ddiv_rn': 'div_rn', 'fdiv_ru': 'div_ru', 'ddiv_ru': 'div_ru', 'fdiv_rz': 'div_rz', 'ddiv_rz': 'div_rz', 'erff': 'erf', 'erfcf': 'erfc', 'erfcinvf': 'erfcinv', 'erfcxf': 'erfcx', 'erfinvf': 'erfinv', 'expf': 'exp', 'exp10f': 'exp10', 'exp2f': 'exp2', 'expm1f': 'expm1', 'fabsf': 'abs', 'fabs': 'abs', 'fast_fdividef': 'fast_dividef', 'fdimf': 'fdim', 'ffsll': 'ffs', 'floorf': 'floor', 'fmaf': 'fma', 'fmaf_rd': 'fma_rd', 'fmaf_rn': 'fma_rn', 'fmaf_ru': 'fma_ru', 'fmaf_rz': 'fma_rz', 'fmodf': 'fmod', 'uhadd': 'hadd', 'hypotf': 'hypot', 'ilogbf': 'ilogb', 'isinff': 'isinf', 'isinfd': 'isinf', 'isnanf': 'isnan', 'isnand': 'isnan', 'j0f': 'j0', 'j1f': 'j1', 'jnf': 'jn', 'ldexpf': 'ldexp', 'lgammaf': 'lgamma', 'llrintf': 'llrint', 'llroundf': 'llround', 'logf': 'log', 'log10f': 'log10', 'log1pf': 'log1p', 'log2f': 'log2', 'logbf': 'logb', 'umax': 'max', 'llmax': 'max', 'ullmax': 'max', 'fmaxf': 'max', 'fmax': 'max', 'umin': 'min', 'llmin': 'min', 'ullmin': 'min', 'fminf': 'min', 'fmin': 'min', 'dmul_rd': 'mul_rd', 'fmul_rd': 'mul_rd', 'dmul_rn': 'mul_rn', 'fmul_rn': 'mul_rn', 'dmul_ru': 'mul_ru', 'fmul_ru': 'mul_ru', 'dmul_rz': 'mul_rz', 'fmul_rz': 'mul_rz', 'umul24': 'mul24', 'umulhi': 'mulhi', 'mul64hi': 'mulhi', 'umul64hi': 'mulhi', 'nearbyintf': 'nearbyint', 'nextafterf': 'nextafter', 'norm3df': 'norm3d', 'norm4df': 'norm4d', 'normcdff': 'normcdf', 'normcdfinvf': 'normcdfinv', 'popcll': 'popc', 'powif': 'pow', 'powi': 'pow', 'powf': 'pow', 'rcbrtf': 'rcbrt', 'frcp_rd': 'rcp_rd', 'drcp_rd': 'rcp_rd', 'frcp_rn': 'rcp_rn', 'drcp_rn': 'rcp_rn', 'frcp_ru': 'rcp_ru', 'drcp_ru': 'rcp_ru', 'frcp_rz': 'rcp_rz', 'drcp_rz': 'rcp_rz', 'remainderf': 'remainder', 'urhadd': 'rhadd', 'rhypotf': 'rhypot', 'rintf': 'rint', 'rnorm3df': 'rnorm3d', 'rnorm4df': 'rnorm4d', 'roundf': 'round', 'rsqrtf': 'rsqrt', 'frsqrt_rn': 'rsqrt_rn', 'usad': 'sad', 'scalbnf': 'scalbn', 'signbitf': 'signbit', 'signbitd': 'signbit', 'sinf': 'sin', 'sinhf': 'sinh', 'sinpif': 'sinpi', 'sqrtf': 'sqrt', 'fsqrt_rd': 'sqrt_rd', 'dsqrt_rd': 'sqrt_rd', 'fsqrt_rn': 'sqrt_rn', 'dsqrt_rn': 'sqrt_rn', 'fsqrt_ru': 'sqrt_ru', 'dsqrt_ru': 'sqrt_ru', 'fsqrt_rz': 'sqrt_rz', 'dsqrt_rz': 'sqrt_rz', 'fsub_rd': 'sub_rd', 'dsub_rd': 'sub_rd', 'fsub_rn': 'sub_rn', 'dsub_rn': 'sub_rn', 'fsub_ru': 'sub_ru', 'dsub_ru': 'sub_ru', 'fsub_rz': 'sub_rz', 'dsub_rz': 'sub_rz', 'tanf': 'tan', 'tanhf': 'tanh', 'tgammaf': 'tgamma', 'truncf': 'trunc', 'y0f': 'y0', 'y1f': 'y1', 'ynf': 'yn' } for symbol in self._symbols.values(): op_name = symbol.op_name if op_name in renaming: op_name = renaming[op_name] symbol._op_name = op_name if op_name in self._symbol_groups: self._symbol_groups[op_name].append(symbol) else: self._symbol_groups[op_name] = [symbol] def parse_symbols(self, input_file) -> None: if len(self.symbols) > 0: return output = subprocess.check_output(["grep", "define", input_file]).decode().splitlines() for line in output: symbol = self._extract_symbol(line) if symbol is None: continue self._symbols[symbol.name] = symbol self._group_symbols() def _output_stubs(self) -> str: # Generate python functions in the following format: # @extern.extern # def <op_name>(<args>, _builder=None): # arg_type_symbol_dict = {[arg_type]: {(symbol, ret_type)}} # return core.extern_elementwise("libdevice", <path>, <args>, <arg_type_symbol_dict>, _builder) import_str = "from . import core\n" import_str += "import os\n" import_str += "import functools\n" header_str = "" header_str += "@functools.lru_cache()\n" header_str += "def libdevice_path():\n" header_str += " import torch\n" header_str += " third_party_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), \"..\", \"third_party\")\n" header_str += " if torch.version.hip is None:\n" header_str += " default = os.path.join(third_party_dir, \"cuda\", \"lib\", \"libdevice.10.bc\")\n" header_str += " else:\n" header_str += " default = ''\n" header_str += " return os.getenv(\"TRITON_LIBDEVICE_PATH\", default)\n" func_str = "" for symbols in self._symbol_groups.values(): func_str += "@core.extern\n" func_name_str = f"def {symbols[0].op_name}(" for arg_name in symbols[0].arg_names: func_name_str += f"{arg_name}, " func_name_str += "_builder=None):\n" return_str = f"\treturn core.extern_elementwise(\"{self._name}\", libdevice_path(), [" for arg_name in symbols[0].arg_names: return_str += f"{arg_name}, " return_str += "], \n" arg_type_symbol_dict_str = "{" for symbol in symbols: arg_type_symbol_dict_str += "(" for arg_type in symbol.arg_types: arg_type_symbol_dict_str += f'core.dtype("{arg_type}"),' ret_type = f'core.dtype("{symbol.ret_type}")' arg_type_symbol_dict_str += "): (\"" + symbol.name + "\", " + ret_type + "),\n" arg_type_symbol_dict_str += "}" return_str += arg_type_symbol_dict_str return_str += f", is_pure={self.is_pure}" return_str += ", _builder=_builder)\n" func_str += func_name_str + return_str + "\n" file_str = import_str + header_str + func_str return file_str class LLVMDisassembler: _path: str _ll_file: str def __init__(self, path) -> None: ''' Invoke llvm-dis to disassemble the given file. :param path: path to llvm-dis ''' self._path = path self._ll_file = "/tmp/extern_lib.ll" def disasm(self, lib_path: str) -> None: subprocess.Popen([self._path, lib_path, "-o", self.ll_file], stdout=subprocess.PIPE).communicate() @property def ll_file(self) -> str: return self._ll_file @property def path(self) -> str: return self._path extern_libs = ["libdevice"] def build( llvm_dis_path: str, lib_path: str, lib_name: str, output_dir: str, ) -> None: ''' Interface function to build the library file. :param llvm_dis_path: path to the llvm-dis binary :param lib_path: path to the external library file :param lib_name: name of the library :param output_dir: path to the output directory ''' if lib_name == "libdevice": extern_lib = Libdevice(lib_path) else: raise Exception(f"Unknown extern library: {lib_name}") llvm_disassembler = LLVMDisassembler(llvm_dis_path) llvm_disassembler.disasm(lib_path) extern_lib.parse_symbols(llvm_disassembler.ll_file) extern_lib.generate_stub_file(output_dir) if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("--llvm-dis", dest="llvm_dis_path", help="Path to llvm-dis", default="llvm-dis") parser.add_argument("--lib-path", dest="lib_path", help="Path to the extern library") parser.add_argument("--lib-name", dest="lib_name", help="Name of the extern library") parser.add_argument("--output", dest="output_dir", help="Output file path", default="/tmp/") args = parser.parse_args() build(args.llvm_dis_path, args.lib_path, args.lib_name, args.output_dir)
14,661
35.746867
130
py
triton
triton-main/python/triton/common/build.py
import contextlib import functools import io import os import shutil import subprocess import sys import sysconfig import setuptools # TODO: is_hip shouldn't be here def is_hip(): import torch return torch.version.hip is not None @functools.lru_cache() def libcuda_dirs(): locs = subprocess.check_output(["whereis", "libcuda.so"]).decode().strip().split()[1:] return [os.path.dirname(loc) for loc in locs] @functools.lru_cache() def rocm_path_dir(): return os.getenv("ROCM_PATH", default="/opt/rocm") @contextlib.contextmanager def quiet(): old_stdout, old_stderr = sys.stdout, sys.stderr sys.stdout, sys.stderr = io.StringIO(), io.StringIO() try: yield finally: sys.stdout, sys.stderr = old_stdout, old_stderr @functools.lru_cache() def cuda_include_dir(): base_dir = os.path.join(os.path.dirname(__file__), os.path.pardir) cuda_path = os.path.join(base_dir, "third_party", "cuda") return os.path.join(cuda_path, "include") def _build(name, src, srcdir): if is_hip(): hip_lib_dir = os.path.join(rocm_path_dir(), "lib") hip_include_dir = os.path.join(rocm_path_dir(), "include") else: cuda_lib_dirs = libcuda_dirs() cu_include_dir = cuda_include_dir() suffix = sysconfig.get_config_var('EXT_SUFFIX') so = os.path.join(srcdir, '{name}{suffix}'.format(name=name, suffix=suffix)) # try to avoid setuptools if possible cc = os.environ.get("CC") if cc is None: # TODO: support more things here. clang = shutil.which("clang") gcc = shutil.which("gcc") cc = gcc if gcc is not None else clang if cc is None: raise RuntimeError("Failed to find C compiler. Please specify via CC environment variable.") # This function was renamed and made public in Python 3.10 if hasattr(sysconfig, 'get_default_scheme'): scheme = sysconfig.get_default_scheme() else: scheme = sysconfig._get_default_scheme() # 'posix_local' is a custom scheme on Debian. However, starting Python 3.10, the default install # path changes to include 'local'. This change is required to use triton with system-wide python. if scheme == 'posix_local': scheme = 'posix_prefix' py_include_dir = sysconfig.get_paths(scheme=scheme)["include"] if is_hip(): ret = subprocess.check_call([cc, src, f"-I{hip_include_dir}", f"-I{py_include_dir}", f"-I{srcdir}", "-shared", "-fPIC", f"-L{hip_lib_dir}", "-lamdhip64", "-o", so]) else: cc_cmd = [cc, src, "-O3", f"-I{cu_include_dir}", f"-I{py_include_dir}", f"-I{srcdir}", "-shared", "-fPIC", "-lcuda", "-o", so] cc_cmd += [f"-L{dir}" for dir in cuda_lib_dirs] ret = subprocess.check_call(cc_cmd) if ret == 0: return so # fallback on setuptools extra_compile_args = [] library_dirs = cuda_lib_dirs include_dirs = [srcdir, cu_include_dir] libraries = ['cuda'] # extra arguments extra_link_args = [] # create extension module ext = setuptools.Extension( name=name, language='c', sources=[src], include_dirs=include_dirs, extra_compile_args=extra_compile_args + ['-O3'], extra_link_args=extra_link_args, library_dirs=library_dirs, libraries=libraries, ) # build extension module args = ['build_ext'] args.append('--build-temp=' + srcdir) args.append('--build-lib=' + srcdir) args.append('-q') args = dict( name=name, ext_modules=[ext], script_args=args, ) with quiet(): setuptools.setup(**args) return so
3,661
30.568966
172
py
triton
triton-main/python/triton/runtime/jit.py
from __future__ import annotations, division import ast import functools import hashlib import inspect import os import subprocess import textwrap from collections import defaultdict, namedtuple from typing import (Callable, Generic, Iterable, List, Optional, TypeVar, Union, cast, overload) from ..common.backend import get_backend TRITON_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) TRITON_VERSION = "2.1.0" def get_cuda_stream(idx=None): if idx is None: idx = get_current_device() try: from torch._C import _cuda_getCurrentRawStream return _cuda_getCurrentRawStream(idx) except ImportError: import torch return torch.cuda.current_stream(idx).cuda_stream def get_current_device(): import torch return torch.cuda.current_device() def set_current_device(idx): import torch torch.cuda.set_device(idx) def get_device_capability(idx): import torch return torch.cuda.get_device_capability(idx) T = TypeVar('T') # ----------------------------------------------------------------------------- # Dependencies Finder # ----------------------------------------------------------------------------- class DependenciesFinder(ast.NodeVisitor): """ This AST visitor is used to find dependencies of a JITFunction. This can be used to invalidate a JITFunction's hash when its source code -- or that of its dependencies -- changes. """ def __init__(self, globals, src) -> None: super().__init__() self.ret = hashlib.md5(src.encode("utf-8")).hexdigest() self.globals = globals def visit_Name(self, node): return self.globals.get(node.id, None) def visit_Attribute(self, node): lhs = self.visit(node.value) while isinstance(lhs, ast.Attribute): lhs = self.visit(lhs.value) if lhs is None or (getattr(lhs, "__name__", "") == "triton" or getattr(lhs, "__name__", "").endswith(".triton")): return None return getattr(lhs, node.attr) def visit_Call(self, node): func = self.visit(node.func) if func is None: return if inspect.isbuiltin(func): return if func.__module__ and (func.__module__.startswith('triton.') or '.triton.' in func.__module__): return assert isinstance(func, JITFunction), f"Function \"{func.__name__}\" is being called from a Triton function but is not a Triton function itself. Decorate it with @triton.jit to fix this" if func.hash is None: tree = ast.parse(func.src) finder = DependenciesFinder(func.__globals__, func.src) finder.visit(tree) func.hash = finder.ret noinline = str(getattr(func, 'noinline', False)) self.ret = (self.ret + func.hash + noinline).encode("utf-8") self.ret = hashlib.md5(self.ret).hexdigest() # ----------------------------------------------------------------------------- # JITFunction # ----------------------------------------------------------------------------- @functools.lru_cache() def version_key(): import pkgutil contents = [] # frontend with open(__file__, "rb") as f: contents += [hashlib.md5(f.read()).hexdigest()] # compiler compiler_path = os.path.join(TRITON_PATH, 'compiler') for lib in pkgutil.iter_modules([compiler_path]): with open(lib.module_finder.find_spec(lib.name).origin, "rb") as f: contents += [hashlib.md5(f.read()).hexdigest()] # backend with open(os.path.join(TRITON_PATH, "_C/libtriton.so"), "rb") as f: contents += [hashlib.md5(f.read()).hexdigest()] # language language_path = os.path.join(TRITON_PATH, 'language') for lib in pkgutil.iter_modules([language_path]): with open(lib.module_finder.find_spec(lib.name).origin, "rb") as f: contents += [hashlib.md5(f.read()).hexdigest()] # ptxas version try: ptxas_version = hashlib.md5(subprocess.check_output(["ptxas", "--version"])).hexdigest() except Exception: ptxas_version = '' return '-'.join(TRITON_VERSION) + '-' + ptxas_version + '-' + '-'.join(contents) class KernelInterface(Generic[T]): run: T def __getitem__(self, grid) -> T: """ A JIT function is launched with: fn[grid](*args, **kwargs). Hence JITFunction.__getitem__ returns a callable proxy that memorizes the grid. """ return cast(T, functools.partial(cast(Callable, self.run), grid=grid)) class JITFunction(KernelInterface[T]): # Hook for inspecting compiled functions and modules cache_hook = None divisibility = 16 @staticmethod def _key_of(arg): if hasattr(arg, "dtype"): return arg.dtype elif isinstance(arg, bool): return "i1" elif isinstance(arg, int): if -2**31 <= arg and arg <= 2**31 - 1: return "i32" elif 2**63 <= arg and arg <= 2**64 - 1: return "u64" else: return "i64" elif isinstance(arg, float): return 'fp32' elif arg is None: return None else: raise TypeError(f'Unsupported type {type(arg)} for {arg}') @staticmethod def _device_of(arg): if hasattr(arg, "device"): if hasattr(arg.device, 'type'): return arg.device.type return '' @staticmethod def _pinned_memory_of(arg): if hasattr(arg, "is_pinned"): if isinstance(arg.is_pinned, Callable): return arg.is_pinned() return False @staticmethod def _spec_of(arg): if hasattr(arg, "data_ptr"): return (arg.data_ptr() % JITFunction.divisibility == 0) elif isinstance(arg, int): return (arg % 16 == 0, arg == 1) return (arg is None, ) def _get_config(self, *args): def is_divisible_by_16(x): if hasattr(x, "data_ptr"): return x.data_ptr() % JITFunction.divisibility == 0 elif isinstance(x, int): return x % JITFunction.divisibility == 0 if x is None: return True return False divisible_by_16 = {i for i, arg in enumerate(args) if is_divisible_by_16(arg) and i not in self.do_not_specialize} equal_to_1 = {i for i, arg in enumerate(args) if not isinstance(arg, bool) and isinstance(arg, int) and arg == 1 and i not in self.do_not_specialize} return namedtuple("instance_descriptor", ["divisible_by_16", "equal_to_1"])(tuple(divisible_by_16), tuple(equal_to_1)) # return _triton.code_gen.instance_descriptor(divisible_by_16, equal_to_1) @staticmethod def _type_of(key): # None are nullptr -- implicitly converted to *i8 if key is None: return '*i8' dtype_str = str(key).split(".")[-1] tys = { "bool": "i1", "float8e4": "fp8e4", "float8e5": "fp8e5", "float8e4b15": "fp8e4b15", "float16": "fp16", "bfloat16": "bf16", "float32": "fp32", "float64": "fp64", "int8": "i8", "int16": "i16", "int32": "i32", "int64": "i64", "uint8": "u8", "uint16": "u16", "uint32": "u32", "uint64": "u64", } # reinterpret can create triton type for v in list(tys.values()): tys[v] = v return key if isinstance(key, str) else f"*{tys[dtype_str]}" def _make_signature(self, sig_key): signature = ",".join([self._type_of(k) for i, k in enumerate(sig_key)]) return signature def _make_constants(self, constexpr_key): constants = dict(zip(self.constexprs, constexpr_key)) return constants def _call_hook(self, key, signature, device, constants, num_warps, num_stages, extern_libs, configs): if JITFunction.cache_hook is None: return False name = self.fn.__name__ module = self.fn.__module__ arg_reprs = ', '.join([f'{name}: {ty}' for name, ty in zip(self.arg_names, key[1])]) repr = f"{name}[num_warps={num_warps}, num_stages={num_stages}]({arg_reprs})" key = str(key) class LegacyCompiler: def __init__(self, module, name): self.module = module self.name = name pass kwargs = dict(signature=signature, device=device, constants=constants, num_warps=num_warps, num_stages=num_stages, extern_libs=extern_libs, configs=configs) return JITFunction.cache_hook(key=key, repr=repr, fn=LegacyCompiler(module, name), compile={"key": key, **kwargs}, is_manual_warmup=False, already_compiled=False) def _get_arg_specialization_key(self, arg) -> str: arg_annotation = self.__annotations__.get(arg, '') if arg_annotation == '': return f'({arg}.data_ptr() % {JITFunction.divisibility} == 0) if hasattr({arg}, "data_ptr") \ else ({arg} % {JITFunction.divisibility} == 0, {arg} == 1) if isinstance({arg}, int) \ else (False,)' elif 'Tensor' in arg_annotation: return f'({arg}.data_ptr() % {JITFunction.divisibility} == 0)' elif arg_annotation == 'int': return f'({arg} % {JITFunction.divisibility} == 0, {arg} == 1)' else: return '(False,)' def _get_arg_sig_key(self, arg) -> str: arg_annotation = self.__annotations__.get(arg, '') if 'Tensor' in arg_annotation: return f'{arg}.dtype' elif arg_annotation == 'bool': return "i1" elif arg_annotation == 'float': return 'fp32' else: return f'_key_of({arg})' def _conclude_device_type(self, device_types: List[str], pinned_memory_flags: List[bool]) -> str: device_types = [device_type for device_type in device_types if device_type != ''] # Return cuda if one of the input tensors is cuda if 'cuda' in device_types: import torch return 'hip' if torch.version.hip else 'cuda' is_cpu = all(device_type == 'cpu' for device_type in device_types) is_pinned_memory = any(pinned_memory_flag for pinned_memory_flag in pinned_memory_flags) # Return cuda if all the input tensors are cpu while the memory is pinned if is_cpu and is_pinned_memory: return 'cuda' return device_types[0] if len(device_types) > 0 else 'cuda' def _make_launcher(self): regular_args = [f'{arg}' for i, arg in enumerate(self.arg_names) if i not in self.constexprs] constexpr_args = [f'{arg}' for i, arg in enumerate(self.arg_names) if i in self.constexprs] args = ', '.join(regular_args) # cache key for regular argument type sig_keys = ', '.join([self._get_arg_sig_key(arg) for arg in regular_args]) device_types = '[' + ', '.join([f'_device_of({arg})' for arg in regular_args]) + ']' pinned_memory_flags = '[' + ', '.join([f'_pinned_memory_of({arg})' for arg in regular_args]) + ']' # cache key for constexpr argument values constexpr_keys = ', '.join(constexpr_args) # cache key for argument specialization specializations = [] for i, arg in enumerate(regular_args): if i in self.do_not_specialize: continue specializations += [self._get_arg_specialization_key(arg)] spec_keys = ', '.join(specializations) grid_args = ','.join([f'"{arg}": {arg}' for arg in self.arg_names]) args_signature = ', '.join(name if dflt == inspect._empty else f'{name} = {dflt}' for name, dflt in zip(self.arg_names, self.arg_defaults)) src = f""" def {self.fn.__name__}({args_signature}, grid=None, num_warps=4, num_stages=3, extern_libs=None, stream=None, warmup=False, device=None, device_type=None): from ..compiler import compile, CompiledKernel sig_key = {sig_keys}, constexpr_key = {f'{constexpr_keys},' if len(constexpr_keys) > 0 else ()} spec_key = {f'{spec_keys},' if len(spec_keys) > 0 else ()} key = (version_key, sig_key, constexpr_key, spec_key, num_warps, num_stages, self.debug) if not extern_libs is None: key = (key, tuple(extern_libs.items())) assert num_warps > 0 and (num_warps & (num_warps - 1)) == 0, "num_warps must be a power of 2" assert grid is not None if callable(grid): grid = grid({{{grid_args}}}) grid_size = len(grid) grid_0 = grid[0] grid_1 = grid[1] if grid_size > 1 else 1 grid_2 = grid[2] if grid_size > 2 else 1 if device_type is None: device_types = [_device_type for _device_type in {device_types} if _device_type != ''] device_type = self._conclude_device_type(device_types, {pinned_memory_flags}) device_backend = None if device_type not in ['cuda', 'hip']: device_backend = get_backend(device_type) if device_backend is None: raise ValueError('Cannot find backend for ' + device_type) if device is None: if device_type in ['cuda', 'hip']: device = get_current_device() set_current_device(device) else: device = device_backend.get_current_device() device_backend.set_current_device(device) if stream is None and not warmup: if device_type in ['cuda', 'hip']: stream = get_cuda_stream(device) else: stream = device_backend.get_stream() bin = cache[device].get(key, None) if bin is not None: if not warmup: bin.c_wrapper(grid_0, grid_1, grid_2, bin.num_warps, bin.shared, stream, bin.cu_function, CompiledKernel.launch_enter_hook, CompiledKernel.launch_exit_hook, bin, {args}) return bin # kernel not cached -- compile else: # build dict of constant values args = [{args}] all_args = {', '.join([f'{arg}' for arg in self.arg_names])}, configs = self._get_config(*all_args), constants = self._make_constants(constexpr_key) constants.update({{i: None for i, arg in enumerate(all_args) if arg is None}}) constants.update({{i: 1 for i in configs[0].equal_to_1}}) # build kernel signature -- doesn't include specialized arguments signature = {{ i: self._type_of(_key_of(arg)) for i, arg in enumerate(all_args) if i not in self.constexprs }} # build stub signature -- includes arguments that are specialized for i, arg in constants.items(): if callable(arg): raise TypeError(f"Callable constexpr at index {{i}} is not supported") if not self._call_hook(key, signature, device, constants, num_warps, num_stages, extern_libs, configs): bin = compile(self, signature=signature, device=device, constants=constants, num_warps=num_warps, num_stages=num_stages, extern_libs=extern_libs, configs=configs, debug=self.debug, device_type=device_type) if not warmup: bin.c_wrapper(grid_0, grid_1, grid_2, bin.num_warps, bin.shared, stream, bin.cu_function, CompiledKernel.launch_enter_hook, CompiledKernel.launch_exit_hook, bin, *args) self.cache[device][key] = bin return bin return None """ scope = {"version_key": version_key(), "get_cuda_stream": get_cuda_stream, "self": self, "_spec_of": self._spec_of, "_key_of": self._key_of, "_device_of": self._device_of, "_pinned_memory_of": self._pinned_memory_of, "cache": self.cache, "__spec__": __spec__, "get_backend": get_backend, "get_current_device": get_current_device, "set_current_device": set_current_device} exec(src, scope) return scope[self.fn.__name__] def __init__(self, fn, version=None, do_not_specialize=None, debug=None, noinline=None): self.fn = fn self.module = fn.__module__ self.version = version # function signature information signature = inspect.signature(fn) self.arg_names = [v.name for v in signature.parameters.values()] self.arg_defaults = [v.default for v in signature.parameters.values()] self.has_defaults = any(v != inspect._empty for v in self.arg_defaults) # specialization hints self.do_not_specialize = [] if do_not_specialize is None else do_not_specialize self.do_not_specialize = {self.arg_names.index(arg) if isinstance(arg, str) else arg for arg in self.do_not_specialize} # function source code (without decorators) self.src = textwrap.dedent(inspect.getsource(fn)) self.src = self.src[self.src.find("def"):] # cache of just-in-time compiled kernels self.cache = defaultdict(dict) self.hash = None # JITFunction can be instantiated as kernel # when called with a grid using __getitem__ self.kernel_decorators = [] self.kernel = None self.debug = True if os.environ.get("TRITON_DEBUG", "0") == "1" else debug self.noinline = noinline # annotations normalize_ty = lambda ty: ty.__name__ if isinstance(ty, type) else ty self.__annotations__ = {name: normalize_ty(ty) for name, ty in fn.__annotations__.items()} # index of constexprs self.constexprs = [self.arg_names.index(name) for name, ty in self.__annotations__.items() if 'constexpr' in ty] # launcher self.run = self._make_launcher() # re-use docs of wrapped function self.__doc__ = fn.__doc__ self.__name__ = fn.__name__ self.__globals__ = fn.__globals__ self.__module__ = fn.__module__ @property def cache_key(self): # TODO : hash should be attribute of `self` if self.hash is None: dependencies_finder = DependenciesFinder(globals=self.__globals__, src=self.src) dependencies_finder.visit(self.parse()) self.hash = dependencies_finder.ret + version_key() return self.hash def warmup(self, *args, **kwargs): return self.run(*map(MockTensor.wrap_dtype, args), **kwargs, warmup=True) # we do not parse `src` in the constructor because # the user might want to monkey-patch self.src dynamically. # Our unit tests do this, for example. def parse(self): tree = ast.parse(self.src) assert isinstance(tree, ast.Module) assert len(tree.body) == 1 assert isinstance(tree.body[0], ast.FunctionDef) return tree def __call__(self, *args, **kwargs): raise RuntimeError("Cannot call @triton.jit'd outside of the scope of a kernel") def __setattr__(self, name, value): # - when kernel decorators change, cached kernel # needs to be cleared if name == 'kernel_decorators': self.kernel = None super(JITFunction, self).__setattr__(name, value) # - when `.src` attribute is set, cache path needs # to be reinitialized if name == 'src': self.hash = None def __repr__(self): return f"JITFunction({self.module}:{self.fn.__name__})" # ----------------------------------------------------------------------------- # `jit` decorator # ----------------------------------------------------------------------------- @overload def jit(fn: T) -> JITFunction[T]: ... @overload def jit( *, version=None, do_not_specialize: Optional[Iterable[int]] = None, debug: Optional[bool] = None, noinline: Optional[bool] = None, ) -> Callable[[T], JITFunction[T]]: ... def jit( fn: Optional[T] = None, *, version=None, do_not_specialize: Optional[Iterable[int]] = None, debug: Optional[bool] = None, noinline: Optional[bool] = None, interpret: Optional[bool] = None, ) -> Union[JITFunction[T], Callable[[T], JITFunction[T]]]: """ Decorator for JIT-compiling a function using the Triton compiler. :note: When a jit'd function is called, arguments are implicitly converted to pointers if they have a :code:`.data_ptr()` method and a `.dtype` attribute. :note: This function will be compiled and run on the GPU. It will only have access to: * python primitives, * builtins within the triton package, * arguments to this function, * other jit'd functions :param fn: the function to be jit-compiled :type fn: Callable """ def decorator(fn: T) -> JITFunction[T]: assert callable(fn) if interpret: from ..interpreter.interpreter import GridSelector return GridSelector(fn) else: return JITFunction( fn, version=version, do_not_specialize=do_not_specialize, debug=debug, noinline=noinline, ) if fn is not None: return decorator(fn) else: return decorator # ----------------------------------------------------------------------------- # Utilities for mocking tensors # ----------------------------------------------------------------------------- class MockTensor: """ Can be used in place of real tensors when calling: kernel.warmup(MockTensor(torch.float32), ...) """ @staticmethod def wrap_dtype(arg): if arg.__class__.__name__ == "dtype" and\ arg.__module__ == "torch": return MockTensor(arg) return arg def __init__(self, dtype): self.dtype = dtype @staticmethod def data_ptr(): return 0 # optimistically assumes multiple of 16 class TensorWrapper: def __init__(self, base, dtype): self.dtype = dtype self.base = base self.is_cuda = base.is_cuda self.device = base.device self.shape = self.base.shape def data_ptr(self): return self.base.data_ptr() def stride(self, i): return self.base.stride(i) def __str__(self) -> str: return f'TensorWrapper[{self.dtype}]({self.base})' def reinterpret(tensor, dtype): if isinstance(tensor, TensorWrapper): if dtype == tensor.base.dtype: # Reinterpreting to the original interpretation; return the base. return tensor.base else: # Reinterpreting a wrapped tensor to a different type. return TensorWrapper(tensor.base, dtype) elif hasattr(tensor, "data_ptr"): # A new wrapper is needed around an unwrapped tensor. return TensorWrapper(tensor, dtype) else: raise TypeError(f'Cannot reinterpret a {type(tensor)}.')
23,031
37.069421
213
py
triton
triton-main/python/triton/runtime/driver.py
import abc import hashlib import os import tempfile from pathlib import Path from ..common.build import _build from .cache import get_cache_manager class DriverBase(metaclass=abc.ABCMeta): CUDA = 0 HIP = 1 @staticmethod def third_party_dir(): return os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "third_party") def __init__(self) -> None: pass # ----------------------------- # CUDA # ----------------------------- class CudaUtils(object): def __new__(cls): if not hasattr(cls, 'instance'): cls.instance = super(CudaUtils, cls).__new__(cls) return cls.instance def __init__(self): dirname = os.path.dirname(os.path.realpath(__file__)) src = Path(os.path.join(dirname, "backends", "cuda.c")).read_text() key = hashlib.md5(src.encode("utf-8")).hexdigest() cache = get_cache_manager(key) fname = "cuda_utils.so" cache_path = cache.get_file(fname) if cache_path is None: with tempfile.TemporaryDirectory() as tmpdir: src_path = os.path.join(tmpdir, "main.c") with open(src_path, "w") as f: f.write(src) so = _build("cuda_utils", src_path, tmpdir) with open(so, "rb") as f: cache_path = cache.put(f.read(), fname, binary=True) import importlib.util spec = importlib.util.spec_from_file_location("cuda_utils", cache_path) mod = importlib.util.module_from_spec(spec) spec.loader.exec_module(mod) self.load_binary = mod.load_binary self.get_device_properties = mod.get_device_properties class CudaDriver(DriverBase): def __new__(cls): if not hasattr(cls, 'instance'): cls.instance = super(CudaDriver, cls).__new__(cls) return cls.instance def __init__(self): self.utils = CudaUtils() self.backend = self.CUDA # ----------------------------- # HIP # ----------------------------- class HIPUtils(object): def __new__(cls): if not hasattr(cls, 'instance'): cls.instance = super(HIPUtils, cls).__new__(cls) return cls.instance def __init__(self): dirname = os.path.dirname(os.path.realpath(__file__)) src = Path(os.path.join(dirname, "backends", "hip.c")).read_text() key = hashlib.md5(src.encode("utf-8")).hexdigest() cache = get_cache_manager(key) fname = "hip_utils.so" cache_path = cache.get_file(fname) if cache_path is None: with tempfile.TemporaryDirectory() as tmpdir: src_path = os.path.join(tmpdir, "main.c") with open(src_path, "w") as f: f.write(src) so = _build("hip_utils", src_path, tmpdir) with open(so, "rb") as f: cache_path = cache.put(f.read(), fname, binary=True) import importlib.util spec = importlib.util.spec_from_file_location("hip_utils", cache_path) mod = importlib.util.module_from_spec(spec) spec.loader.exec_module(mod) self.load_binary = mod.load_binary self.get_device_properties = mod.get_device_properties class HIPDriver(DriverBase): def __new__(cls): if not hasattr(cls, 'instance'): cls.instance = super(HIPDriver, cls).__new__(cls) return cls.instance def __init__(self): self.utils = HIPUtils() self.backend = self.HIP class UnsupportedDriver(DriverBase): def __new__(cls): if not hasattr(cls, 'instance'): cls.instance = super(UnsupportedDriver, cls).__new__(cls) return cls.instance def __init__(self): self.utils = None self.backend = None # ----------------------------- # Driver # ----------------------------- class LazyProxy: def __init__(self, init_fn): self._init_fn = init_fn self._obj = None def _initialize_obj(self): if self._obj is None: self._obj = self._init_fn() def __getattr__(self, name): self._initialize_obj() return getattr(self._obj, name) def __setattr__(self, name, value): if name in ['_init_fn', '_obj']: super().__setattr__(name, value) else: self._initialize_obj() setattr(self._obj, name, value) def __delattr__(self, name): self._initialize_obj() delattr(self._obj, name) def __repr__(self): if self._obj is None: return f"<{self.__class__.__name__} for {self._init_fn} not yet initialized>" return repr(self._obj) def __str__(self): self._initialize_obj() return str(self._obj) def initialize_driver(): import torch if torch.version.hip is not None: return HIPDriver() elif torch.cuda.is_available(): return CudaDriver() else: return UnsupportedDriver() driver = LazyProxy(initialize_driver)
5,045
27.834286
92
py
triton
triton-main/python/triton/interpreter/torch_wrapper.py
try: import torch as _torch except ImportError: _torch = None class TorchWrapper: """ Helps in making torch an optional dependency """ def __getattr__(self, name): if _torch is None: raise ImportError("Triton requires PyTorch to be installed") return getattr(_torch, name) torch = TorchWrapper()
353
17.631579
72
py
triton
triton-main/python/triton/interpreter/tl_lang.py
from __future__ import annotations from ..language import core as lcore from . import torch_wrapper from .core import ExecutionContext from .memory_map import MemoryMap torch = torch_wrapper.torch def _primitive_to_tensor(x): """ Converts various Python primitive data types to PyTorch tensor. """ tensor_args = {"device": "cuda"} if isinstance(x, bool): return torch.tensor([x], dtype=torch.bool, **tensor_args) elif isinstance(x, int): if -(2**31) <= x < 2**31: return torch.tensor([x], dtype=torch.int32, **tensor_args) elif -(2**63) <= x < 2**63: return torch.tensor([x], dtype=torch.int64, **tensor_args) else: raise RuntimeError(f"Nonrepresentable integer {x}.") elif isinstance(x, float): return torch.tensor([x], dtype=torch.float32, **tensor_args) elif torch.is_tensor(x): return x elif isinstance(x, WrappedTensor): return x elif isinstance(x, debugger_constexpr): if x.value is None: return None return _primitive_to_tensor(x.value) elif x is None: return None assert False, f"cannot convert {x} of type {type(x)} to tensor" def _infer_tensor(func): """ A decorator function to harmonize function args: - converts primitives to PyTorch tensors - wraps PyTorch tensors with WrappedTensors """ def wrapper(*args): new_args = tuple(map(lambda v: _primitive_to_tensor(v), args)) new_args = tuple(map(lambda v: WrappedTensor(v) if torch.is_tensor(v) else v, new_args)) return func(*new_args) return wrapper def _tensor_operation(func): """ A decorator function to unwrap WrappedTensors and debugger_constexpr before calling the function. Can be combined with _infer_tensor decorator to harmonize args (everything to torch tensor). """ def wrapper(*args, **kwargs): for arg in args: assert not torch.is_tensor(arg), "unexpected tensor argument" def unwrap_tensor(v): if isinstance(v, WrappedTensor): return v.tensor if isinstance(v, debugger_constexpr): return v.value return v new_args = tuple(map(unwrap_tensor, args)) new_kwargs = {k: unwrap_tensor(v) for k, v in kwargs.items()} result = func(args[0], *new_args[1:], **new_kwargs) return WrappedTensor(result) if torch.is_tensor(result) else result return wrapper class debugger_constexpr: def __init__(self, value): if isinstance(value, debugger_constexpr): self.value = value.value else: self.value = value def __str__(self) -> str: return "debugger_constexpr(" + str(self.value) + ")" def __index__(self) -> int: return self.value def __bool__(self): return bool(self.value) def __ge__(self, other): other = other.value if isinstance(other, debugger_constexpr) else other return self.value >= other def __gt__(self, other): other = other.value if isinstance(other, debugger_constexpr) else other return self.value > other def __le__(self, other): other = other.value if isinstance(other, debugger_constexpr) else other return self.value <= other def __lt__(self, other): other = other.value if isinstance(other, debugger_constexpr) else other return self.value < other def __eq__(self, other): other = other.value if isinstance(other, debugger_constexpr) else other return self.value == other def __or__(self, other): other = other.value if isinstance(other, debugger_constexpr) else other return self.value | other def __ror__(self, other): other = other.value if isinstance(other, debugger_constexpr) else other return self.value | other def __and__(self, other): other = other.value if isinstance(other, debugger_constexpr) else other return self.value & other def __rand__(self, other): other = other.value if isinstance(other, debugger_constexpr) else other return self.value & other def to(self, dtype, bitcast=False, _builder=None): if dtype in [torch.int64]: ret_ty = int elif dtype == torch.bool: ret_ty = bool elif dtype in [torch.float64]: ret_ty = float else: raise ValueError("dtype not supported in debugger") return debugger_constexpr(ret_ty(self.value)) class WrappedTensor: def __init__(self, tensor): self.tensor = tensor def __index__(self) -> int: return self.tensor.item() def __str__(self) -> str: return "wrapped_" + str(self.tensor) def __bool__(self) -> bool: return torch.all(self.tensor == True).item() # noqa: E712 @property def dtype(self): return self.tensor.dtype @_infer_tensor @_tensor_operation def __add__(self, other): return torch.add(self.tensor, other) @_infer_tensor @_tensor_operation def __radd__(self, other): return self.__add__(other) @_infer_tensor @_tensor_operation def __sub__(self, other): return torch.sub(self.tensor, other) @_infer_tensor @_tensor_operation def __rsub__(self, other): return torch.sub(other, self.tensor) @_infer_tensor @_tensor_operation def __mul__(self, other): return torch.mul(self.tensor, other) @_infer_tensor @_tensor_operation def __rmul__(self, other): return self.__mul__(other) @_infer_tensor @_tensor_operation def __truediv__(self, other): return torch.div(self.tensor, other) @_infer_tensor @_tensor_operation def __rtruediv__(self, other): return torch.div(other, self.tensor) @_infer_tensor @_tensor_operation def __floordiv__(self, other): return torch.floor_divide(self.tensor, other) @_infer_tensor @_tensor_operation def __rfloordiv__(self, other): return torch.floor_divide(other, self.tensor) @_infer_tensor @_tensor_operation def __mod__(self, other): return torch.remainder(self.tensor, other) @_infer_tensor @_tensor_operation def __rmod__(self, other): return torch.remainder(other, self.tensor) @_infer_tensor @_tensor_operation def __neg__(self): return -self.tensor @_infer_tensor @_tensor_operation def __invert__(self): return ~self.tensor @_infer_tensor @_tensor_operation def __and__(self, other): return torch.bitwise_and(self.tensor, other) @_infer_tensor @_tensor_operation def __or__(self, other): return torch.bitwise_or(self.tensor, other) @_infer_tensor @_tensor_operation def __xor__(self, other): return torch.bitwise_xor(self.tensor, other) @_infer_tensor @_tensor_operation def __lshift__(self, other): return torch.bitwise_left_shift(self.tensor, other) @_infer_tensor @_tensor_operation def __rshift__(self, other): return torch.bitwise_right_shift(self.tensor, other) @_infer_tensor @_tensor_operation def __gt__(self, other): return self.tensor > other @_infer_tensor @_tensor_operation def __rgt__(self, other): return other > self.tensor @_infer_tensor @_tensor_operation def __ge__(self, other): return self.tensor >= other @_infer_tensor @_tensor_operation def __rge__(self, other): return other >= self.tensor @_infer_tensor @_tensor_operation def __lt__(self, other): return self.tensor < other @_infer_tensor @_tensor_operation def __rlt__(self, other): return other < self.tensor @_infer_tensor @_tensor_operation def __le__(self, other): return self.tensor <= other @_infer_tensor @_tensor_operation def __rle__(self, other): return other <= self.tensor @_infer_tensor @_tensor_operation def __eq__(self, other): return torch.equal(self.tensor, other) @_infer_tensor @_tensor_operation def __ne__(self, other): return not torch.equal(self.tensor, other) @_tensor_operation def __getitem__(self, slices): return self.tensor.__getitem__(slices) # if isinstance(slices, slice): # slices = [slices] # src_shape = self.shape # dst_shape = [] # curr = 0 # for sl in slices: # if isinstance(sl, constexpr) and sl.value is None: # dst_shape.append(1) # elif sl == slice(None, None, None): # dst_shape.append(src_shape[curr].value) # curr += 1 # ret = torch.reshape(self.tensor, dst_shape, ) # return ret @_tensor_operation def to(self, dtype, bitcast=False): return self.tensor.to(dtype) # if isinstance(bitcast, constexpr): # bitcast = bitcast.value # if bitcast: # return semantic.bitcast(self, dtype, ) # return semantic.cast(self, dtype, ) def _constexpr_to_value(v): if isinstance(v, debugger_constexpr): return v.value return v class TritonLangProxy: _memory_map: MemoryMap _context: ExecutionContext def __init__(self, memory_map: MemoryMap, context: ExecutionContext): self._memory_map = memory_map self._context = context # Types # Removed void, int1, float8, uint16, uint32, uint64, pi32_t # constexpr = debugger_constexpr # Program functions @_tensor_operation def load( self, pointer: torch.Tensor, mask: torch.Tensor = None, other=0.0, cache_modifier="", eviction_policy="", volatile=False, ): return self._memory_map.load(pointer, mask, other) @_tensor_operation def store(self, pointer: torch.Tensor, value: torch.Tensor, mask=None): return self._memory_map.store(pointer, value, mask) @_tensor_operation def program_id(self, axis): assert axis < len(self._context.program_id) return torch.tensor([self._context.program_id[axis]], dtype=torch.int32, device="cuda") @_tensor_operation def num_programs(self, axis): assert axis < len(self._context.program_size) return torch.tensor([self._context.program_size[axis]], dtype=torch.int32, device="cuda") @_tensor_operation def arange(self, start, end): return torch.arange(start=start, end=end, dtype=torch.int32, device="cuda") @_tensor_operation def zeros(self, shape, dtype): for i, d in enumerate(shape): if not isinstance(d, debugger_constexpr): raise TypeError(f"Shape element {i} must have type `constexpr`") if not isinstance(d.value, int): raise TypeError(f"Shape element {i} must have type `constexpr[int]`, got `constexpr[{type(d.value)}]") shape = [x.value for x in shape] if isinstance(dtype, lcore.dtype): if dtype.is_fp32(): dtype = torch.float32 elif dtype.is_fp16(): dtype = torch.float16 elif dtype.is_bf16(): dtype = torch.bfloat16 elif dtype.is_int32(): dtype = torch.int32 elif dtype.is_int16(): dtype = torch.int16 elif dtype.is_int8(): dtype = torch.int8 else: raise TypeError(f"Unsupported dtype {dtype}") return torch.zeros(size=shape, dtype=dtype, device="cuda") @_tensor_operation def dequantize(self, input, scale, shift, nbit, dst_ty=None): if dst_ty is None: dst_ty = torch.float16 raise NotImplementedError() @_tensor_operation def broadcast(self, input, other): raise NotImplementedError() @_tensor_operation def broadcast_to(self, input, shape): raise NotImplementedError() @_tensor_operation def cat(self, input, shape): raise NotImplementedError() @_tensor_operation def reshape(self, input, shape): raise NotImplementedError() @_tensor_operation def dot(self, input, other, trans_a=False, trans_b=False, allow_tf32=True): assert input.dtype == other.dtype if trans_a: input = input.T if trans_b: other = other.T return torch.matmul(input=input, other=other) @_tensor_operation def atomic_cas(self, pointer, cmp, val): stored = self._memory_map.load(pointer, None, 0.0) if not isinstance(cmp, torch.Tensor): cmp = torch.tensor([cmp], dtype=stored.dtype, device="cuda") if not isinstance(val, torch.Tensor): val = torch.tensor([val], dtype=stored.dtype, device="cuda") if stored == cmp: self._memory_map.store(pointer, val, None) return stored @_tensor_operation def atomic_xchg(self, pointer, val, mask=None): if isinstance(val, int): val = torch.tensor([val], dtype=torch.int32, device="cuda") stored = self._memory_map.load(pointer, mask, 0.0) self._memory_map.store(pointer, val, mask) return stored @_tensor_operation def atomic_add(self, pointer, val, mask=None): # arbitrary other value as it will masked during storing stored = self._memory_map.load(pointer, mask, 0.0) result = stored + val self._memory_map.store(pointer, result, mask) return stored @_tensor_operation def atomic_max(self, pointer, val, mask=None): stored = self._memory_map.load(pointer, mask, 0.0) result = torch.maximum(stored, val) self._memory_map.store(pointer, result, mask) return stored @_tensor_operation def atomic_min(self, pointer, val, mask=None): stored = self._memory_map.load(pointer, mask, 0.0) result = torch.minimum(stored, val) self._memory_map.store(pointer, result, mask) return stored @_tensor_operation def atomic_and(self, pointer, val, mask=None): stored = self._memory_map.load(pointer, mask, 0) result = torch.bitwise_and(stored, val) self._memory_map.store(pointer, result, mask) return stored @_tensor_operation def atomic_or(self, pointer, val, mask=None): stored = self._memory_map.load(pointer, mask, 0) result = torch.bitwise_or(stored, val) self._memory_map.store(pointer, result, mask) return stored @_tensor_operation def atomic_xor(self, pointer, val, mask=None): stored = self._memory_map.load(pointer, mask, 0) result = torch.bitwise_xor(stored, val) self._memory_map.store(pointer, result, mask) return stored @_tensor_operation def where(self, condition, x, y): condition = _primitive_to_tensor(condition) x = _primitive_to_tensor(x) y = _primitive_to_tensor(y) return torch.where(condition, x, y) @_tensor_operation def umulhi(self, x, y): raise NotImplementedError() @_tensor_operation def fdiv(self, x, y, ieee_rounding=False): raise NotImplementedError() @_tensor_operation def exp(self, x): return torch.exp(x) @_tensor_operation def log(self, x): return torch.log(x) @_tensor_operation def cos(self, x): return torch.cos(x) @_tensor_operation def sin(self, x): return torch.sin(x) @_tensor_operation def sqrt(self, x): return torch.sqrt(x) @_tensor_operation def globaltimer(self): raise NotImplementedError() @_tensor_operation def clock(self): raise NotImplementedError() @_tensor_operation def debug_barrier(self): raise NotImplementedError() @_tensor_operation def multiple_of(self, input, values): return input @_tensor_operation def max_contiguous(self, input, values): return input @_tensor_operation def max_constancy(self, input, values): return input @_tensor_operation def abs(self, x): return torch.abs(x) @_tensor_operation def cdiv(self, x, div): return (x + div - 1) // div @_tensor_operation def minimum(self, x, y): if isinstance(x, int): x = torch.tensor(x, device="cuda") if isinstance(y, int): y = torch.tensor(y, device="cuda") return torch.minimum(x, y) @_tensor_operation def maximum(self, x, y): return torch.maximum(x, y) @_tensor_operation def sigmoid(self, x): raise NotImplementedError() @_tensor_operation def softmax(self, x, ieee_rounding=False): raise NotImplementedError() @_tensor_operation def ravel(self, x): raise NotImplementedError() @_tensor_operation def swizzle2d(self, i, j, size_i, size_j, size_g): raise NotImplementedError() @_tensor_operation def zeros_like(self, input): raise NotImplementedError() @_tensor_operation def max(self, input, axis=None): if axis is None: return torch.max(input) return torch.max(input, dim=axis).values @_tensor_operation def argmax(self, input, axis): raise NotImplementedError() @_tensor_operation def min(self, input, axis=None): if axis is None: return torch.min(input) return torch.min(input, dim=axis).values @_tensor_operation def argmin(self, input, axis): raise NotImplementedError() @_tensor_operation def sum(self, input, axis=None): if axis is None: return torch.sum(input) return torch.sum(input, dim=axis) @_tensor_operation def xor_sum(self, input, axis): raise NotImplementedError() @_tensor_operation def cumsum(self, input, axis=None): if axis is None: return torch.cumsum(input) return torch.cumsum(input, dim=axis) @_tensor_operation def cumprod(self, input, axis=None): if axis is None: return torch.cumprod(input) return torch.cumprod(input, dim=axis)
18,501
27.819315
118
py
triton
triton-main/python/triton/interpreter/interpreter.py
import itertools import random from typing import Tuple from .. import language as tl # import .language.core as lcore from ..language import core as lcore from . import torch_wrapper from .core import ExecutionContext from .memory_map import MemoryMap from .tl_lang import (TritonLangProxy, WrappedTensor, _primitive_to_tensor, debugger_constexpr) torch = torch_wrapper.torch tl_method_backup = {} def get_proxy_method(proxy, name): method = getattr(proxy, name) def fun(*args, **kwarg): return method(*args, **kwarg) return fun def attach_triton(module, proxy): method_list = [func for func in dir(TritonLangProxy) if func[0] != "_"] for name in method_list: if hasattr(module, name): attr = getattr(module, name) tl_method_backup[name] = attr if callable(attr): setattr(module, name, get_proxy_method(proxy, name)) else: setattr(module, name, getattr(proxy, name)) def detach_triton(module): for name, method in tl_method_backup.items(): setattr(module, name, method) def program_ids_from_grid(grid: Tuple[int, ...]) -> Tuple[int, ...]: # reverse the grid dimensions and generate the range for each dimension reversed_grid = reversed(grid) ranges_for_each_dimension = [range(dim) for dim in reversed_grid] # gen all combinations index_combinations = list(itertools.product(*ranges_for_each_dimension)) random.shuffle(index_combinations) for index_combination in index_combinations: yield index_combination class DebuggerFunction: def __init__(self, func, grid=(1,)): self.func = func self.grid = grid def _is_constexpr(self, name): return name in self.func.__annotations__ and self.func.__annotations__[name] is lcore.constexpr def _get_constexpr(self): result = [] for name, annotation in self.func.__annotations__.items(): if annotation is lcore.constexpr: result.append(name) return result def _assert_constexpr(self, **kwargs): constexp = self._get_constexpr() missing = [i for i in constexp if i not in kwargs.keys()] assert len(missing) == 0, f"You must specify constexpr {missing}" def _get_grid(self, **kwargs): if callable(self.grid): return self.grid(kwargs) else: return self.grid def __call__(self, *args, **kwargs): self._assert_constexpr(**kwargs) memory = MemoryMap() def convert_arg(v): name, arg = v if torch.is_tensor(arg): ptr = memory.add_tensor(arg) return WrappedTensor(torch.tensor([ptr], dtype=torch.int64, device="cuda")) if self._is_constexpr(name): return debugger_constexpr(arg) return WrappedTensor(_primitive_to_tensor(arg)) new_args = tuple(map(convert_arg, zip(self.func.__code__.co_varnames, args))) new_kwargs = {k: convert_arg((k, v)) for (k, v) in kwargs.items() if k not in ["num_warps", "num_stages"]} grid = self._get_grid(**kwargs) for program_id in program_ids_from_grid(grid): proxy = TritonLangProxy(memory, ExecutionContext(program_id, grid)) attach_triton(tl, proxy) self.func(*new_args, **new_kwargs) detach_triton(tl) class GridSelector: """ Entry point of the debugger """ def __init__(self, func): version = torch.__version__ assert version[0] == "2", f"Triton Debugger only supports torch >= 2.0, using {version}" self.func = func def __getitem__(self, grid): return DebuggerFunction(self.func, grid) def __call__(self, *args, **kwargs): return DebuggerFunction(self.func)(*args, **kwargs) class AutotuneGridSelector: def __init__(self, func, autotune_params): self.func = func self.autotune_params = autotune_params def __getitem__(self, grid): return AutotuneRunner(self.func, self.autotune_params, grid) def __call__(self, *args, **kwargs): return AutotuneRunner(self.func, self.autotune_params)(*args, **kwargs) class AutotuneRunner: def __init__(self, func, autotune_params, grid=None): self.func = func self.autotune_params = autotune_params self.grid = grid def __call__(self, *args, **kwargs): assert len(self.autotune_params["configs"]) >= 1 for config in self.autotune_params["configs"][1:]: def convert_arg(v): if torch.is_tensor(v): return torch.clone(v) return v new_args = tuple(map(convert_arg, args)) new_kwargs = {k: convert_arg(v) for k, v in kwargs.items()} if self.grid: self.func[self.grid](*new_args, **new_kwargs, **config.kwargs) else: self.func(*new_args, **new_kwargs, **config.kwargs) main_config = self.autotune_params["configs"][0] if self.grid: self.func[self.grid](*args, **kwargs, **main_config.kwargs) else: self.func(*args, **kwargs, **main_config.kwargs) def triton_debug_autotune(**kwars): def wrapper(func): return AutotuneGridSelector(func, kwars) return wrapper
5,412
30.47093
114
py
triton
triton-main/python/triton/interpreter/memory_map.py
from __future__ import annotations import dataclasses from . import torch_wrapper torch = torch_wrapper.torch @dataclasses.dataclass class RegisteredStorage: storage: torch.Storage dtype: torch.dtype size: int ptr: int @property def end_ptr(self) -> int: return self.ptr + self.size @property def access_tensor(self) -> torch.Tensor: return torch.tensor(self.storage, dtype=self.dtype, device=self.storage.device) def ensure_immutable(self): assert self.storage.data_ptr() == self.ptr and self.storage.size() == self.size class MemoryMap: storages: [RegisteredStorage] def __init__(self): self.storages = [] def _get_registered_storage(self, pointer: torch.Tensor): max_pointer = torch.max(pointer).item() min_pointer = torch.min(pointer).item() registered_storage = next( filter( lambda registered: min_pointer >= registered.ptr and max_pointer < registered.end_ptr, self.storages ), None, ) if registered_storage is None: raise Exception("Storage not found or pointers spanning multiple tensors") registered_storage.ensure_immutable() return registered_storage def add_tensor(self, t: torch.Tensor): storage = t.untyped_storage() self.storages.append(RegisteredStorage(storage, t.dtype, storage.size(), storage.data_ptr())) return t.data_ptr() def load( self, pointer: torch.Tensor, mask: torch.Tensor = None, other=0.0, ): assert pointer.is_cuda assert 0 < pointer.dim() < 3 assert pointer.dtype == torch.int64 if mask is None: mask = torch.ones_like(pointer).bool() assert mask.is_cuda assert 0 < mask.dim() < 3 assert mask.dtype == torch.bool mask = mask.expand(pointer.size()) if torch.all(~mask): # Todo: The type is wrong here, we can't determine the correct type return torch.full_like(pointer, fill_value=other, dtype=torch.float16, device="cuda") registered_storage = self._get_registered_storage(pointer[mask]) access_tensor = registered_storage.access_tensor index_tensor = pointer - registered_storage.ptr block = torch.full_like(pointer, fill_value=other, dtype=access_tensor.dtype, device="cuda") block[mask] = access_tensor[index_tensor[mask]] return block def store(self, pointer: torch.Tensor, value: torch.Tensor, mask=None): assert 0 < pointer.dim() < 3 assert pointer.dtype == torch.int64 if mask is None: mask = torch.ones_like(pointer).bool() assert 0 < mask.dim() < 3 assert mask.dtype == torch.bool mask = mask.expand(pointer.size()) if torch.all(~mask): return registered_storage = self._get_registered_storage(pointer[mask]) access_tensor = registered_storage.access_tensor index_tensor = pointer - registered_storage.ptr access_tensor[index_tensor[mask]] = value[mask].to(access_tensor.dtype)
3,187
29.951456
116
py
triton
triton-main/python/triton/compiler/make_launcher.py
import hashlib import os import tempfile from ..common import _build from ..runtime.cache import get_cache_manager from ..runtime.jit import version_key def is_hip(): import torch return torch.version.hip is not None # ----- stub -------- def make_so_cache_key(version_hash, signature, constants): # Get unique key for the compiled code signature = {k: 'ptr' if v[0] == '*' else v for k, v in signature.items()} key = f"{version_hash}-{''.join(signature.values())}{constants}" key = hashlib.md5(key.encode("utf-8")).hexdigest() return key def make_stub(name, signature, constants): # name of files that are cached so_cache_key = make_so_cache_key(version_key(), signature, constants) so_cache_manager = get_cache_manager(so_cache_key) so_name = f"{name}.so" # retrieve stub from cache if it exists cache_path = so_cache_manager.get_file(so_name) if cache_path is None: with tempfile.TemporaryDirectory() as tmpdir: src = generate_launcher(constants, signature) src_path = os.path.join(tmpdir, "main.c") with open(src_path, "w") as f: f.write(src) so = _build(name, src_path, tmpdir) with open(so, "rb") as f: return so_cache_manager.put(f.read(), so_name, binary=True) else: return cache_path # ----- source code generation -------- def ty_to_cpp(ty): if ty[0] == '*': return "hipDeviceptr_t" if is_hip() else "CUdeviceptr" return { "i1": "int32_t", "i8": "int8_t", "i16": "int16_t", "i32": "int32_t", "i64": "int64_t", "u32": "uint32_t", "u64": "uint64_t", "fp16": "float", "bf16": "float", "fp32": "float", "f32": "float", "fp64": "double", }[ty] def generate_launcher(constants, signature): arg_decls = ', '.join(f"{ty_to_cpp(ty)} arg{i}" for i, ty in signature.items()) def _extracted_type(ty): if ty[0] == '*': return "PyObject*" return { 'i1': 'int32_t', 'i32': 'int32_t', 'i64': 'int64_t', 'u32': 'uint32_t', 'u64': 'uint64_t', 'fp16': 'float', 'bf16': 'float', 'fp32': 'float', 'f32': 'float', 'fp64': 'double', }[ty] def format_of(ty): return { "PyObject*": "O", "float": "f", "double": "d", "long": "l", "uint32_t": "I", "int32_t": "i", "uint64_t": "K", "int64_t": "L", }[ty] format = "iiiiiKKOOO" + ''.join([format_of(_extracted_type(ty)) for ty in signature.values()]) # generate glue code if is_hip(): src = f""" #define __HIP_PLATFORM_AMD__ #include <hip/hip_runtime.h> #include <Python.h> #include <stdio.h> static inline void gpuAssert(hipError_t code, const char *file, int line) {{ if (code != HIP_SUCCESS) {{ const char* prefix = "Triton Error [HIP]: "; const char* str = hipGetErrorString(code); char err[1024] = {{0}}; snprintf(err, 1024, "%s Code: %d, Messsage: %s", prefix, code, str ); PyErr_SetString(PyExc_RuntimeError, err); }} }} #define HIP_CHECK(ans) {{ gpuAssert((ans), __FILE__, __LINE__); }} static void _launch(int gridX, int gridY, int gridZ, int num_warps, int shared_memory, hipStream_t stream, hipFunction_t function, {arg_decls}) {{ void *params[] = {{ {', '.join(f"&arg{i}" for i in signature.keys() if i not in constants)} }}; if (gridX*gridY*gridZ > 0) {{ HIP_CHECK(hipModuleLaunchKernel(function, gridX, gridY, gridZ, 64*num_warps, 1, 1, shared_memory, stream, params, 0)); }} }} typedef struct _DevicePtrInfo {{ hipDeviceptr_t dev_ptr; bool valid; }} DevicePtrInfo; static inline DevicePtrInfo getPointer(PyObject *obj, int idx) {{ DevicePtrInfo ptr_info; ptr_info.dev_ptr = 0; ptr_info.valid = true; if (PyLong_Check(obj)) {{ ptr_info.dev_ptr = (hipDeviceptr_t)PyLong_AsUnsignedLongLong(obj); return ptr_info; }} if (obj == Py_None) {{ // valid nullptr return ptr_info; }} PyObject *ptr = PyObject_GetAttrString(obj, "data_ptr"); if (ptr) {{ PyObject *empty_tuple = PyTuple_New(0); PyObject *ret = PyObject_Call(ptr, empty_tuple, NULL); Py_DECREF(empty_tuple); Py_DECREF(ptr); if (!PyLong_Check(ret)) {{ PyErr_SetString(PyExc_TypeError, "data_ptr method of Pointer object must return 64-bit int"); ptr_info.valid = false; return ptr_info; }} ptr_info.dev_ptr = (hipDeviceptr_t)PyLong_AsUnsignedLongLong(ret); if (!ptr_info.dev_ptr) return ptr_info; uint64_t dev_ptr; hipError_t status = hipPointerGetAttribute(&dev_ptr, HIP_POINTER_ATTRIBUTE_DEVICE_POINTER, ptr_info.dev_ptr); if (status == hipErrorInvalidValue) {{ PyErr_Format(PyExc_ValueError, "Pointer argument (at %d) cannot be accessed from Triton (cpu tensor?)", idx); ptr_info.valid = false; }} ptr_info.dev_ptr = (hipDeviceptr_t)dev_ptr; return ptr_info; }} PyErr_SetString(PyExc_TypeError, "Pointer argument must be either uint64 or have data_ptr method"); return ptr_info; }} static PyObject* launch(PyObject* self, PyObject* args) {{ int gridX, gridY, gridZ; uint64_t _stream; uint64_t _function; int num_warps; int shared_memory; PyObject *launch_enter_hook = NULL; PyObject *launch_exit_hook = NULL; PyObject *compiled_kernel = NULL; {' '.join([f"{_extracted_type(ty)} _arg{i}; " for i, ty in signature.items()])} if (!PyArg_ParseTuple(args, \"{format}\", &gridX, &gridY, &gridZ, &num_warps, &shared_memory, &_stream, &_function, &launch_enter_hook, &launch_exit_hook, &compiled_kernel, {', '.join(f"&_arg{i}" for i, ty in signature.items())})) {{ return NULL; }} if (launch_enter_hook != Py_None) {{ PyObject_CallObject(launch_enter_hook, args); }} // raise exception asap {"; ".join([f"DevicePtrInfo ptr_info{i} = getPointer(_arg{i}, {i}); if (!ptr_info{i}.valid) return NULL;" if ty[0] == "*" else "" for i, ty in signature.items()])}; _launch(gridX, gridY, gridZ, num_warps, shared_memory, (hipStream_t)_stream, (hipFunction_t)_function, {', '.join(f"ptr_info{i}.dev_ptr" if ty[0]=="*" else f"_arg{i}" for i, ty in signature.items())}); if (launch_exit_hook != Py_None) {{ PyObject_CallObject(launch_exit_hook, args); }} if (PyErr_Occurred()) {{ return NULL; }} // return None Py_INCREF(Py_None); return Py_None; }} static PyMethodDef ModuleMethods[] = {{ {{"launch", launch, METH_VARARGS, "Entry point for all kernels with this signature"}}, {{NULL, NULL, 0, NULL}} // sentinel }}; static struct PyModuleDef ModuleDef = {{ PyModuleDef_HEAD_INIT, \"__triton_launcher\", NULL, //documentation -1, //size ModuleMethods }}; PyMODINIT_FUNC PyInit___triton_launcher(void) {{ PyObject *m = PyModule_Create(&ModuleDef); if(m == NULL) {{ return NULL; }} PyModule_AddFunctions(m, ModuleMethods); return m; }} """ else: src = f""" #include \"cuda.h\" #include <stdbool.h> #include <Python.h> static inline void gpuAssert(CUresult code, const char *file, int line) {{ if (code != CUDA_SUCCESS) {{ const char* prefix = "Triton Error [CUDA]: "; const char* str; cuGetErrorString(code, &str); char err[1024] = {{0}}; strcat(err, prefix); strcat(err, str); PyErr_SetString(PyExc_RuntimeError, err); }} }} #define CUDA_CHECK(ans) {{ gpuAssert((ans), __FILE__, __LINE__); }} static void _launch(int gridX, int gridY, int gridZ, int num_warps, int shared_memory, CUstream stream, CUfunction function, {arg_decls}) {{ void *params[] = {{ {', '.join(f"&arg{i}" for i in signature.keys() if i not in constants)} }}; if(gridX*gridY*gridZ > 0){{ CUDA_CHECK(cuLaunchKernel(function, gridX, gridY, gridZ, 32*num_warps, 1, 1, shared_memory, stream, params, 0)); }} }} typedef struct _DevicePtrInfo {{ CUdeviceptr dev_ptr; bool valid; }} DevicePtrInfo; static inline DevicePtrInfo getPointer(PyObject *obj, int idx) {{ DevicePtrInfo ptr_info; ptr_info.dev_ptr = 0; ptr_info.valid = true; if (PyLong_Check(obj)) {{ ptr_info.dev_ptr = PyLong_AsUnsignedLongLong(obj); return ptr_info; }} if (obj == Py_None) {{ // valid nullptr return ptr_info; }} PyObject *ptr = PyObject_GetAttrString(obj, "data_ptr"); if(ptr){{ PyObject *empty_tuple = PyTuple_New(0); PyObject *ret = PyObject_Call(ptr, empty_tuple, NULL); Py_DECREF(empty_tuple); Py_DECREF(ptr); if (!PyLong_Check(ret)) {{ PyErr_SetString(PyExc_TypeError, "data_ptr method of Pointer object must return 64-bit int"); ptr_info.valid = false; return ptr_info; }} ptr_info.dev_ptr = PyLong_AsUnsignedLongLong(ret); if(!ptr_info.dev_ptr) return ptr_info; uint64_t dev_ptr; int status = cuPointerGetAttribute(&dev_ptr, CU_POINTER_ATTRIBUTE_DEVICE_POINTER, ptr_info.dev_ptr); if (status == CUDA_ERROR_INVALID_VALUE) {{ PyErr_Format(PyExc_ValueError, "Pointer argument (at %d) cannot be accessed from Triton (cpu tensor?)", idx); ptr_info.valid = false; }} ptr_info.dev_ptr = dev_ptr; Py_DECREF(ret); // Thanks ChatGPT! return ptr_info; }} PyErr_SetString(PyExc_TypeError, "Pointer argument must be either uint64 or have data_ptr method"); return ptr_info; }} static PyObject* launch(PyObject* self, PyObject* args) {{ int gridX, gridY, gridZ; uint64_t _stream; uint64_t _function; int num_warps; int shared_memory; PyObject *launch_enter_hook = NULL; PyObject *launch_exit_hook = NULL; PyObject *compiled_kernel = NULL; {' '.join([f"{_extracted_type(ty)} _arg{i}; " for i, ty in signature.items()])} if(!PyArg_ParseTuple(args, \"{format}\", &gridX, &gridY, &gridZ, &num_warps, &shared_memory, &_stream, &_function, &launch_enter_hook, &launch_exit_hook, &compiled_kernel, {', '.join(f"&_arg{i}" for i, ty in signature.items())})) {{ return NULL; }} if (launch_enter_hook != Py_None) {{ PyObject_CallObject(launch_enter_hook, args); }} // raise exception asap {"; ".join([f"DevicePtrInfo ptr_info{i} = getPointer(_arg{i}, {i}); if (!ptr_info{i}.valid) return NULL;" if ty[0] == "*" else "" for i, ty in signature.items()])}; _launch(gridX, gridY, gridZ, num_warps, shared_memory, (CUstream)_stream, (CUfunction)_function, {', '.join(f"ptr_info{i}.dev_ptr" if ty[0]=="*" else f"_arg{i}"for i, ty in signature.items())}); if (launch_exit_hook != Py_None) {{ PyObject_CallObject(launch_exit_hook, args); }} if(PyErr_Occurred()) {{ return NULL; }} // return None Py_INCREF(Py_None); return Py_None; }} static PyMethodDef ModuleMethods[] = {{ {{"launch", launch, METH_VARARGS, "Entry point for all kernels with this signature"}}, {{NULL, NULL, 0, NULL}} // sentinel }}; static struct PyModuleDef ModuleDef = {{ PyModuleDef_HEAD_INIT, \"__triton_launcher\", NULL, //documentation -1, //size ModuleMethods }}; PyMODINIT_FUNC PyInit___triton_launcher(void) {{ PyObject *m = PyModule_Create(&ModuleDef); if(m == NULL) {{ return NULL; }} PyModule_AddFunctions(m, ModuleMethods); return m; }} """ return src
11,854
30.697861
239
py
triton
triton-main/python/triton/compiler/compiler.py
from __future__ import annotations import functools import hashlib import json import os import re import subprocess import tempfile from collections import namedtuple from pathlib import Path from typing import Any, Tuple from .._C.libtriton.triton import (add_external_libs, compile_ptx_to_cubin, get_shared_memory_size, ir, translate_llvmir_to_hsaco, translate_llvmir_to_ptx, translate_triton_gpu_to_llvmir) from ..common.backend import get_backend # from ..runtime import driver, jit, JITFunction # TODO: runtime.errors from ..runtime.autotuner import OutOfResources from ..runtime.cache import get_cache_manager from ..runtime.driver import driver from ..runtime.jit import (JITFunction, get_cuda_stream, get_current_device, get_device_capability, version_key) from ..tools.disasm import extract from .code_generator import ast_to_ttir from .make_launcher import make_stub def inline_triton_ir(mod): pm = ir.pass_manager(mod.context) pm.enable_debug() pm.add_inliner_pass() pm.run(mod) return mod def ttir_compute_capability_rewrite(mod, arch): # For hardware without support, we must rewrite all load/store # with block (tensor) pointers into tensors of pointers pm = ir.pass_manager(mod.context) pm.enable_debug() if _is_cuda(arch): pm.add_rewrite_tensor_pointer_pass(arch) pm.run(mod) return mod def optimize_ttir(mod, arch): mod = inline_triton_ir(mod) mod = ttir_compute_capability_rewrite(mod, arch) pm = ir.pass_manager(mod.context) pm.enable_debug() pm.add_inliner_pass() pm.add_triton_combine_pass() pm.add_canonicalizer_pass() pm.add_reorder_broadcast_pass() pm.add_cse_pass() pm.add_licm_pass() pm.add_symbol_dce_pass() pm.run(mod) return mod def ttir_to_ttgir(mod, num_warps): pm = ir.pass_manager(mod.context) pm.enable_debug() pm.add_convert_triton_to_tritongpu_pass(num_warps) pm.run(mod) return mod def optimize_ttgir(mod, num_stages, arch): pm = ir.pass_manager(mod.context) pm.enable_debug() pm.add_tritongpu_coalesce_pass() pm.add_tritongpu_remove_layout_conversions_pass() if isinstance(arch, int): pm.add_tritongpu_accelerate_matmul_pass(arch) pm.add_tritongpu_remove_layout_conversions_pass() pm.add_tritongpu_optimize_dot_operands_pass() pm.add_tritongpu_pipeline_pass(num_stages) pm.add_tritongpu_prefetch_pass() pm.add_tritongpu_optimize_dot_operands_pass() pm.add_tritongpu_remove_layout_conversions_pass() pm.add_tritongpu_decompose_conversions_pass() pm.add_tritongpu_reorder_instructions_pass() pm.add_cse_pass() pm.add_symbol_dce_pass() pm.run(mod) return mod def _add_external_libs(mod, libs): for name, path in libs.items(): if len(name) == 0 or len(path) == 0: return add_external_libs(mod, list(libs.keys()), list(libs.values())) def ttgir_to_llir(mod, extern_libs, arch): if extern_libs: _add_external_libs(mod, extern_libs) # TODO: separate tritongpu_to_llvmir for different backends if _is_cuda(arch): return translate_triton_gpu_to_llvmir(mod, arch, False) else: return translate_triton_gpu_to_llvmir(mod, 0, True) # PTX translation @functools.lru_cache() def ptx_get_version(cuda_version) -> int: ''' Get the highest PTX version supported by the current CUDA driver. ''' assert isinstance(cuda_version, str) major, minor = map(int, cuda_version.split('.')) if major == 12: return 80 + minor if major == 11: return 70 + minor if major == 10: return 63 + minor raise RuntimeError("Triton only support CUDA 10.0 or higher") @functools.lru_cache() def path_to_ptxas(): base_dir = os.path.join(os.path.dirname(__file__), os.pardir) paths = [ os.environ.get("TRITON_PTXAS_PATH", ""), os.path.join(base_dir, "third_party", "cuda", "bin", "ptxas") ] for ptxas in paths: ptxas_bin = ptxas.split(" ")[0] if os.path.exists(ptxas_bin) and os.path.isfile(ptxas_bin): result = subprocess.check_output([ptxas_bin, "--version"], stderr=subprocess.STDOUT) if result is not None: version = re.search(r".*release (\d+\.\d+).*", result.decode("utf-8"), flags=re.MULTILINE) if version is not None: return ptxas, version.group(1) raise RuntimeError("Cannot find ptxas") def llir_to_ptx(mod: Any, arch: int, ptx_version: int = None) -> str: ''' Translate TritonGPU module to PTX code. :param mod: a TritonGPU dialect module :return: PTX code ''' if ptx_version is None: _, cuda_version = path_to_ptxas() ptx_version = ptx_get_version(cuda_version) return translate_llvmir_to_ptx(mod, arch, ptx_version) def ptx_to_cubin(ptx: str, arch: int): ''' Compile TritonGPU module to cubin. :param ptx: ptx code :param compute_capability: compute capability :return: str ''' ptxas, _ = path_to_ptxas() return compile_ptx_to_cubin(ptx, ptxas, arch) # AMDGCN translation def get_amdgcn_bitcode_paths(arch): gpu_arch_agnostic_bitcode_libraries = ["opencl.bc", "ocml.bc", "ockl.bc", "oclc_finite_only_off.bc", "oclc_daz_opt_off.bc", "oclc_correctly_rounded_sqrt_on.bc", "oclc_unsafe_math_off.bc", "oclc_wavefrontsize64_on.bc"] gfx_arch = arch[1] gfx_arch_id = re.search('gfx(\\w+)', gfx_arch).group(1).strip() gpu_arch_specific_bitcode_library = 'oclc_isa_version_' + gfx_arch_id + ".bc" bitcode_path_dir = os.path.join(Path(__file__).parent.resolve(), "third_party/rocm/lib/bitcode/") amdgcn_bitcode_paths = {} i = 1 for bc_lib in gpu_arch_agnostic_bitcode_libraries: bc_path = bitcode_path_dir + bc_lib if os.path.exists(bc_path): amdgcn_bitcode_paths['library_' + str(i)] = bc_path i += 1 bc_gfx_path = bitcode_path_dir + gpu_arch_specific_bitcode_library if os.path.exists(bc_gfx_path): amdgcn_bitcode_paths['library_' + str(i)] = bc_gfx_path return amdgcn_bitcode_paths def get_amdgpu_arch_fulldetails(): """ get the amdgpu fulll ISA details for compiling: i.e., arch_triple: amdgcn-amd-amdhsa; arch_name: gfx906; arch_features: sramecc+:xnack- """ try: # TODO: package rocm.cc with Triton rocm_path_dir = os.getenv("ROCM_PATH", default="/opt/rocm") rocminfo = subprocess.check_output(rocm_path_dir + '/bin/rocminfo').decode() gfx_arch_details = re.search('amd.*', rocminfo).group(0).strip().split('--') arch_triple = gfx_arch_details[0] arch_name_features = gfx_arch_details[1].split(':') arch_name = arch_name_features[0] arch_features = "" if (len(arch_name_features) == 3): arch_features = "+" + re.search('\\w+', arch_name_features[1]).group(0) + ","\ "-" + re.search('\\w+', arch_name_features[2]).group(0) return [arch_triple, arch_name, arch_features] except BaseException: return None def llir_to_amdgcn_and_hsaco(mod: Any, gfx_arch: str, gfx_triple: str, gfx_features: str) -> Tuple[str, str]: ''' Translate TritonGPU module to HSACO code based on full details of gpu architecture. :param mod: a TritonGPU dialect module :return: - AMDGCN code - Path to HSACO object ''' return translate_llvmir_to_hsaco(mod, gfx_arch, gfx_triple, gfx_features) # ------------------------------------------------------------------------------ # compiler # ------------------------------------------------------------------------------ def get_kernel_name(src: str, pattern: str) -> str: ''' Get kernel name from PTX code. This Kernel name is required when launching the kernel. ''' # There is a name mangling in PTX codegen, so the original kernel names in Triton IR are not available in PTX/cubin. assert src for line in src.split('\n'): line = line.strip() if line.startswith(pattern): return line.split()[-1] def convert_type_repr(x): match = re.search(r'!tt\.ptr<(.*)>', x) if match is not None: return '*' + convert_type_repr(match.group(1)) return x def make_hash(fn, arch, **kwargs): if isinstance(fn, JITFunction): configs = kwargs["configs"] signature = kwargs["signature"] constants = kwargs.get("constants", dict()) num_warps = kwargs.get("num_warps", 4) num_stages = kwargs.get("num_stages", 3) debug = kwargs.get("debug", False) # Get unique key for the compiled code get_conf_key = lambda conf: (sorted(conf.divisible_by_16), sorted(conf.equal_to_1)) configs_key = [get_conf_key(conf) for conf in configs] key = f"{fn.cache_key}-{''.join(signature.values())}-{configs_key}-{constants}-{num_warps}-{num_stages}-{debug}-{arch}" return hashlib.md5(key.encode("utf-8")).hexdigest() assert isinstance(fn, str) return hashlib.md5((Path(fn).read_text() + version_key()).encode("utf-8")).hexdigest() # - ^\s*tt\.func\s+ : match the start of the string, any leading whitespace, the keyword func, # and any following whitespace # - (public\s+)? : optionally match the keyword public and any following whitespace # - (@\w+) : match an @ symbol followed by one or more word characters # (letters, digits, or underscores), and capture it as group 1 (the function name) # - (\((?:%\w+: \S+(?: \{\S+ = \S+ : \S+\})?(?:, )?)*\)) : match a pair of parentheses enclosing # zero or more arguments separated by commas, and capture it as group 2 (the argument list) mlir_prototype_pattern = r'^\s*tt\.func\s+(?:public\s+)?(@\w+)(\((?:%\w+: \S+(?: \{\S+ = \S+ : \S+\})?(?:, )?)*\))\s*\{\s*$' ptx_prototype_pattern = r"\.(?:visible|extern)\s+\.(?:entry|func)\s+(\w+)\s*\(([^)]*)\)" prototype_pattern = { "ttir": mlir_prototype_pattern, "ttgir": mlir_prototype_pattern, "ptx": ptx_prototype_pattern, } mlir_arg_type_pattern = r'%\w+: ([^,^\)\s]+)(?: \{\S+ = \S+ : \S+\})?,?' ptx_arg_type_pattern = r"\.param\s+\.(\w+)" arg_type_pattern = { "ttir": mlir_arg_type_pattern, "ttgir": mlir_arg_type_pattern, "ptx": ptx_arg_type_pattern, } ttgir_num_warps_pattern = r'"triton_gpu.num-warps"\s?=\s?(\d+)\s?:' def _get_jsonable_constants(constants): def _is_jsonable(x): try: json.dumps(x) return True except (TypeError, OverflowError): return False serialized_constants = {} for constant in constants: if _is_jsonable(constants[constant]): serialized_constants[constant] = constants[constant] return serialized_constants def parse_mlir_module(path, context): module = ir.parse_mlir_module(path, context) # module takes ownership of the context module.context = context return module instance_descriptor = namedtuple("instance_descriptor", ["divisible_by_16", "equal_to_1"], defaults=[set(), set()]) # TODO: architecture descriptor class def _is_cuda(arch): return isinstance(arch, int) def get_architecture_descriptor(capability): try: import torch except ImportError: raise ImportError("Triton requires PyTorch to be installed") if capability is None: if torch.version.hip is None: device = get_current_device() capability = get_device_capability(device) capability = capability[0] * 10 + capability[1] else: capability = get_amdgpu_arch_fulldetails() return capability def add_rocm_stages(arch, extern_libs, stages): extern_libs.update(get_amdgcn_bitcode_paths(arch)) for key in list(extern_libs): if extern_libs[key] == '' or extern_libs[key] is None: extern_libs.pop(key) gfx_arch_full_details = arch gfx_arch = os.environ.get('MI_GPU_ARCH', gfx_arch_full_details[1]) if gfx_arch is None: raise RuntimeError('gfx_arch is None (not specified)') stages["amdgcn"] = (lambda path: Path(path).read_text(), lambda src: llir_to_amdgcn_and_hsaco(src, gfx_arch, gfx_arch_full_details[0], gfx_arch_full_details[2])) def add_cuda_stages(arch, extern_libs, stages): stages["ptx"] = (lambda path: Path(path).read_text(), lambda src: llir_to_ptx(src, arch)) stages["cubin"] = (lambda path: Path(path).read_bytes(), lambda src: ptx_to_cubin(src, arch)) def compile(fn, **kwargs): # Get device type to decide which backend should be used device_type = kwargs.get("device_type", "cuda") _device_backend = get_backend(device_type) if device_type in ["cuda", "hip"]: arch = get_architecture_descriptor(kwargs.get("cc", None)) else: _device_backend = get_backend(device_type) assert _device_backend arch = _device_backend.get_architecture_descriptor(**kwargs) is_cuda = device_type == "cuda" and _is_cuda(arch) is_hip = device_type in ["cuda", "hip"] and not is_cuda context = ir.context() constants = kwargs.get("constants", dict()) num_warps = kwargs.get("num_warps", 4) num_stages = kwargs.get("num_stages", 3 if is_cuda and arch >= 75 else 2) extern_libs = kwargs.get("extern_libs", dict()) if extern_libs is None: extern_libs = dict() debug = kwargs.get("debug", False) # build compilation stages stages = dict() stages["ast"] = (lambda path: fn, None) stages["ttir"] = (lambda path: parse_mlir_module(path, context), lambda src: optimize_ttir(ast_to_ttir(src, signature, configs[0], constants, debug=debug, arch=arch), arch)) stages["ttgir"] = (lambda path: parse_mlir_module(path, context), lambda src: optimize_ttgir(ttir_to_ttgir(src, num_warps), num_stages, arch)) stages["llir"] = (lambda path: Path(path).read_text(), lambda src: ttgir_to_llir(src, extern_libs, arch)) if is_cuda: add_cuda_stages(arch, extern_libs, stages) elif is_hip: add_rocm_stages(arch, extern_libs, stages) else: _device_backend.add_stages(arch, extern_libs, stages) # find out the signature of the function if isinstance(fn, JITFunction): configs = kwargs.get("configs", None) signature = kwargs["signature"] if configs is None: configs = [instance_descriptor()] assert len(configs) == 1 kwargs["configs"] = configs name = fn.__name__ first_stage = 0 if isinstance(signature, str): signature = {k: v.strip() for k, v in enumerate(signature.split(","))} kwargs["signature"] = signature else: assert isinstance(fn, str) _, ir_name = os.path.basename(fn).split(".") src = Path(fn).read_text() import re match = re.search(prototype_pattern[ir_name], src, re.MULTILINE) name, signature = match.group(1), match.group(2) types = re.findall(arg_type_pattern[ir_name], signature) if ir_name == 'ttgir': num_warps_matches = re.findall(ttgir_num_warps_pattern, src) assert len(num_warps_matches) == 1, "Expected exactly one match for num_warps" assert "num_warps" not in kwargs or int(num_warps_matches[0]) == num_warps, "num_warps in ttgir does not match num_warps in compile" num_warps = int(num_warps_matches[0]) param_tys = [convert_type_repr(ty) for ty in types] signature = {k: v for k, v in enumerate(param_tys)} first_stage = list(stages.keys()).index(ir_name) # cache manager if is_cuda or is_hip: so_path = make_stub(name, signature, constants) else: so_path = _device_backend.make_launcher_stub(name, signature, constants) # create cache manager fn_cache_manager = get_cache_manager(make_hash(fn, arch, **kwargs)) # determine name and extension type of provided function if isinstance(fn, JITFunction): name, ext = fn.__name__, "ast" else: name, ext = os.path.basename(fn).split(".") # load metadata if any metadata = None metadata_filename = f"{name}.json" # The group is addressed by the metadata metadata_group = fn_cache_manager.get_group( metadata_filename ) or {} metadata_path = metadata_group.get(metadata_filename) if metadata_path is not None: with open(metadata_path) as f: metadata = json.load(f) else: metadata = {"num_warps": num_warps, "num_stages": num_stages, "constants": _get_jsonable_constants(constants), "debug": debug, "arch": arch, } if ext == "ptx": assert "shared" in kwargs, "ptx compilation must provide shared memory size" metadata["shared"] = kwargs["shared"] # Add device type to meta information metadata["device_type"] = device_type first_stage = list(stages.keys()).index(ext) asm = dict() module = fn # run compilation pipeline and populate metadata for ir_name, (parse, compile_kernel) in list(stages.items())[first_stage:]: ir_filename = f"{name}.{ir_name}" if ir_name == ext: next_module = parse(fn) else: path = metadata_group.get(ir_filename) if path is None: next_module = compile_kernel(module) if ir == "amdgcn": extra_file_name = f"{name}.hsaco_path" metadata_group[ir_filename] = fn_cache_manager.put(next_module[0], ir_filename) metadata_group[extra_file_name] = fn_cache_manager.put(next_module[1], extra_file_name) else: metadata_group[ir_filename] = fn_cache_manager.put(next_module, ir_filename) fn_cache_manager.put(next_module, ir_filename) else: if ir_name == "amdgcn": extra_file_name = f"{name}.hsaco_path" hasco_path = metadata_group.get(extra_file_name) assert hasco_path is not None, "Expected to have hsaco_path in metadata when we have the amdgcn" next_module = (parse(path), parse(hasco_path)) else: next_module = parse(path) if ir_name == "cubin": asm[ir_name] = next_module elif ir_name == "amdgcn": asm[ir_name] = str(next_module[0]) else: asm[ir_name] = str(next_module) if ir_name == "llir" and "shared" not in metadata: metadata["shared"] = get_shared_memory_size(module) if ir_name == "ptx": metadata["name"] = get_kernel_name(next_module, pattern='// .globl') if ir_name == "amdgcn": metadata["name"] = get_kernel_name(next_module[0], pattern='.globl') asm["hsaco_path"] = next_module[1] if not is_cuda and not is_hip: _device_backend.add_meta_info(ir_name, module, next_module, metadata, asm) module = next_module # write-back metadata, if it didn't come from the cache if metadata_path is None: metadata_group[metadata_filename] = fn_cache_manager.put(json.dumps(metadata), metadata_filename, binary=False) fn_cache_manager.put_group(metadata_filename, metadata_group) # return handle to compiled kernel return CompiledKernel(fn, so_path, metadata, asm) class CompiledKernel: # Hooks for external tools to monitor the execution of triton kernels launch_enter_hook = None launch_exit_hook = None def __init__(self, fn, so_path, metadata, asm): # initialize launcher import importlib.util spec = importlib.util.spec_from_file_location("__triton_launcher", so_path) mod = importlib.util.module_from_spec(spec) self.fn = fn spec.loader.exec_module(mod) self.c_wrapper = getattr(mod, "launch") # initialize metadata self.shared = metadata["shared"] if "shared" in metadata else 0 self.num_warps = metadata["num_warps"] self.num_stages = metadata["num_stages"] self.constants = metadata["constants"] self.device_type = metadata["device_type"] self.device_backend = get_backend(self.device_type) if self.device_type not in ["cuda", "hip"] else None # initialize asm dict self.asm = asm # binaries are lazily initialized # because it involves doing runtime things # (e.g., checking amount of shared memory on current device) self.metadata = metadata self.cu_module = None self.cu_function = None def _init_handles(self): if self.cu_module is not None: return if self.device_type in ["cuda", "hip"]: device = get_current_device() bin_path = { driver.HIP: "hsaco_path", driver.CUDA: "cubin" }[driver.backend] max_shared = driver.utils.get_device_properties(device)["max_shared_mem"] fn_load_binary = driver.utils.load_binary else: assert self.device_backend device = self.device_backend.get_current_device() bin_path = self.device_backend.get_kernel_bin() max_shared = self.device_backend.get_device_properties(device)["max_shared_mem"] fn_load_binary = self.device_backend.get_load_binary_fn() if self.shared > max_shared: raise OutOfResources(self.shared, max_shared, "shared memory") mod, func, n_regs, n_spills = fn_load_binary(self.metadata["name"], self.asm[bin_path], self.shared, device) self.n_spills = n_spills self.n_regs = n_regs self.cu_module = mod self.cu_function = func def __getattribute__(self, name): if name == 'c_wrapper': self._init_handles() return super().__getattribute__(name) def __getitem__(self, grid): self._init_handles() def runner(*args, stream=None): if stream is None: if self.device_type in ["cuda", "rocm"]: stream = get_cuda_stream() else: stream = get_backend(self.device_type).get_stream(None) self.c_wrapper(grid[0], grid[1], grid[2], self.num_warps, self.shared, stream, self.cu_function, CompiledKernel.launch_enter_hook, CompiledKernel.launch_exit_hook, self, *args) return runner def get_sass(self, fun=None): if 'sass' in self.asm: return self.asm['sass'] fd, path = tempfile.mkstemp() try: with open(fd, 'wb') as cubin: cubin.write(self.asm['cubin']) self.sass = extract(path, fun) finally: os.remove(path) self.asm['sass'] = self.sass return self.sass
23,660
36.797125
144
py
triton
triton-main/python/triton/language/core.py
from __future__ import annotations from contextlib import contextmanager from enum import Enum from functools import wraps from typing import Callable, List, Sequence, TypeVar from .._C.libtriton.triton import ir from ..runtime.jit import jit from . import math, semantic T = TypeVar('T') TRITON_MAX_TENSOR_NUMEL = 131072 TRITON_BUILTIN = "__triton_builtin__" def builtin(fn: T) -> T: """Mark a function as a builtin.""" assert callable(fn) @wraps(fn) def wrapper(*args, **kwargs): if "_builder" not in kwargs or kwargs["_builder"] is None: raise ValueError( "Did you forget to add @triton.jit ? " "(`_builder` argument must be provided outside of JIT functions.)" ) return fn(*args, **kwargs) setattr(wrapper, TRITON_BUILTIN, True) return wrapper def is_builtin(fn) -> bool: """Is this a registered triton builtin function?""" return getattr(fn, TRITON_BUILTIN, False) def _to_tensor(x, builder): if isinstance(x, bool): return tensor(builder.get_int1(x), int1) # Note: compile-time const integers are represented by unsigned values elif isinstance(x, int): if -2**31 <= x < 2**31: return tensor(builder.get_int32(x), int32) elif 2**31 <= x < 2**32: return tensor(builder.get_int32(x), uint32) elif -2**63 <= x < 2**63: return tensor(builder.get_int64(x), int64) elif 2**63 <= x < 2**64: return tensor(builder.get_int64(x), uint64) else: raise RuntimeError(f'Nonrepresentable integer {x}.') elif isinstance(x, float): min_float32 = 2 ** -126 max_float32 = (2 - 2**-23) * 2**127 abs_x = __builtins__['abs'](x) if abs_x == float("inf") or\ abs_x == 0.0 or \ x != x or \ min_float32 <= abs_x <= max_float32: return tensor(builder.get_fp32(x), float32) else: return tensor(builder.get_fp64(x), float64) elif isinstance(x, constexpr): return _to_tensor(x.value, builder) elif isinstance(x, tensor): return x assert False, f"cannot convert {x} of type {type(x)} to tensor" class dtype: SINT_TYPES = ['int8', 'int16', 'int32', 'int64'] UINT_TYPES = ['int1', 'uint8', 'uint16', 'uint32', 'uint64'] FP_TYPES = ['fp8e4b15', 'fp8e4', 'fp8e5', 'fp16', 'bf16', 'fp32', 'fp64'] STANDARD_FP_TYPES = ['fp16', 'bf16', 'fp32', 'fp64'] OTHER_TYPES = ['void'] class SIGNEDNESS(Enum): SIGNED = 0 UNSIGNED = 1 def __init__(self, name): self.name = name assert name in dtype.SINT_TYPES + dtype.UINT_TYPES + dtype.FP_TYPES + dtype.OTHER_TYPES, name if name in dtype.SINT_TYPES: self.int_signedness = dtype.SIGNEDNESS.SIGNED self.int_bitwidth = int(name.split('int')[-1]) self.primitive_bitwidth = self.int_bitwidth elif name in dtype.UINT_TYPES: self.int_signedness = dtype.SIGNEDNESS.UNSIGNED self.int_bitwidth = int(name.split('int')[-1]) self.primitive_bitwidth = self.int_bitwidth elif name in dtype.FP_TYPES: if name == 'fp8e4b15': self.fp_mantissa_width = 3 self.primitive_bitwidth = 8 self.exponent_bias = 15 elif name == 'fp8e4': self.fp_mantissa_width = 3 self.primitive_bitwidth = 8 self.exponent_bias = 7 elif name == 'fp8e5': self.fp_mantissa_width = 2 self.primitive_bitwidth = 8 self.exponent_bias = 15 elif name == 'fp16': self.fp_mantissa_width = 10 self.primitive_bitwidth = 16 self.exponent_bias = 15 elif name == 'bf16': self.fp_mantissa_width = 7 self.primitive_bitwidth = 16 self.exponent_bias = 127 elif name == 'fp32': self.fp_mantissa_width = 23 self.primitive_bitwidth = 32 self.exponent_bias = 127 elif name == 'fp64': self.fp_mantissa_width = 53 self.primitive_bitwidth = 64 self.exponent_bias = 1023 else: raise RuntimeError(f'Unsupported floating-point type {name}') elif name == 'void': self.primitive_bitwidth = 0 def is_fp8(self): return 'fp8' in self.name def is_fp8e4(self): return self.name == 'fp8e4' def is_fp8e4b15(self): return self.name == 'fp8e4b15' def is_fp16(self): return self.name == 'fp16' def is_bf16(self): return self.name == 'bf16' def is_fp32(self): return self.name == 'fp32' def is_fp64(self): return self.name == 'fp64' def is_int1(self): return self.name == 'int1' def is_int8(self): return self.name == 'int8' def is_int16(self): return self.name == 'int16' def is_int32(self): return self.name == 'int32' def is_int64(self): return self.name == 'int64' def is_uint8(self): return self.name == 'uint8' def is_uint16(self): return self.name == 'uint16' def is_uint32(self): return self.name == 'uint32' def is_uint64(self): return self.name == 'uint64' def is_floating(self): return self.name in dtype.FP_TYPES def is_standard_floating(self): return self.name in dtype.STANDARD_FP_TYPES def is_int_signed(self): return self.name in dtype.SINT_TYPES def is_int_unsigned(self): return self.name in dtype.UINT_TYPES def is_int(self): return self.name in dtype.SINT_TYPES + dtype.UINT_TYPES def is_bool(self): return self.is_int1() @staticmethod def is_void(): raise RuntimeError("Not implemented") @staticmethod def is_block(): return False @staticmethod def is_ptr(): return False def __eq__(self, other: dtype): if not isinstance(other, dtype): return False return self.name == other.name def __ne__(self, other: dtype): return not self.__eq__(other) def __hash__(self): return hash((self.name,)) @property def scalar(self): return self def to_ir(self, builder: ir.builder) -> ir.type: if self.name == 'void': return builder.get_void_ty() elif self.name == 'int1': return builder.get_int1_ty() elif self.name in ('int8', 'uint8'): return builder.get_int8_ty() elif self.name in ('int16', 'uint16'): return builder.get_int16_ty() elif self.name in ('int32', 'uint32'): return builder.get_int32_ty() elif self.name in ('int64', 'uint64'): return builder.get_int64_ty() elif self.name == 'fp8e5': return builder.get_fp8e5_ty() elif self.name == 'fp8e4': return builder.get_fp8e4_ty() elif self.name == 'fp8e4b15': return builder.get_fp8e4b15_ty() elif self.name == 'fp16': return builder.get_half_ty() elif self.name == 'bf16': return builder.get_bf16_ty() elif self.name == 'fp32': return builder.get_float_ty() elif self.name == 'fp64': return builder.get_double_ty() raise ValueError(f'fail to convert {self} to ir type') def __str__(self): return self.name @property def cache_key_part(self) -> str: """See cache_key_part() in triton.cc.""" return self.name def __repr__(self): return f'triton.language.{self.name}' class pointer_type(dtype): def __init__(self, element_ty: dtype, address_space: int = 1): if not isinstance(element_ty, dtype): raise TypeError('element_ty is a {type(element_ty).__name__}.') self.element_ty = element_ty self.address_space = address_space self.name = self.__str__() def to_ir(self, builder: ir.builder) -> ir.pointer_type: return builder.get_ptr_ty(self.element_ty.to_ir(builder), 1) def __str__(self): return f'pointer<{self.element_ty}>' def __repr__(self): return self.__str__() def is_ptr(self): return True def __eq__(self, other: pointer_type) -> bool: if not isinstance(other, pointer_type): return False return self.element_ty == other.element_ty and self.address_space == other.address_space def __ne__(self, other: pointer_type) -> bool: return not self.__eq__(other) @property def scalar(self): return self class block_type(dtype): def __init__(self, element_ty: dtype, shape: List): self.element_ty = element_ty # Note that block_type's shape is a list of int # while tensor's shape is a list of constexpr. # shape can be empty ([]) when an input is a 0D tensor. if not shape: raise TypeError('0d block_type is forbidden') if isinstance(shape[0], constexpr): shape = [s.value for s in shape] self.shape = shape self.numel = 1 for s in self.shape: self.numel *= s if self.numel > TRITON_MAX_TENSOR_NUMEL: raise ValueError(f"numel ({self.numel}) exceeds triton maximum tensor numel ({TRITON_MAX_TENSOR_NUMEL})") self.name = self.__str__() def to_ir(self, builder: ir.builder) -> ir.block_type: return builder.get_block_ty(self.element_ty.to_ir(builder), self.shape) def __str__(self): return f'<{self.shape}, {self.element_ty}>' def __repr__(self): return self.__str__() def is_block(self): return True def get_block_shapes(self) -> List[int]: return self.shape def __eq__(self, other: block_type) -> bool: if not isinstance(other, block_type): return False return self.element_ty == other.element_ty and self.shape == other.shape def __ne__(self, other: block_type) -> bool: return not self.__eq__(other) @property def scalar(self): return self.element_ty class function_type(dtype): def __init__(self, ret_types: List[dtype], param_types: List[dtype]) -> None: self.ret_types = ret_types self.param_types = param_types def __str__(self): return f'fn ({self.param_types}) -> {self.ret_types}' def to_ir(self, builder: ir.builder): ir_param_types = [ty.to_ir(builder) for ty in self.param_types] ret_types = [ret_type.to_ir(builder) for ret_type in self.ret_types] return builder.get_function_ty(ir_param_types, ret_types) # scalar types void = dtype('void') int1 = dtype('int1') int8 = dtype('int8') int16 = dtype('int16') int32 = dtype('int32') int64 = dtype('int64') uint8 = dtype('uint8') uint16 = dtype('uint16') uint32 = dtype('uint32') uint64 = dtype('uint64') float8e5 = dtype('fp8e5') float8e4 = dtype('fp8e4') float8e4b15 = dtype('fp8e4b15') float16 = dtype('fp16') bfloat16 = dtype('bf16') float32 = dtype('fp32') float64 = dtype('fp64') # pointer types pi32_t = pointer_type(int32) # ----------------------- # constexpr # ----------------------- class constexpr: """ This class is used to store a value that is known at compile-time. """ def __init__(self, value): if isinstance(value, constexpr): self.value = value.value else: self.value = value def __repr__(self) -> str: return f"constexpr[{self.value}]" def __index__(self): return self.value def __add__(self, other): return constexpr(self.value + other.value) def __radd__(self, other): return constexpr(other.value + self.value) def __sub__(self, other): return constexpr(self.value - other.value) def __rsub__(self, other): return constexpr(other.value - self.value) def __mul__(self, other): return constexpr(self.value * other.value) def __mod__(self, other): return constexpr(self.value % other.value) def __rmul__(self, other): return constexpr(other.value * self.value) def __truediv__(self, other): return constexpr(self.value / other.value) def __rtruediv__(self, other): return constexpr(other.value / self.value) def __floordiv__(self, other): return constexpr(self.value // other.value) def __rfloordiv__(self, other): return constexpr(other.value // self.value) def __gt__(self, other): return constexpr(self.value > other.value) def __rgt__(self, other): return constexpr(other.value > self.value) def __ge__(self, other): return constexpr(self.value >= other.value) def __rge__(self, other): return constexpr(other.value >= self.value) def __lt__(self, other): return constexpr(self.value < other.value) def __rlt__(self, other): return constexpr(other.value < self.value) def __le__(self, other): return constexpr(self.value <= other.value) def __rle__(self, other): return constexpr(other.value <= self.value) def __eq__(self, other): return constexpr(self.value == other.value) def __ne__(self, other): return constexpr(self.value != other.value) def __bool__(self): return bool(self.value) def __neg__(self): return constexpr(-self.value) def __and__(self, other): return constexpr(self.value & other.value) def logical_and(self, other): return constexpr(self.value and other.value) def __or__(self, other): return constexpr(self.value | other.value) def __xor__(self, other): return constexpr(self.value ^ other.value) def logical_or(self, other): return constexpr(self.value or other.value) def __pos__(self): return constexpr(+self.value) def __invert__(self): return constexpr(~self.value) def __pow__(self, other): return constexpr(self.value ** other.value) def __rshift__(self, other): return constexpr(self.value >> other.value) def __lshift__(self, other): return constexpr(self.value << other.value) def __not__(self): return constexpr(not self.value) def __call__(self, *args, **kwds): return self.value(*args, **kwds) class tensor: def __init__(self, handle, type: dtype): # IR handle self.handle = handle # Block shape self.shape = (1, ) if type.is_block(): self.shape = type.shape self.numel = 1 for s in self.shape: self.numel *= s self.numel = constexpr(self.numel) self.type = type # Tensor type (can be block_type) # Following the practice in pytorch, dtype is scalar type self.dtype = type.scalar self.shape = [constexpr(s) for s in self.shape] def __str__(self) -> str: # ex. "float32[3,4]" return str(self.dtype) + '[' + ','.join(str(s) for s in self.shape) + ']' @builtin def __add__(self, other, _builder=None): other = _to_tensor(other, _builder) return semantic.add(self, other, _builder) def __radd__(self, other, _builder=None): return self.__add__(other, _builder=_builder) @builtin def __sub__(self, other, _builder=None): other = _to_tensor(other, _builder) return semantic.sub(self, other, _builder) def __rsub__(self, other, _builder=None): other = _to_tensor(other, _builder) return semantic.sub(other, self, _builder) @builtin def __mul__(self, other, _builder=None): other = _to_tensor(other, _builder) return semantic.mul(self, other, _builder) def __rmul__(self, other, _builder=None): return self.__mul__(other, _builder=_builder) @builtin def __truediv__(self, other, _builder=None): other = _to_tensor(other, _builder) return semantic.truediv(self, other, _builder) def __rtruediv__(self, other, _builder=None): other = _to_tensor(other, _builder) return semantic.truediv(other, self, _builder) @builtin def __floordiv__(self, other, _builder=None): other = _to_tensor(other, _builder) return semantic.floordiv(self, other, _builder) @builtin def __rfloordiv__(self, other, _builder=None): other = _to_tensor(other, _builder) return semantic.floordiv(other, self, _builder) @builtin def __mod__(self, other, _builder=None): other = _to_tensor(other, _builder) return semantic.mod(self, other, _builder) @builtin def __rmod__(self, other, _builder=None): other = _to_tensor(other, _builder) return semantic.mod(other, self, _builder) # unary operators @builtin def __neg__(self, _builder=None): return semantic.minus(self, _builder) @builtin def __invert__(self, _builder=None): return semantic.invert(self, _builder) # bitwise operators @builtin def __and__(self, other, _builder=None): other = _to_tensor(other, _builder) return semantic.and_(self, other, _builder) @builtin def __rand__(self, other, _builder=None): other = _to_tensor(other, _builder) return semantic.and_(other, self, _builder) @builtin def __or__(self, other, _builder=None): other = _to_tensor(other, _builder) return semantic.or_(self, other, _builder) @builtin def __ror__(self, other, _builder=None): other = _to_tensor(other, _builder) return semantic.or_(other, self, _builder) @builtin def __xor__(self, other, _builder=None): other = _to_tensor(other, _builder) return semantic.xor_(self, other, _builder) @builtin def __rxor__(self, other, _builder=None): other = _to_tensor(other, _builder) return semantic.xor_(other, self, _builder) @builtin def __lshift__(self, other, _builder=None): other = _to_tensor(other, _builder) return semantic.shl(self, other, _builder) @builtin def __rlshift__(self, other, _builder=None): other = _to_tensor(other, _builder) return semantic.shl(other, self, _builder) @builtin def __rshift__(self, other, _builder=None): other = _to_tensor(other, _builder) if self.dtype.is_int_signed(): return semantic.ashr(self, other, _builder) else: return semantic.lshr(self, other, _builder) @builtin def __rrshift__(self, other, _builder=None): other = _to_tensor(other, _builder) if self.dtype.is_int_signed(): return semantic.ashr(other, self, _builder) else: return semantic.lshr(other, self, _builder) # comparison operators # > @builtin def __gt__(self, other, _builder=None): other = _to_tensor(other, _builder) return semantic.greater_than(self, other, _builder) @builtin def __rgt__(self, other, _builder=None): other = _to_tensor(other, _builder) return semantic.greater_than(other, self, _builder) # >= @builtin def __ge__(self, other, _builder=None): other = _to_tensor(other, _builder) return semantic.greater_equal(self, other, _builder) @builtin def __rge__(self, other, _builder=None): other = _to_tensor(other, _builder) return semantic.greater_equal(other, self, _builder) # < @builtin def __lt__(self, other, _builder=None): other = _to_tensor(other, _builder) return semantic.less_than(self, other, _builder) @builtin def __rlt__(self, other, _builder=None): other = _to_tensor(other, _builder) return semantic.less_than(other, self, _builder) # <= @builtin def __le__(self, other, _builder=None): other = _to_tensor(other, _builder) return semantic.less_equal(self, other, _builder) @builtin def __rle__(self, other, _builder=None): other = _to_tensor(other, _builder) return semantic.less_equal(other, self, _builder) # == @builtin def __eq__(self, other, _builder=None): other = _to_tensor(other, _builder) return semantic.equal(self, other, _builder) @builtin def __ne__(self, other, _builder=None): other = _to_tensor(other, _builder) return semantic.not_equal(self, other, _builder) @builtin def logical_and(self, other, _builder=None): other = _to_tensor(other, _builder) return semantic.logical_and(self, other, _builder) @builtin def logical_or(self, other, _builder=None): other = _to_tensor(other, _builder) return semantic.logical_or(self, other, _builder) # note: __not__ isn't actually a magic method in python # but it's ok because our ASTVisitor handles it @builtin def __not__(self, _builder=None): return semantic.not_(self, _builder) @builtin def __getitem__(self, slices, _builder=None): if isinstance(slices, slice): slices = [slices] ret = self for dim, sl in enumerate(slices): if isinstance(sl, constexpr) and sl.value is None: ret = semantic.expand_dims(ret, dim, _builder) elif isinstance(sl, slice) and sl.start is None and sl.stop is None and sl.step is None: pass else: assert False, f"unsupported tensor index: {sl}" return ret @property def T(self): assert False, "Transposition must be created by the AST Visitor" @builtin def to(self, dtype, bitcast=False, _builder=None): if isinstance(bitcast, constexpr): bitcast = bitcast.value if bitcast: return semantic.bitcast(self, dtype, _builder) return semantic.cast(self, dtype, _builder) # ----------------------- # SPMD Programming Model # ----------------------- def _constexpr_to_value(v): if isinstance(v, constexpr): return v.value return v @builtin def program_id(axis, _builder=None): """ Returns the id of the current program instance along the given :code:`axis`. :param axis: The axis of the 3D launch grid. Has to be either 0, 1 or 2. :type axis: int """ # if axis == -1: # pid0 = program_id(0, _builder) # pid1 = program_id(1, _builder) # pid2 = program_id(2, _builder) # npg0 = num_programs(0, _builder) # npg1 = num_programs(0, _builder) # return pid0 + pid1*npg0 + pid2*npg0*npg1 axis = _constexpr_to_value(axis) return semantic.program_id(axis, _builder) @builtin def num_programs(axis, _builder=None): """ Returns the number of program instances launched along the given :code:`axis`. :param axis: The axis of the 3D launch grid. Has to be either 0, 1 or 2. :type axis: int """ axis = _constexpr_to_value(axis) return semantic.num_programs(axis, _builder) # ----------------------- # Block Initialization # ----------------------- @builtin def arange(start, end, _builder=None): """ Returns contiguous values within the left-closed and right-open interval [:code:`start`, :code:`end`). \ End - Start must be less than or equal to TRITON_MAX_TENSOR_NUMEL = 131072 :param start: Start of the interval. Must be a power of two. :type start: int32 :param end: End of the interval. Must be a power of two > start. :type end: int32 """ start = _constexpr_to_value(start) end = _constexpr_to_value(end) return semantic.arange(start, end, _builder) def _shape_check_impl(shape): shape = _constexpr_to_value(shape) for i, d in enumerate(shape): if not isinstance(d, constexpr): raise TypeError(f"Shape element {i} must have type `constexpr`") if not isinstance(d.value, int): raise TypeError(f"Shape element {i} must have type `constexpr[int]`, got `constexpr[{type(d.value)}]") return [_constexpr_to_value(x) for x in shape] @builtin def full(shape, value, dtype, _builder=None): """ Returns a tensor filled with the scalar value for the given :code:`shape` and :code:`dtype`. :param shape: Shape of the new array, e.g., (8, 16) or (8, ) :value value: A scalar value to fill the array with :type shape: tuple of ints :param dtype: Data-type of the new array, e.g., :code:`tl.float16` :type dtype: DType """ shape = _shape_check_impl(shape) value = _constexpr_to_value(value) dtype = _constexpr_to_value(dtype) return semantic.full(shape, value, dtype, _builder) # ----------------------- # Shape Manipulation # ----------------------- @builtin def broadcast(input, other, _builder=None): """ Tries to broadcast the two given blocks to a common compatible shape. :param input: The first input tensor. :type input: Block :param other: The second input tensor. :type other: Block """ return semantic.broadcast_impl_value(input, other, _builder) @builtin def broadcast_to(input, shape, _builder=None): """ Tries to broadcast the given tensor to a new :code:`shape`. :param input: The input tensor. :type input: Block :param shape: The desired shape. :type shape: Tuple[int] """ shape = _shape_check_impl(shape) return semantic.broadcast_impl_shape(input, shape, _builder) @builtin def trans(input, _builder=None): return semantic.trans(input, _builder) @builtin def cat(input, other, can_reorder=False, _builder=None): """ Concatenate the given blocks :param input: The first input tensor. :type input: :param other: The second input tensor. :type other: :param reorder: Compiler hint. If true, the compiler is allowed to reorder elements while concatenating inputs. Only use if the order does not matter (e.g., result is only used in reduction ops) """ return semantic.cat(input, other, can_reorder, _builder) @builtin def view(input, shape, _builder=None): """ Returns a tensor with the same elements as `input` but a different shape. The order of the elements may not be preserved. :param input: The input tensor. :type input: :param shape: The desired shape. :type shape: Tuple[int] """ shape = _shape_check_impl(shape) return semantic.view(input, shape, _builder) @builtin def reshape(input, shape, _builder=None): shape = _shape_check_impl(shape) return semantic.reshape(input, shape, _builder) def _wrap_axis(axis, ndim): if not (-ndim <= axis < ndim): raise ValueError(f"invalid axis {axis}. Expected {-ndim} <= axis < {ndim}") return axis if axis >= 0 else axis + ndim @builtin def expand_dims(input, axis, _builder=None): """ Expand the shape of a tensor, by inserting new length-1 dimensions. Axis indices are with respect to the resulting tensor, so ``result.shape[axis]`` will be 1 for each axis. :param input: The input tensor. :type input: tl.tensor :param axis: The indices to add new axes :type axis: int | Sequence[int] """ axis = _constexpr_to_value(axis) axes = list(axis) if isinstance(axis, Sequence) else [axis] new_ndim = len(input.shape) + len(axes) axes = [_wrap_axis(_constexpr_to_value(d), new_ndim) for d in axes] if len(set(axes)) != len(axes): raise ValueError(f"expand_dims recieved duplicate axes, normalized axes = {axes}") ret = input for a in sorted(axes): ret = semantic.expand_dims(ret, a, _builder) return ret # ----------------------- # Linear Algebra # ----------------------- @builtin def dot(input, other, allow_tf32=True, out_dtype=float32, _builder=None): """ Returns the matrix product of two blocks. The two blocks must be two-dimensional and have compatible inner dimensions. :param input: The first tensor to be multiplied. :type input: 2D tensor of scalar-type in {:code:`float16`, :code:`bfloat16`, :code:`float32`} :param other: The second tensor to be multiplied. :type other: 2D tensor of scalar-type in {:code:`float16`, :code:`bfloat16`, :code:`float32`} """ allow_tf32 = _constexpr_to_value(allow_tf32) out_dtype = _constexpr_to_value(out_dtype) return semantic.dot(input, other, allow_tf32, out_dtype, _builder) # ----------------------- # Non-Atomic Memory Operations # ----------------------- @builtin def load(pointer, mask=None, other=None, boundary_check=tuple(), padding_option="", cache_modifier="", eviction_policy="", volatile=False, _builder=None): """ Return a tensor of data whose values are loaded from memory at location defined by `pointer`: (1) `pointer` could be a single element pointer, then a scalar will be loaded - `mask` and `other` must be scalar too - `other` is implicitly typecast to `pointer.dtype.element_ty` - `boundary_check` and `padding_option` must be empty (2) `pointer` could be element-wise tensor of pointers, in which case: - `mask` and `other` are implicitly broadcast to `pointer.shape` - `other` is implicitly typecast to `pointer.dtype.element_ty` - `boundary_check` and `padding_option` must be empty (3) `pointer` could be a block pointer defined by `make_block_ptr`, in which case: - `mask` and `other` must be None - `boundary_check` and `padding_option` can be specified to control the behavior of out-of-bound access :param pointer: Pointer to the data to be loaded :type pointer: `triton.PointerType`, or block of `dtype=triton.PointerType` :param mask: if `mask[idx]` is false, do not load the data at address `pointer[idx]` (must be `None` with block pointers) :type mask: Block of `triton.int1`, optional :param other: if `mask[idx]` is false, return `other[idx]` :type other: Block, optional :param boundary_check: tuple of integers, indicating the dimensions which should do the boundary check :type boundary_check: tuple of ints, optional :param padding_option: should be one of {"", "zero", "nan"}, do padding while out of bound :param cache_modifier: changes cache option in NVIDIA PTX :type cache_modifier: str, optional :param eviction_policy: changes eviction policy in NVIDIA PTX :type eviction_policy: str, optional :param volatile: changes volatile option in NVIDIA PTX :type volatile: bool, optional """ # `mask` and `other` can be constexpr if _constexpr_to_value(mask) is not None: mask = _to_tensor(mask, _builder) if _constexpr_to_value(other) is not None: other = _to_tensor(other, _builder) padding_option = _constexpr_to_value(padding_option) cache_modifier = _constexpr_to_value(cache_modifier) eviction_policy = _constexpr_to_value(eviction_policy) volatile = _constexpr_to_value(volatile) return semantic.load(pointer, mask, other, boundary_check, padding_option, cache_modifier, eviction_policy, volatile, _builder) @builtin def store(pointer, value, mask=None, boundary_check=(), cache_modifier="", eviction_policy="", _builder=None): """ Store a tensor of data into memory locations defined by `pointer`: (1) `pointer` could be a single element pointer, then a scalar will be stored - `mask` must be scalar too - `boundary_check` and `padding_option` must be empty (2) `pointer` could be element-wise tensor of pointers, in which case: - `mask` is implicitly broadcast to `pointer.shape` - `boundary_check` must be empty (3) or `pointer` could be a block pointer defined by `make_block_ptr`, in which case: - `mask` must be None - `boundary_check` can be specified to control the behavior of out-of-bound access `value` is implicitly broadcast to `pointer.shape` and typecast to `pointer.dtype.element_ty`. :param pointer: The memory location where the elements of `value` are stored :type pointer: `triton.PointerType`, or block of `dtype=triton.PointerType` :param value: The tensor of elements to be stored :type value: Block :param mask: If `mask[idx]` is false, do not store `value[idx]` at `pointer[idx]` :type mask: Block of triton.int1, optional :param boundary_check: tuple of integers, indicating the dimensions which should do the boundary check :type boundary_check: tuple of ints, optional :param cache_modifier: changes cache option in NVIDIA PTX :type cache_modifier: str, optional :param eviction_policy: changes eviction policy in NVIDIA PTX :type eviction_policy: str, optional """ # `value` can be constexpr value = _to_tensor(value, _builder) if _constexpr_to_value(mask) is not None: mask = _to_tensor(mask, _builder) cache_modifier = _constexpr_to_value(cache_modifier) eviction_policy = _constexpr_to_value(eviction_policy) return semantic.store(pointer, value, mask, boundary_check, cache_modifier, eviction_policy, _builder) @builtin def make_block_ptr(base: tensor, shape, strides, offsets, block_shape, order, _builder=None): """ Returns a pointer to a block in a parent tensor :param base: The base pointer to the parent tensor :param shape: The shape of the parent tensor :param strides: The strides of the parent tensor :param offsets: The offsets to the block :param block_shape: The shape of the block :param order: The order of the original data format """ return semantic.make_block_ptr(base, shape, strides, offsets, block_shape, order, _builder) @builtin def advance(base: tensor, offsets, _builder=None): """ Advance a block pointer :param base: the block pointer to advance :param offsets: the offsets to advance, a tuple by dimension """ return semantic.advance(base, offsets, _builder) # ----------------------- # Atomic Memory Operations # ----------------------- def _add_atomic_docstr(name: str) -> Callable[[T], T]: def _decorator(func: T) -> T: docstr = """ Performs an atomic {name} at the memory location specified by :code:`pointer`. Return the data stored at :code:`pointer` before the atomic operation. :param pointer: The memory locations to compare-and-swap. :type pointer: Block of dtype=triton.PointerDType :param cmp: The values expected to be found in the atomic object :type cmp: Block of dtype=`pointer.dtype.element_ty` :param val: The values to copy in case the expected value matches the contained value. :type val: Block of dtype=`pointer.dtype.element_ty` """ func.__doc__ = docstr.format(name=name) return func return _decorator @builtin @_add_atomic_docstr("compare-and-swap") def atomic_cas(pointer, cmp, val, sem=None, _builder=None): cmp = _to_tensor(cmp, _builder) val = _to_tensor(val, _builder) sem = _constexpr_to_value(sem) return semantic.atomic_cas(pointer, cmp, val, sem, _builder) @builtin @_add_atomic_docstr("exchange") def atomic_xchg(pointer, val, mask=None, sem=None, _builder=None): val = _to_tensor(val, _builder) sem = _constexpr_to_value(sem) return semantic.atomic_xchg(pointer, val, mask, sem, _builder) @builtin @_add_atomic_docstr("add") def atomic_add(pointer, val, mask=None, sem=None, _builder=None): val = _to_tensor(val, _builder) sem = _constexpr_to_value(sem) return semantic.atomic_add(pointer, val, mask, sem, _builder) @builtin @_add_atomic_docstr("max") def atomic_max(pointer, val, mask=None, sem=None, _builder=None): val = _to_tensor(val, _builder) sem = _constexpr_to_value(sem) return semantic.atomic_max(pointer, val, mask, sem, _builder) @builtin @_add_atomic_docstr("min") def atomic_min(pointer, val, mask=None, sem=None, _builder=None): val = _to_tensor(val, _builder) sem = _constexpr_to_value(sem) return semantic.atomic_min(pointer, val, mask, sem, _builder) @builtin @_add_atomic_docstr("logical and") def atomic_and(pointer, val, mask=None, sem=None, _builder=None): val = _to_tensor(val, _builder) sem = _constexpr_to_value(sem) return semantic.atomic_and(pointer, val, mask, sem, _builder) @builtin @_add_atomic_docstr("logical or") def atomic_or(pointer, val, mask=None, sem=None, _builder=None): val = _to_tensor(val, _builder) sem = _constexpr_to_value(sem) return semantic.atomic_or(pointer, val, mask, sem, _builder) @builtin @_add_atomic_docstr("logical xor") def atomic_xor(pointer, val, mask=None, sem=None, _builder=None): val = _to_tensor(val, _builder) sem = _constexpr_to_value(sem) return semantic.atomic_xor(pointer, val, mask, sem, _builder) # ----------------------- # Conditioning # ----------------------- @builtin def where(condition, x, y, _builder=None): """ Returns a tensor of elements from either :code:`x` or :code:`y`, depending on :code:`condition`. Note that :code:`x` and :code:`y` are always evaluated regardless of the value of :code:`condition`. If you want to avoid unintended memory operations, use the :code:`mask` arguments in `triton.load` and `triton.store` instead. The shape of :code:`x` and :code:`y` are both broadcast to the shape of :code:`condition`. :code:`x` and :code:`y` must have the same data type. :param condition: When True (nonzero), yield x, otherwise yield y. :type condition: Block of triton.bool :param x: values selected at indices where condition is True. :param y: values selected at indices where condition is False. """ condition = _to_tensor(condition, _builder) x = _to_tensor(x, _builder) y = _to_tensor(y, _builder) return semantic.where(condition, x, y, _builder) # ----------------------- # Math # ----------------------- @builtin def umulhi(x, y, _builder=None): x = _to_tensor(x, _builder) y = _to_tensor(y, _builder) return semantic.umulhi(x, y, _builder) @builtin def fdiv(x, y, ieee_rounding=False, _builder=None): ieee_rounding = _constexpr_to_value(ieee_rounding) return semantic.fdiv(x, y, ieee_rounding, _builder) def _add_math_1arg_docstr(name: str) -> Callable[[T], T]: def _decorator(func: T) -> T: docstr = """ Computes the element-wise {name} of :code:`x`. :param x: the input values :type x: Block """ func.__doc__ = docstr.format(name=name) return func return _decorator @builtin @_add_math_1arg_docstr("exponential") def exp(x, _builder=None): return semantic.exp(x, _builder) @builtin @_add_math_1arg_docstr("natural logarithm") def log(x, _builder=None): return semantic.log(x, _builder) @builtin @_add_math_1arg_docstr("cosine") def cos(x, _builder=None): return semantic.cos(x, _builder) @builtin @_add_math_1arg_docstr("sine") def sin(x, _builder=None): return semantic.sin(x, _builder) @builtin @_add_math_1arg_docstr("square root") def sqrt(x, _builder=None): return semantic.sqrt(x, _builder) @builtin @_add_math_1arg_docstr("absolute value") def abs(x, _builder=None): return semantic.abs(x, _builder) # ----------------------- # Reductions # ----------------------- def _add_reduction_docstr(name: str, return_indices_arg: str = None, tie_break_arg: str = None) -> Callable[[T], T]: def _decorator(func: T) -> T: docstr = """ Returns the {name} of all elements in the :code:`input` tensor along the provided :code:`axis` :param input: the input values :param axis: the dimension along which the reduction should be done""" if return_indices_arg is not None: docstr += f""" :param {return_indices_arg}: if true, return index corresponding to the {name} value""" if tie_break_arg is not None: docstr += f""" :param {tie_break_arg}: if true, return the left-most indices in case of ties for values that aren't NaN""" func.__doc__ = docstr.format(name=name) return func return _decorator @contextmanager def _insertion_guard(builder): ip = builder.get_insertion_point() yield builder.restore_insertion_point(ip) @builtin def reduce(input, axis, combine_fn, _builder=None, _generator=None): """Applies the combine_fn to all elements in :code:`input` tensors along the provided :code:`axis` :param input: the input tensor, or tuple of tensors :param axis: the dimension along which the reduction should be done :param combine_fn: a function to combine two groups of scalar tensors (must be marked with @triton.jit) """ if isinstance(input, tensor): return reduce((input,), axis, combine_fn, _builder=_builder, _generator=_generator)[0] def make_combine_region(reduce_op): in_scalar_tys = [t.type.scalar for t in input] prototype = function_type(in_scalar_tys, in_scalar_tys * 2) region = reduce_op.get_region(0) with _insertion_guard(_builder): param_types = [ty.to_ir(_builder) for ty in prototype.param_types] block = _builder.create_block_with_parent(region, param_types) args = [tensor(block.arg(i), ty) for i, ty in enumerate(prototype.param_types)] results = _generator.call_JitFunction(combine_fn, args, kwargs={}) if isinstance(results, tensor): handles = [results.handle] else: handles = [r.handle for r in results] _builder.create_reduce_ret(*handles) if axis is not None: axis = _constexpr_to_value(axis) return semantic.reduction(input, axis, make_combine_region, _builder) @builtin def _promote_reduction_input(t, _builder=None): scalar_ty = t.type.scalar # input is extended to 32-bits if necessary # this increases numerical accuracy and can be done pretty much for free # on GPUs if scalar_ty.is_int() and scalar_ty.int_bitwidth < 32: return t.to(int32, _builder=_builder) # hardware doesn't support FMAX, FMIN, CMP for bfloat16 if scalar_ty is bfloat16: return t.to(float32, _builder=_builder) return t @builtin def _reduce_with_indices(input, axis, combine_fn, _builder=None, _generator=None): axis = _constexpr_to_value(axis) n = input.shape[axis] index = arange(0, n, _builder=_builder) if len(input.shape) > 1: # Broadcast index across the non-reduced axes axes_to_expand = [constexpr(d) for d in range(len(input.shape))] del axes_to_expand[axis] index = expand_dims(index, axes_to_expand, _builder=_builder) index = broadcast_to(index, input.shape, _builder=_builder) rvalue, rindices = reduce((input, index), axis, combine_fn, _builder=_builder, _generator=_generator) return rvalue, rindices @jit def minimum(x, y): """ Computes the element-wise minimum of :code:`x` and :code:`y`. :param input: the first input tensor :type input: Block :param other: the second input tensor :type other: Block """ return where(x < y, x, y) @jit def maximum(x, y): """ Computes the element-wise maximum of :code:`x` and :code:`y`. :param input: the first input tensor :type input: Block :param other: the second input tensor :type other: Block """ return where(x > y, x, y) # max and argmax @jit def _argmax_combine(value1, index1, value2, index2, tie_break_left): if tie_break_left: tie = value1 == value2 and index1 < index2 else: tie = False gt = value1 > value2 or tie v_ret = where(gt, value1, value2) i_ret = where(gt, index1, index2) return v_ret, i_ret @jit def _argmax_combine_tie_break_left(value1, index1, value2, index2): return _argmax_combine(value1, index1, value2, index2, True) @jit def _argmax_combine_tie_break_fast(value1, index1, value2, index2): return _argmax_combine(value1, index1, value2, index2, False) @jit def _fast_max(x, y): return math.max(x, y) @jit @_add_reduction_docstr("maximum", return_indices_arg="return_indices", tie_break_arg="return_indices_tie_break_left") def max(input, axis=None, return_indices=False, return_indices_tie_break_left=True): input = _promote_reduction_input(input) if return_indices: if return_indices_tie_break_left: return _reduce_with_indices(input, axis, _argmax_combine_tie_break_left) else: return _reduce_with_indices(input, axis, _argmax_combine_tie_break_fast) else: if constexpr(input.dtype.primitive_bitwidth) < 32: if constexpr(input.dtype.is_floating()): input = input.to(float32) else: assert input.dtype.is_integer_type() input = input.to(int32) return reduce(input, axis, _fast_max) @jit @_add_reduction_docstr("maximum index", tie_break_arg="tie_break_left") def argmax(input, axis, tie_break_left=True): (_, ret) = max(input, axis, return_indices=True, return_indices_tie_break_left=tie_break_left) return ret # min and argmin @jit def _argmin_combine(value1, index1, value2, index2, tie_break_left): if tie_break_left: tie = value1 == value2 and index1 < index2 else: tie = False lt = value1 < value2 or tie value_ret = where(lt, value1, value2) index_ret = where(lt, index1, index2) return value_ret, index_ret @jit def _argmin_combine_tie_break_left(value1, index1, value2, index2): return _argmin_combine(value1, index1, value2, index2, True) @jit def _argmin_combine_tie_break_fast(value1, index1, value2, index2): return _argmin_combine(value1, index1, value2, index2, False) @jit def _fast_min(x, y): return math.min(x, y) @jit @_add_reduction_docstr("minimum", return_indices_arg="return_indices", tie_break_arg="return_indices_tie_break_left") def min(input, axis=None, return_indices=False, return_indices_tie_break_left=True): input = _promote_reduction_input(input) if return_indices: if return_indices_tie_break_left: return _reduce_with_indices(input, axis, _argmin_combine_tie_break_left) else: return _reduce_with_indices(input, axis, _argmin_combine_tie_break_fast) else: if constexpr(input.dtype.primitive_bitwidth) < 32: if constexpr(input.dtype.is_floating()): input = input.to(float32) else: assert input.dtype.is_integer_type() input = input.to(int32) return reduce(input, axis, _fast_min) @jit @_add_reduction_docstr("minimum index", tie_break_arg="tie_break_left") def argmin(input, axis, tie_break_left=True): _, ret = min(input, axis, return_indices=True, return_indices_tie_break_left=tie_break_left) return ret @jit def _sum_combine(a, b): return a + b # sum @jit @_add_reduction_docstr("sum") def sum(input, axis=None): input = _promote_reduction_input(input) return reduce(input, axis, _sum_combine) @jit def _xor_combine(a, b): return a ^ b # xor sum @builtin @_add_reduction_docstr("xor sum") def xor_sum(input, axis=None, _builder=None, _generator=None): scalar_ty = input.type.scalar if not scalar_ty.is_int(): raise ValueError("xor_sum only supported for integers") input = _promote_reduction_input(input, _builder=_builder) return reduce(input, axis, _xor_combine, _builder=_builder, _generator=_generator) # ----------------------- # Scans # ----------------------- def _add_scan_docstr(name: str, return_indices_arg: str = None, tie_break_arg: str = None) -> Callable[[T], T]: def _decorator(func: T) -> T: docstr = """ Returns the {name} of all elements in the :code:`input` tensor along the provided :code:`axis` :param input: the input values :param axis: the dimension along which the scan should be done""" func.__doc__ = docstr.format(name=name) return func return _decorator @builtin def associative_scan(input, axis, combine_fn, _builder=None, _generator=None): """Applies the combine_fn to each elements with a carry in :code:`input` tensors along the provided :code:`axis` and update the carry :param input: the input tensor, or tuple of tensors :param axis: the dimension along which the reduction should be done :param combine_fn: a function to combine two groups of scalar tensors (must be marked with @triton.jit) """ if isinstance(input, tensor): return associative_scan((input,), axis, combine_fn, _builder=_builder, _generator=_generator)[0] def make_combine_region(scan_op): in_scalar_tys = [t.type.scalar for t in input] prototype = function_type(in_scalar_tys, in_scalar_tys * 2) region = scan_op.get_region(0) with _insertion_guard(_builder): param_types = [ty.to_ir(_builder) for ty in prototype.param_types] block = _builder.create_block_with_parent(region, param_types) args = [tensor(block.arg(i), ty) for i, ty in enumerate(prototype.param_types)] results = _generator.call_JitFunction(combine_fn, args, kwargs={}) if isinstance(results, tensor): handles = [results.handle] else: handles = [r.handle for r in results] _builder.create_scan_ret(*handles) axis = _constexpr_to_value(axis) return semantic.associative_scan(input, axis, make_combine_region, _builder) # cumsum @jit @_add_scan_docstr("cumsum") def cumsum(input, axis=0): # todo rename this to a generic function name input = _promote_reduction_input(input) return associative_scan(input, axis, _sum_combine) # cumprod @jit def _prod_combine(a, b): return a * b @jit @_add_scan_docstr("cumprod") def cumprod(input, axis=0): # todo rename this to a generic function name input = _promote_reduction_input(input) return associative_scan(input, axis, _prod_combine) # ----------------------- # Compiler Hint Ops # ----------------------- @builtin def debug_barrier(_builder=None): ''' Insert a barrier to synchronize all threads in a block. ''' return semantic.debug_barrier(_builder) @builtin def multiple_of(input, values, _builder=None): """ Let the compiler knows that the values in :code:`input` are all multiples of :code:`value`. """ if isinstance(values, constexpr): values = [values] for i, d in enumerate(values): if not isinstance(d, constexpr): raise TypeError(f"values element {i} must have type `constexpr`") if not isinstance(d.value, int): raise TypeError(f"values element {i} must have type `constexpr[int]`, got `constexpr[{type(d.value)}]") values = [x.value for x in values] return semantic.multiple_of(input, values) @builtin def max_contiguous(input, values, _builder=None): """ Let the compiler knows that the `value` first values in :code:`input` are contiguous. """ if isinstance(values, constexpr): values = [values] for i, d in enumerate(values): if not isinstance(d, constexpr): raise TypeError(f"values element {i} must have type `constexpr`") if not isinstance(d.value, int): raise TypeError(f"values element {i} must have type `constexpr[int]`, got `constexpr[{type(d.value)}]") values = [x.value for x in values] return semantic.max_contiguous(input, values) @builtin def max_constancy(input, values, _builder=None): """ Let the compiler knows that the `value` first values in :code:`input` are constant. e.g. if :code:`values` is [4], then each group of 4 values in :code:`input` should all be equal, for example [0, 0, 0, 0, 1, 1, 1, 1]. """ if isinstance(values, constexpr): values = [values] for i, d in enumerate(values): if not isinstance(d, constexpr): raise TypeError(f"values element {i} must have type `constexpr`") if not isinstance(d.value, int): raise TypeError(f"values element {i} must have type `constexpr[int]`, got `constexpr[{type(d.value)}]") values = [x.value for x in values] return semantic.max_constancy(input, values) # ----------------------- # Debugging functions # ----------------------- @builtin def static_print(*values, sep: str = " ", end: str = "\n", file=None, flush=False, _builder=None): ''' Print the values at compile time. The parameters are the same as the builtin :code:`print`. NOTE: Calling the Python builtin :code:`print` is not the same as calling this, it instead maps to :code:`device_print`, which has special requirements for the arguments. .. highlight:: python .. code-block:: python tl.static_print(f"{BLOCK_SIZE=}") ''' pass @builtin def static_assert(cond, msg="", _builder=None): ''' Assert the condition at compile time. Does not require that the :code:`TRITON_DEBUG` environment variable is set. .. highlight:: python .. code-block:: python tl.static_assert(BLOCK_SIZE == 1024) ''' pass @builtin def device_print(prefix, *args, _builder=None): ''' Print the values at runtime from the device. String formatting does not work for runtime values, so you should provide the values you want to print as arguments. The first value must be a string, all following values must be scalars or tensors. Calling the Python builtin :code:`print` is the same as calling this function, and the requirements for the arguments will match this function (not the normal requirements for :code:`print`). .. highlight:: python .. code-block:: python tl.device_print("pid", pid) print("pid", pid) :param prefix: a prefix to print before the values. This is required to be a string literal. :param args: the values to print. They can be any tensor or scalar. ''' import string prefix = _constexpr_to_value(prefix) assert isinstance(prefix, str), f"{prefix} is not string" b_ascii = True for ch in prefix: if ch not in string.printable: b_ascii = False break assert b_ascii, f"{prefix} is not an ascii string" new_args = [] for arg in args: new_args.append(_to_tensor(arg, _builder)) return semantic.device_print(prefix, new_args, _builder) @builtin def device_assert(cond, msg="", _builder=None): ''' Assert the condition at runtime from the device. Requires that the environment variable :code:`TRITON_DEBUG` is set to a value besides :code:`0` in order for this to have any effect. Using the Python :code:`assert` statement is the same as calling this function, except that the second argument must be provided and must be a string, e.g. :code:`assert pid == 0, "pid != 0"`. The environment variable must be set for this :code:`assert` statement to have any effect. .. highlight:: python .. code-block:: python tl.device_assert(pid == 0) assert pid == 0, f"pid != 0" :param cond: the condition to assert. This is required to be a boolean tensor. :param msg: the message to print if the assertion fails. This is required to be a string literal. ''' msg = _constexpr_to_value(msg) import inspect frame = inspect.currentframe() module = inspect.getmodule(frame) # The triton function module doesn't have the name attribute. # We use this trick to find the caller. while hasattr(module, "__name__"): frame = frame.f_back module = inspect.getmodule(frame) lineno = 0 func_name = 'unknown' file_name = 'unknown' if frame is not None: func_name = frame.f_code.co_name file_name = frame.f_back.f_code.co_filename # TODO: The line number currently indicates the line # where the triton function is called but not where the # device_assert is called. Need to enhance this. lineno = frame.f_back.f_lineno return semantic.device_assert(_to_tensor(cond, _builder), msg, file_name, func_name, lineno, _builder) # ----------------------- # Iterators # ----------------------- class static_range: """ Iterator that counts upward forever. .. highlight:: python .. code-block:: python @triton.jit def kernel(...): for i in tl.static_range(10): ... :note: This is a special iterator used to implement similar semantics to Python's :code:`range` in the context of :code:`triton.jit` functions. In addition, it also guides the compiler to unroll the loop aggressively. :param arg1: the start value. :param arg2: the end value. :param step: the step value. """ def __init__(self, arg1, arg2=None, step=None): assert isinstance(arg1, constexpr) if step is None: self.step = constexpr(1) else: assert isinstance(step, constexpr) self.step = step if arg2 is None: self.start = constexpr(0) self.end = arg1 else: assert isinstance(arg2, constexpr) self.start = arg1 self.end = arg2 def __iter__(self): raise RuntimeError("static_range can only be used in @triton.jit'd functions") def __next__(self): raise RuntimeError("static_range can only be used in @triton.jit'd functions") # ----------------------- # Extern functions # ----------------------- def dispatch(func, lib_name: str, lib_path: str, args: list, arg_type_symbol_dict: dict, ret_shape: tuple, is_pure: bool, _builder=None): ''' Dispatch a function to a library :param func: the function to dispatch :param lib_name: the name of the library :param lib_path: the path of the library :param args: the arguments of the function :param arg_type_symbol_dict: the type of the arguments :param ret_shape: the shape of the return value :param _builder: the builder :return: the return value of the function ''' if len(arg_type_symbol_dict) == 0: raise ValueError("arg_type_symbol_dict is empty") num_args = len(list(arg_type_symbol_dict.keys())[0]) if len(args) != num_args: raise ValueError(f"length of input args does not match." f"Expect {len(args)}, got {num_args}") arg_types = [] arg_list = [] for arg in args: if isinstance(arg, tensor): arg_types.append(arg.dtype) arg_list.append(arg.handle) else: arg_types.append(type(arg)) arg_list.append(arg) arg_types = tuple(arg_types) if arg_types not in arg_type_symbol_dict: raise ValueError(f"input arg type does not match." f"Expect one of {arg_type_symbol_dict.keys()}, got {arg_types}") else: symbol = arg_type_symbol_dict[arg_types][0] ret_type = arg_type_symbol_dict[arg_types][1] if ret_shape: ret_type = block_type(ret_type, ret_shape) return tensor(func(lib_name, lib_path, symbol, arg_list, ret_type.to_ir(_builder), is_pure), ret_type) def extern_elementwise(lib_name: str, lib_path: str, args: list, arg_type_symbol_dict: dict, is_pure: bool, _builder=None): ''' Dispatch an elementwise function to a library :param lib_name: the name of the library :param lib_path: the path of the library :param args: the arguments of the function :param arg_type_symbol_dict: the type of the arguments :param is_pure: whether the function is pure :param _builder: the builder :return: the return value of the function ''' dispatch_args = args.copy() all_scalar = True ret_shape = None arg_types = [] for i in range(len(dispatch_args)): dispatch_args[i] = _to_tensor(dispatch_args[i], _builder) arg_types.append(dispatch_args[i].dtype) if dispatch_args[i].type.is_block(): all_scalar = False if len(arg_types) > 0: arg_types = tuple(arg_types) arithmetic_check = True # If there's a type tuple that is not supported by the library, we will do arithmetic check if arg_types in arg_type_symbol_dict: arithmetic_check = False broadcast_arg = dispatch_args[0] # Get the broadcast shape over all the arguments for i, item in enumerate(dispatch_args): _, broadcast_arg = semantic.binary_op_type_checking_impl( item, broadcast_arg, _builder, arithmetic_check=arithmetic_check) # Change the shape of each argument based on the broadcast shape for i in range(len(dispatch_args)): dispatch_args[i], _ = semantic.binary_op_type_checking_impl( dispatch_args[i], broadcast_arg, _builder, arithmetic_check=arithmetic_check) if not all_scalar: ret_shape = broadcast_arg.shape func = getattr(_builder, "create_extern_elementwise") return dispatch(func, lib_name, lib_path, dispatch_args, arg_type_symbol_dict, ret_shape, is_pure, _builder) def extern(fn): """A decorator for external functions.""" return builtin(fn)
62,163
31.175983
137
py
triton
triton-main/python/triton/language/math.py
import functools import os from . import core @functools.lru_cache() def libdevice_path(): import torch third_party_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "third_party") if torch.version.hip is None: default = os.path.join(third_party_dir, "cuda", "lib", "libdevice.10.bc") else: default = '' return os.getenv("TRITON_LIBDEVICE_PATH", default) @core.extern def clz(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("int32"),): ("__nv_clz", core.dtype("int32")), (core.dtype("int64"),): ("__nv_clzll", core.dtype("int32")), }, is_pure=True, _builder=_builder) @core.extern def popc(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("int32"),): ("__nv_popc", core.dtype("int32")), (core.dtype("int64"),): ("__nv_popcll", core.dtype("int32")), }, is_pure=True, _builder=_builder) @core.extern def byte_perm(arg0, arg1, arg2, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, arg1, arg2, ], {(core.dtype("int32"), core.dtype("int32"), core.dtype("int32"),): ("__nv_byte_perm", core.dtype("int32")), }, is_pure=True, _builder=_builder) @core.extern def min(arg0, arg1, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, arg1, ], {(core.dtype("int32"), core.dtype("int32"),): ("__nv_min", core.dtype("int32")), (core.dtype("uint32"), core.dtype("uint32"),): ("__nv_umin", core.dtype("uint32")), (core.dtype("int64"), core.dtype("int64"),): ("__nv_llmin", core.dtype("int64")), (core.dtype("uint64"), core.dtype("uint64"),): ("__nv_ullmin", core.dtype("uint64")), (core.dtype("fp32"), core.dtype("fp32"),): ("__nv_fminf", core.dtype("fp32")), (core.dtype("fp64"), core.dtype("fp64"),): ("__nv_fmin", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def max(arg0, arg1, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, arg1, ], {(core.dtype("int32"), core.dtype("int32"),): ("__nv_max", core.dtype("int32")), (core.dtype("uint32"), core.dtype("uint32"),): ("__nv_umax", core.dtype("uint32")), (core.dtype("int64"), core.dtype("int64"),): ("__nv_llmax", core.dtype("int64")), (core.dtype("uint64"), core.dtype("uint64"),): ("__nv_ullmax", core.dtype("uint64")), (core.dtype("fp32"), core.dtype("fp32"),): ("__nv_fmaxf", core.dtype("fp32")), (core.dtype("fp64"), core.dtype("fp64"),): ("__nv_fmax", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def mulhi(arg0, arg1, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, arg1, ], {(core.dtype("int32"), core.dtype("int32"),): ("__nv_mulhi", core.dtype("int32")), (core.dtype("uint32"), core.dtype("uint32"),): ("__nv_umulhi", core.dtype("uint32")), (core.dtype("int64"), core.dtype("int64"),): ("__nv_mul64hi", core.dtype("int64")), (core.dtype("uint64"), core.dtype("uint64"),): ("__nv_umul64hi", core.dtype("uint64")), }, is_pure=True, _builder=_builder) @core.extern def mul24(arg0, arg1, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, arg1, ], {(core.dtype("int32"), core.dtype("int32"),): ("__nv_mul24", core.dtype("int32")), (core.dtype("uint32"), core.dtype("uint32"),): ("__nv_umul24", core.dtype("uint32")), }, is_pure=True, _builder=_builder) @core.extern def brev(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("int32"),): ("__nv_brev", core.dtype("int32")), (core.dtype("int64"),): ("__nv_brevll", core.dtype("int64")), }, is_pure=True, _builder=_builder) @core.extern def sad(arg0, arg1, arg2, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, arg1, arg2, ], {(core.dtype("int32"), core.dtype("int32"), core.dtype("uint32"),): ("__nv_sad", core.dtype("int32")), (core.dtype("uint32"), core.dtype("uint32"), core.dtype("uint32"),): ("__nv_usad", core.dtype("uint32")), }, is_pure=True, _builder=_builder) @core.extern def abs(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("int32"),): ("__nv_abs", core.dtype("int32")), (core.dtype("int64"),): ("__nv_llabs", core.dtype("int64")), (core.dtype("fp32"),): ("__nv_fabsf", core.dtype("fp32")), (core.dtype("fp64"),): ("__nv_fabs", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def floor(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_floorf", core.dtype("fp32")), (core.dtype("fp64"),): ("__nv_floor", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def rcp64h(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp64"),): ("__nv_rcp64h", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def rsqrt(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_rsqrtf", core.dtype("fp32")), (core.dtype("fp64"),): ("__nv_rsqrt", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def ceil(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp64"),): ("__nv_ceil", core.dtype("fp64")), (core.dtype("fp32"),): ("__nv_ceilf", core.dtype("fp32")), }, is_pure=True, _builder=_builder) @core.extern def trunc(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp64"),): ("__nv_trunc", core.dtype("fp64")), (core.dtype("fp32"),): ("__nv_truncf", core.dtype("fp32")), }, is_pure=True, _builder=_builder) @core.extern def exp2(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_exp2f", core.dtype("fp32")), (core.dtype("fp64"),): ("__nv_exp2", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def saturatef(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_saturatef", core.dtype("fp32")), }, is_pure=True, _builder=_builder) @core.extern def fma_rn(arg0, arg1, arg2, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, arg1, arg2, ], {(core.dtype("fp32"), core.dtype("fp32"), core.dtype("fp32"),): ("__nv_fmaf_rn", core.dtype("fp32")), (core.dtype("fp64"), core.dtype("fp64"), core.dtype("fp64"),): ("__nv_fma_rn", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def fma_rz(arg0, arg1, arg2, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, arg1, arg2, ], {(core.dtype("fp32"), core.dtype("fp32"), core.dtype("fp32"),): ("__nv_fmaf_rz", core.dtype("fp32")), (core.dtype("fp64"), core.dtype("fp64"), core.dtype("fp64"),): ("__nv_fma_rz", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def fma_rd(arg0, arg1, arg2, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, arg1, arg2, ], {(core.dtype("fp32"), core.dtype("fp32"), core.dtype("fp32"),): ("__nv_fmaf_rd", core.dtype("fp32")), (core.dtype("fp64"), core.dtype("fp64"), core.dtype("fp64"),): ("__nv_fma_rd", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def fma_ru(arg0, arg1, arg2, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, arg1, arg2, ], {(core.dtype("fp32"), core.dtype("fp32"), core.dtype("fp32"),): ("__nv_fmaf_ru", core.dtype("fp32")), (core.dtype("fp64"), core.dtype("fp64"), core.dtype("fp64"),): ("__nv_fma_ru", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def fast_dividef(arg0, arg1, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, arg1, ], {(core.dtype("fp32"), core.dtype("fp32"),): ("__nv_fast_fdividef", core.dtype("fp32")), }, is_pure=True, _builder=_builder) @core.extern def div_rn(arg0, arg1, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, arg1, ], {(core.dtype("fp32"), core.dtype("fp32"),): ("__nv_fdiv_rn", core.dtype("fp32")), (core.dtype("fp64"), core.dtype("fp64"),): ("__nv_ddiv_rn", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def div_rz(arg0, arg1, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, arg1, ], {(core.dtype("fp32"), core.dtype("fp32"),): ("__nv_fdiv_rz", core.dtype("fp32")), (core.dtype("fp64"), core.dtype("fp64"),): ("__nv_ddiv_rz", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def div_rd(arg0, arg1, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, arg1, ], {(core.dtype("fp32"), core.dtype("fp32"),): ("__nv_fdiv_rd", core.dtype("fp32")), (core.dtype("fp64"), core.dtype("fp64"),): ("__nv_ddiv_rd", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def div_ru(arg0, arg1, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, arg1, ], {(core.dtype("fp32"), core.dtype("fp32"),): ("__nv_fdiv_ru", core.dtype("fp32")), (core.dtype("fp64"), core.dtype("fp64"),): ("__nv_ddiv_ru", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def rcp_rn(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_frcp_rn", core.dtype("fp32")), (core.dtype("fp64"),): ("__nv_drcp_rn", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def rcp_rz(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_frcp_rz", core.dtype("fp32")), (core.dtype("fp64"),): ("__nv_drcp_rz", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def rcp_rd(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_frcp_rd", core.dtype("fp32")), (core.dtype("fp64"),): ("__nv_drcp_rd", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def rcp_ru(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_frcp_ru", core.dtype("fp32")), (core.dtype("fp64"),): ("__nv_drcp_ru", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def sqrt_rn(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_fsqrt_rn", core.dtype("fp32")), (core.dtype("fp64"),): ("__nv_dsqrt_rn", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def sqrt_rz(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_fsqrt_rz", core.dtype("fp32")), (core.dtype("fp64"),): ("__nv_dsqrt_rz", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def sqrt_rd(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_fsqrt_rd", core.dtype("fp32")), (core.dtype("fp64"),): ("__nv_dsqrt_rd", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def sqrt_ru(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_fsqrt_ru", core.dtype("fp32")), (core.dtype("fp64"),): ("__nv_dsqrt_ru", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def sqrt(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_sqrtf", core.dtype("fp32")), (core.dtype("fp64"),): ("__nv_sqrt", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def add_rn(arg0, arg1, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, arg1, ], {(core.dtype("fp64"), core.dtype("fp64"),): ("__nv_dadd_rn", core.dtype("fp64")), (core.dtype("fp32"), core.dtype("fp32"),): ("__nv_fadd_rn", core.dtype("fp32")), }, is_pure=True, _builder=_builder) @core.extern def add_rz(arg0, arg1, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, arg1, ], {(core.dtype("fp64"), core.dtype("fp64"),): ("__nv_dadd_rz", core.dtype("fp64")), (core.dtype("fp32"), core.dtype("fp32"),): ("__nv_fadd_rz", core.dtype("fp32")), }, is_pure=True, _builder=_builder) @core.extern def add_rd(arg0, arg1, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, arg1, ], {(core.dtype("fp64"), core.dtype("fp64"),): ("__nv_dadd_rd", core.dtype("fp64")), (core.dtype("fp32"), core.dtype("fp32"),): ("__nv_fadd_rd", core.dtype("fp32")), }, is_pure=True, _builder=_builder) @core.extern def add_ru(arg0, arg1, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, arg1, ], {(core.dtype("fp64"), core.dtype("fp64"),): ("__nv_dadd_ru", core.dtype("fp64")), (core.dtype("fp32"), core.dtype("fp32"),): ("__nv_fadd_ru", core.dtype("fp32")), }, is_pure=True, _builder=_builder) @core.extern def mul_rn(arg0, arg1, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, arg1, ], {(core.dtype("fp64"), core.dtype("fp64"),): ("__nv_dmul_rn", core.dtype("fp64")), (core.dtype("fp32"), core.dtype("fp32"),): ("__nv_fmul_rn", core.dtype("fp32")), }, is_pure=True, _builder=_builder) @core.extern def mul_rz(arg0, arg1, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, arg1, ], {(core.dtype("fp64"), core.dtype("fp64"),): ("__nv_dmul_rz", core.dtype("fp64")), (core.dtype("fp32"), core.dtype("fp32"),): ("__nv_fmul_rz", core.dtype("fp32")), }, is_pure=True, _builder=_builder) @core.extern def mul_rd(arg0, arg1, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, arg1, ], {(core.dtype("fp64"), core.dtype("fp64"),): ("__nv_dmul_rd", core.dtype("fp64")), (core.dtype("fp32"), core.dtype("fp32"),): ("__nv_fmul_rd", core.dtype("fp32")), }, is_pure=True, _builder=_builder) @core.extern def mul_ru(arg0, arg1, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, arg1, ], {(core.dtype("fp64"), core.dtype("fp64"),): ("__nv_dmul_ru", core.dtype("fp64")), (core.dtype("fp32"), core.dtype("fp32"),): ("__nv_fmul_ru", core.dtype("fp32")), }, is_pure=True, _builder=_builder) @core.extern def double2float_rn(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp64"),): ("__nv_double2float_rn", core.dtype("fp32")), }, is_pure=True, _builder=_builder) @core.extern def double2float_rz(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp64"),): ("__nv_double2float_rz", core.dtype("fp32")), }, is_pure=True, _builder=_builder) @core.extern def double2float_rd(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp64"),): ("__nv_double2float_rd", core.dtype("fp32")), }, is_pure=True, _builder=_builder) @core.extern def double2float_ru(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp64"),): ("__nv_double2float_ru", core.dtype("fp32")), }, is_pure=True, _builder=_builder) @core.extern def double2int_rn(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp64"),): ("__nv_double2int_rn", core.dtype("int32")), }, is_pure=True, _builder=_builder) @core.extern def double2int_rz(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp64"),): ("__nv_double2int_rz", core.dtype("int32")), }, is_pure=True, _builder=_builder) @core.extern def double2int_rd(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp64"),): ("__nv_double2int_rd", core.dtype("int32")), }, is_pure=True, _builder=_builder) @core.extern def double2int_ru(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp64"),): ("__nv_double2int_ru", core.dtype("int32")), }, is_pure=True, _builder=_builder) @core.extern def double2uint_rn(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp64"),): ("__nv_double2uint_rn", core.dtype("int32")), }, is_pure=True, _builder=_builder) @core.extern def double2uint_rz(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp64"),): ("__nv_double2uint_rz", core.dtype("int32")), }, is_pure=True, _builder=_builder) @core.extern def double2uint_rd(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp64"),): ("__nv_double2uint_rd", core.dtype("int32")), }, is_pure=True, _builder=_builder) @core.extern def double2uint_ru(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp64"),): ("__nv_double2uint_ru", core.dtype("int32")), }, is_pure=True, _builder=_builder) @core.extern def int2double_rn(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("int32"),): ("__nv_int2double_rn", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def uint2double_rn(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("uint32"),): ("__nv_uint2double_rn", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def float2int_rn(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_float2int_rn", core.dtype("int32")), }, is_pure=True, _builder=_builder) @core.extern def float2int_rz(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_float2int_rz", core.dtype("int32")), }, is_pure=True, _builder=_builder) @core.extern def float2int_rd(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_float2int_rd", core.dtype("int32")), }, is_pure=True, _builder=_builder) @core.extern def float2int_ru(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_float2int_ru", core.dtype("int32")), }, is_pure=True, _builder=_builder) @core.extern def float2uint_rn(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_float2uint_rn", core.dtype("int32")), }, is_pure=True, _builder=_builder) @core.extern def float2uint_rz(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_float2uint_rz", core.dtype("int32")), }, is_pure=True, _builder=_builder) @core.extern def float2uint_rd(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_float2uint_rd", core.dtype("int32")), }, is_pure=True, _builder=_builder) @core.extern def float2uint_ru(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_float2uint_ru", core.dtype("int32")), }, is_pure=True, _builder=_builder) @core.extern def int2float_rn(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("int32"),): ("__nv_int2float_rn", core.dtype("fp32")), }, is_pure=True, _builder=_builder) @core.extern def int2float_rz(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("int32"),): ("__nv_int2float_rz", core.dtype("fp32")), }, is_pure=True, _builder=_builder) @core.extern def int2float_rd(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("int32"),): ("__nv_int2float_rd", core.dtype("fp32")), }, is_pure=True, _builder=_builder) @core.extern def int2float_ru(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("int32"),): ("__nv_int2float_ru", core.dtype("fp32")), }, is_pure=True, _builder=_builder) @core.extern def uint2float_rn(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("uint32"),): ("__nv_uint2float_rn", core.dtype("fp32")), }, is_pure=True, _builder=_builder) @core.extern def uint2float_rz(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("uint32"),): ("__nv_uint2float_rz", core.dtype("fp32")), }, is_pure=True, _builder=_builder) @core.extern def uint2float_rd(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("uint32"),): ("__nv_uint2float_rd", core.dtype("fp32")), }, is_pure=True, _builder=_builder) @core.extern def uint2float_ru(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("uint32"),): ("__nv_uint2float_ru", core.dtype("fp32")), }, is_pure=True, _builder=_builder) @core.extern def hiloint2double(arg0, arg1, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, arg1, ], {(core.dtype("int32"), core.dtype("int32"),): ("__nv_hiloint2double", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def double2loint(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp64"),): ("__nv_double2loint", core.dtype("int32")), }, is_pure=True, _builder=_builder) @core.extern def double2hiint(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp64"),): ("__nv_double2hiint", core.dtype("int32")), }, is_pure=True, _builder=_builder) @core.extern def float2ll_rn(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_float2ll_rn", core.dtype("int64")), }, is_pure=True, _builder=_builder) @core.extern def float2ll_rz(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_float2ll_rz", core.dtype("int64")), }, is_pure=True, _builder=_builder) @core.extern def float2ll_rd(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_float2ll_rd", core.dtype("int64")), }, is_pure=True, _builder=_builder) @core.extern def float2ll_ru(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_float2ll_ru", core.dtype("int64")), }, is_pure=True, _builder=_builder) @core.extern def float2ull_rn(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_float2ull_rn", core.dtype("int64")), }, is_pure=True, _builder=_builder) @core.extern def float2ull_rz(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_float2ull_rz", core.dtype("int64")), }, is_pure=True, _builder=_builder) @core.extern def float2ull_rd(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_float2ull_rd", core.dtype("int64")), }, is_pure=True, _builder=_builder) @core.extern def float2ull_ru(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_float2ull_ru", core.dtype("int64")), }, is_pure=True, _builder=_builder) @core.extern def double2ll_rn(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp64"),): ("__nv_double2ll_rn", core.dtype("int64")), }, is_pure=True, _builder=_builder) @core.extern def double2ll_rz(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp64"),): ("__nv_double2ll_rz", core.dtype("int64")), }, is_pure=True, _builder=_builder) @core.extern def double2ll_rd(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp64"),): ("__nv_double2ll_rd", core.dtype("int64")), }, is_pure=True, _builder=_builder) @core.extern def double2ll_ru(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp64"),): ("__nv_double2ll_ru", core.dtype("int64")), }, is_pure=True, _builder=_builder) @core.extern def double2ull_rn(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp64"),): ("__nv_double2ull_rn", core.dtype("int64")), }, is_pure=True, _builder=_builder) @core.extern def double2ull_rz(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp64"),): ("__nv_double2ull_rz", core.dtype("int64")), }, is_pure=True, _builder=_builder) @core.extern def double2ull_rd(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp64"),): ("__nv_double2ull_rd", core.dtype("int64")), }, is_pure=True, _builder=_builder) @core.extern def double2ull_ru(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp64"),): ("__nv_double2ull_ru", core.dtype("int64")), }, is_pure=True, _builder=_builder) @core.extern def ll2float_rn(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("int64"),): ("__nv_ll2float_rn", core.dtype("fp32")), }, is_pure=True, _builder=_builder) @core.extern def ll2float_rz(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("int64"),): ("__nv_ll2float_rz", core.dtype("fp32")), }, is_pure=True, _builder=_builder) @core.extern def ll2float_rd(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("int64"),): ("__nv_ll2float_rd", core.dtype("fp32")), }, is_pure=True, _builder=_builder) @core.extern def ll2float_ru(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("int64"),): ("__nv_ll2float_ru", core.dtype("fp32")), }, is_pure=True, _builder=_builder) @core.extern def ull2float_rn(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("uint64"),): ("__nv_ull2float_rn", core.dtype("fp32")), }, is_pure=True, _builder=_builder) @core.extern def ull2float_rz(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("uint64"),): ("__nv_ull2float_rz", core.dtype("fp32")), }, is_pure=True, _builder=_builder) @core.extern def ull2float_rd(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("uint64"),): ("__nv_ull2float_rd", core.dtype("fp32")), }, is_pure=True, _builder=_builder) @core.extern def ull2float_ru(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("uint64"),): ("__nv_ull2float_ru", core.dtype("fp32")), }, is_pure=True, _builder=_builder) @core.extern def ll2double_rn(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("int64"),): ("__nv_ll2double_rn", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def ll2double_rz(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("int64"),): ("__nv_ll2double_rz", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def ll2double_rd(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("int64"),): ("__nv_ll2double_rd", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def ll2double_ru(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("int64"),): ("__nv_ll2double_ru", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def ull2double_rn(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("uint64"),): ("__nv_ull2double_rn", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def ull2double_rz(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("uint64"),): ("__nv_ull2double_rz", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def ull2double_rd(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("uint64"),): ("__nv_ull2double_rd", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def ull2double_ru(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("uint64"),): ("__nv_ull2double_ru", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def int_as_float(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("int32"),): ("__nv_int_as_float", core.dtype("fp32")), }, is_pure=True, _builder=_builder) @core.extern def float_as_int(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_float_as_int", core.dtype("int32")), }, is_pure=True, _builder=_builder) @core.extern def uint_as_float(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("uint32"),): ("__nv_uint_as_float", core.dtype("fp32")), }, is_pure=True, _builder=_builder) @core.extern def float_as_uint(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_float_as_uint", core.dtype("int32")), }, is_pure=True, _builder=_builder) @core.extern def longlong_as_double(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("int64"),): ("__nv_longlong_as_double", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def double_as_longlong(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp64"),): ("__nv_double_as_longlong", core.dtype("int64")), }, is_pure=True, _builder=_builder) @core.extern def fast_sinf(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_fast_sinf", core.dtype("fp32")), }, is_pure=True, _builder=_builder) @core.extern def fast_cosf(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_fast_cosf", core.dtype("fp32")), }, is_pure=True, _builder=_builder) @core.extern def fast_log2f(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_fast_log2f", core.dtype("fp32")), }, is_pure=True, _builder=_builder) @core.extern def fast_logf(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_fast_logf", core.dtype("fp32")), }, is_pure=True, _builder=_builder) @core.extern def fast_expf(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_fast_expf", core.dtype("fp32")), }, is_pure=True, _builder=_builder) @core.extern def fast_tanf(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_fast_tanf", core.dtype("fp32")), }, is_pure=True, _builder=_builder) @core.extern def fast_exp10f(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_fast_exp10f", core.dtype("fp32")), }, is_pure=True, _builder=_builder) @core.extern def fast_log10f(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_fast_log10f", core.dtype("fp32")), }, is_pure=True, _builder=_builder) @core.extern def fast_powf(arg0, arg1, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, arg1, ], {(core.dtype("fp32"), core.dtype("fp32"),): ("__nv_fast_powf", core.dtype("fp32")), }, is_pure=True, _builder=_builder) @core.extern def hadd(arg0, arg1, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, arg1, ], {(core.dtype("int32"), core.dtype("int32"),): ("__nv_hadd", core.dtype("int32")), (core.dtype("uint32"), core.dtype("uint32"),): ("__nv_uhadd", core.dtype("uint32")), }, is_pure=True, _builder=_builder) @core.extern def rhadd(arg0, arg1, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, arg1, ], {(core.dtype("int32"), core.dtype("int32"),): ("__nv_rhadd", core.dtype("int32")), (core.dtype("uint32"), core.dtype("uint32"),): ("__nv_urhadd", core.dtype("uint32")), }, is_pure=True, _builder=_builder) @core.extern def sub_rn(arg0, arg1, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, arg1, ], {(core.dtype("fp32"), core.dtype("fp32"),): ("__nv_fsub_rn", core.dtype("fp32")), (core.dtype("fp64"), core.dtype("fp64"),): ("__nv_dsub_rn", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def sub_rz(arg0, arg1, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, arg1, ], {(core.dtype("fp32"), core.dtype("fp32"),): ("__nv_fsub_rz", core.dtype("fp32")), (core.dtype("fp64"), core.dtype("fp64"),): ("__nv_dsub_rz", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def sub_rd(arg0, arg1, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, arg1, ], {(core.dtype("fp32"), core.dtype("fp32"),): ("__nv_fsub_rd", core.dtype("fp32")), (core.dtype("fp64"), core.dtype("fp64"),): ("__nv_dsub_rd", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def sub_ru(arg0, arg1, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, arg1, ], {(core.dtype("fp32"), core.dtype("fp32"),): ("__nv_fsub_ru", core.dtype("fp32")), (core.dtype("fp64"), core.dtype("fp64"),): ("__nv_dsub_ru", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def rsqrt_rn(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_frsqrt_rn", core.dtype("fp32")), }, is_pure=True, _builder=_builder) @core.extern def ffs(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("int32"),): ("__nv_ffs", core.dtype("int32")), (core.dtype("int64"),): ("__nv_ffsll", core.dtype("int32")), }, is_pure=True, _builder=_builder) @core.extern def rint(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_rintf", core.dtype("fp32")), (core.dtype("fp64"),): ("__nv_rint", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def llrint(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_llrintf", core.dtype("int64")), (core.dtype("fp64"),): ("__nv_llrint", core.dtype("int64")), }, is_pure=True, _builder=_builder) @core.extern def nearbyint(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_nearbyintf", core.dtype("fp32")), (core.dtype("fp64"),): ("__nv_nearbyint", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def isnan(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_isnanf", core.dtype("int32")), (core.dtype("fp64"),): ("__nv_isnand", core.dtype("int32")), }, is_pure=True, _builder=_builder) @core.extern def signbit(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_signbitf", core.dtype("int32")), (core.dtype("fp64"),): ("__nv_signbitd", core.dtype("int32")), }, is_pure=True, _builder=_builder) @core.extern def copysign(arg0, arg1, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, arg1, ], {(core.dtype("fp32"), core.dtype("fp32"),): ("__nv_copysignf", core.dtype("fp32")), (core.dtype("fp64"), core.dtype("fp64"),): ("__nv_copysign", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def finitef(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_finitef", core.dtype("int32")), }, is_pure=True, _builder=_builder) @core.extern def isinf(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_isinff", core.dtype("int32")), (core.dtype("fp64"),): ("__nv_isinfd", core.dtype("int32")), }, is_pure=True, _builder=_builder) @core.extern def nextafter(arg0, arg1, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, arg1, ], {(core.dtype("fp32"), core.dtype("fp32"),): ("__nv_nextafterf", core.dtype("fp32")), (core.dtype("fp64"), core.dtype("fp64"),): ("__nv_nextafter", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def sin(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_sinf", core.dtype("fp32")), (core.dtype("fp64"),): ("__nv_sin", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def cos(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_cosf", core.dtype("fp32")), (core.dtype("fp64"),): ("__nv_cos", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def sinpi(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_sinpif", core.dtype("fp32")), (core.dtype("fp64"),): ("__nv_sinpi", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def cospi(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_cospif", core.dtype("fp32")), (core.dtype("fp64"),): ("__nv_cospi", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def tan(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_tanf", core.dtype("fp32")), (core.dtype("fp64"),): ("__nv_tan", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def log2(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_log2f", core.dtype("fp32")), (core.dtype("fp64"),): ("__nv_log2", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def exp(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_expf", core.dtype("fp32")), (core.dtype("fp64"),): ("__nv_exp", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def exp10(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_exp10f", core.dtype("fp32")), (core.dtype("fp64"),): ("__nv_exp10", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def cosh(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_coshf", core.dtype("fp32")), (core.dtype("fp64"),): ("__nv_cosh", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def sinh(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_sinhf", core.dtype("fp32")), (core.dtype("fp64"),): ("__nv_sinh", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def tanh(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_tanhf", core.dtype("fp32")), (core.dtype("fp64"),): ("__nv_tanh", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def atan2(arg0, arg1, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, arg1, ], {(core.dtype("fp32"), core.dtype("fp32"),): ("__nv_atan2f", core.dtype("fp32")), (core.dtype("fp64"), core.dtype("fp64"),): ("__nv_atan2", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def atan(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_atanf", core.dtype("fp32")), (core.dtype("fp64"),): ("__nv_atan", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def asin(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_asinf", core.dtype("fp32")), (core.dtype("fp64"),): ("__nv_asin", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def acos(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_acosf", core.dtype("fp32")), (core.dtype("fp64"),): ("__nv_acos", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def log(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_logf", core.dtype("fp32")), (core.dtype("fp64"),): ("__nv_log", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def log10(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_log10f", core.dtype("fp32")), (core.dtype("fp64"),): ("__nv_log10", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def log1p(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_log1pf", core.dtype("fp32")), (core.dtype("fp64"),): ("__nv_log1p", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def acosh(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_acoshf", core.dtype("fp32")), (core.dtype("fp64"),): ("__nv_acosh", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def asinh(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_asinhf", core.dtype("fp32")), (core.dtype("fp64"),): ("__nv_asinh", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def atanh(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_atanhf", core.dtype("fp32")), (core.dtype("fp64"),): ("__nv_atanh", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def expm1(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_expm1f", core.dtype("fp32")), (core.dtype("fp64"),): ("__nv_expm1", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def hypot(arg0, arg1, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, arg1, ], {(core.dtype("fp32"), core.dtype("fp32"),): ("__nv_hypotf", core.dtype("fp32")), (core.dtype("fp64"), core.dtype("fp64"),): ("__nv_hypot", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def rhypot(arg0, arg1, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, arg1, ], {(core.dtype("fp32"), core.dtype("fp32"),): ("__nv_rhypotf", core.dtype("fp32")), (core.dtype("fp64"), core.dtype("fp64"),): ("__nv_rhypot", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def norm3d(arg0, arg1, arg2, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, arg1, arg2, ], {(core.dtype("fp32"), core.dtype("fp32"), core.dtype("fp32"),): ("__nv_norm3df", core.dtype("fp32")), (core.dtype("fp64"), core.dtype("fp64"), core.dtype("fp64"),): ("__nv_norm3d", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def rnorm3d(arg0, arg1, arg2, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, arg1, arg2, ], {(core.dtype("fp32"), core.dtype("fp32"), core.dtype("fp32"),): ("__nv_rnorm3df", core.dtype("fp32")), (core.dtype("fp64"), core.dtype("fp64"), core.dtype("fp64"),): ("__nv_rnorm3d", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def norm4d(arg0, arg1, arg2, arg3, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, arg1, arg2, arg3, ], {(core.dtype("fp32"), core.dtype("fp32"), core.dtype("fp32"), core.dtype("fp32"),): ("__nv_norm4df", core.dtype("fp32")), (core.dtype("fp64"), core.dtype("fp64"), core.dtype("fp64"), core.dtype("fp64"),): ("__nv_norm4d", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def rnorm4d(arg0, arg1, arg2, arg3, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, arg1, arg2, arg3, ], {(core.dtype("fp32"), core.dtype("fp32"), core.dtype("fp32"), core.dtype("fp32"),): ("__nv_rnorm4df", core.dtype("fp32")), (core.dtype("fp64"), core.dtype("fp64"), core.dtype("fp64"), core.dtype("fp64"),): ("__nv_rnorm4d", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def cbrt(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_cbrtf", core.dtype("fp32")), (core.dtype("fp64"),): ("__nv_cbrt", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def rcbrt(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_rcbrtf", core.dtype("fp32")), (core.dtype("fp64"),): ("__nv_rcbrt", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def j0(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_j0f", core.dtype("fp32")), (core.dtype("fp64"),): ("__nv_j0", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def j1(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_j1f", core.dtype("fp32")), (core.dtype("fp64"),): ("__nv_j1", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def y0(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_y0f", core.dtype("fp32")), (core.dtype("fp64"),): ("__nv_y0", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def y1(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_y1f", core.dtype("fp32")), (core.dtype("fp64"),): ("__nv_y1", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def yn(arg0, arg1, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, arg1, ], {(core.dtype("int32"), core.dtype("fp32"),): ("__nv_ynf", core.dtype("fp32")), (core.dtype("int32"), core.dtype("fp64"),): ("__nv_yn", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def jn(arg0, arg1, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, arg1, ], {(core.dtype("int32"), core.dtype("fp32"),): ("__nv_jnf", core.dtype("fp32")), (core.dtype("int32"), core.dtype("fp64"),): ("__nv_jn", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def cyl_bessel_i0(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_cyl_bessel_i0f", core.dtype("fp32")), (core.dtype("fp64"),): ("__nv_cyl_bessel_i0", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def cyl_bessel_i1(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_cyl_bessel_i1f", core.dtype("fp32")), (core.dtype("fp64"),): ("__nv_cyl_bessel_i1", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def erf(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_erff", core.dtype("fp32")), (core.dtype("fp64"),): ("__nv_erf", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def erfinv(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_erfinvf", core.dtype("fp32")), (core.dtype("fp64"),): ("__nv_erfinv", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def erfc(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_erfcf", core.dtype("fp32")), (core.dtype("fp64"),): ("__nv_erfc", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def erfcx(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_erfcxf", core.dtype("fp32")), (core.dtype("fp64"),): ("__nv_erfcx", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def erfcinv(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_erfcinvf", core.dtype("fp32")), (core.dtype("fp64"),): ("__nv_erfcinv", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def normcdfinv(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_normcdfinvf", core.dtype("fp32")), (core.dtype("fp64"),): ("__nv_normcdfinv", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def normcdf(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_normcdff", core.dtype("fp32")), (core.dtype("fp64"),): ("__nv_normcdf", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def lgamma(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_lgammaf", core.dtype("fp32")), (core.dtype("fp64"),): ("__nv_lgamma", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def ldexp(arg0, arg1, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, arg1, ], {(core.dtype("fp32"), core.dtype("int32"),): ("__nv_ldexpf", core.dtype("fp32")), (core.dtype("fp64"), core.dtype("int32"),): ("__nv_ldexp", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def scalbn(arg0, arg1, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, arg1, ], {(core.dtype("fp32"), core.dtype("int32"),): ("__nv_scalbnf", core.dtype("fp32")), (core.dtype("fp64"), core.dtype("int32"),): ("__nv_scalbn", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def fmod(arg0, arg1, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, arg1, ], {(core.dtype("fp32"), core.dtype("fp32"),): ("__nv_fmodf", core.dtype("fp32")), (core.dtype("fp64"), core.dtype("fp64"),): ("__nv_fmod", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def remainder(arg0, arg1, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, arg1, ], {(core.dtype("fp32"), core.dtype("fp32"),): ("__nv_remainderf", core.dtype("fp32")), (core.dtype("fp64"), core.dtype("fp64"),): ("__nv_remainder", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def fma(arg0, arg1, arg2, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, arg1, arg2, ], {(core.dtype("fp32"), core.dtype("fp32"), core.dtype("fp32"),): ("__nv_fmaf", core.dtype("fp32")), (core.dtype("fp64"), core.dtype("fp64"), core.dtype("fp64"),): ("__nv_fma", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def pow(arg0, arg1, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, arg1, ], {(core.dtype("fp32"), core.dtype("int32"),): ("__nv_powif", core.dtype("fp32")), (core.dtype("fp64"), core.dtype("int32"),): ("__nv_powi", core.dtype("fp64")), (core.dtype("fp32"), core.dtype("fp32"),): ("__nv_powf", core.dtype("fp32")), (core.dtype("fp64"), core.dtype("fp64"),): ("__nv_pow", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def tgamma(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_tgammaf", core.dtype("fp32")), (core.dtype("fp64"),): ("__nv_tgamma", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def round(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_roundf", core.dtype("fp32")), (core.dtype("fp64"),): ("__nv_round", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def llround(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_llroundf", core.dtype("int64")), (core.dtype("fp64"),): ("__nv_llround", core.dtype("int64")), }, is_pure=True, _builder=_builder) @core.extern def fdim(arg0, arg1, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, arg1, ], {(core.dtype("fp32"), core.dtype("fp32"),): ("__nv_fdimf", core.dtype("fp32")), (core.dtype("fp64"), core.dtype("fp64"),): ("__nv_fdim", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def ilogb(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_ilogbf", core.dtype("int32")), (core.dtype("fp64"),): ("__nv_ilogb", core.dtype("int32")), }, is_pure=True, _builder=_builder) @core.extern def logb(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp32"),): ("__nv_logbf", core.dtype("fp32")), (core.dtype("fp64"),): ("__nv_logb", core.dtype("fp64")), }, is_pure=True, _builder=_builder) @core.extern def isfinited(arg0, _builder=None): return core.extern_elementwise("libdevice", libdevice_path(), [arg0, ], {(core.dtype("fp64"),): ("__nv_isfinited", core.dtype("int32")), }, is_pure=True, _builder=_builder)
75,127
47.943322
157
py
triton
triton-main/python/triton/ops/flash_attention.py
""" Fused Attention =============== This is a Triton implementation of the Flash Attention algorithm (see: Dao et al., https://arxiv.org/pdf/2205.14135v2.pdf; Rabe and Staats https://arxiv.org/pdf/2112.05682v2.pdf) Sequence Parallel implementation inspired by HazyResearch (see https://github.com/HazyResearch/flash-attention/blob/main/flash_attn/flash_attn_triton.py) """ import torch from .. import cdiv, jit from .. import language as tl @jit def _fwd_kernel( Q, K, V, sm_scale, L, M, Out, stride_qz, stride_qh, stride_qm, stride_qk, stride_kz, stride_kh, stride_kn, stride_kk, stride_vz, stride_vh, stride_vk, stride_vn, stride_oz, stride_oh, stride_om, stride_on, Z, H, N_CTX, BLOCK_M: tl.constexpr, BLOCK_DMODEL: tl.constexpr, BLOCK_N: tl.constexpr, MODE: tl.constexpr, ): start_m = tl.program_id(0) off_hz = tl.program_id(1) qvk_offset = off_hz * stride_qh Q_block_ptr = tl.make_block_ptr( base=Q + qvk_offset, shape=(N_CTX, BLOCK_DMODEL), strides=(stride_qm, stride_qk), offsets=(start_m * BLOCK_M, 0), block_shape=(BLOCK_M, BLOCK_DMODEL), order=(1, 0) ) K_block_ptr = tl.make_block_ptr( base=K + qvk_offset, shape=(BLOCK_DMODEL, N_CTX), strides=(stride_kk, stride_kn), offsets=(0, 0), block_shape=(BLOCK_DMODEL, BLOCK_N), order=(0, 1) ) V_block_ptr = tl.make_block_ptr( base=V + qvk_offset, shape=(N_CTX, BLOCK_DMODEL), strides=(stride_vk, stride_vn), offsets=(0, 0), block_shape=(BLOCK_N, BLOCK_DMODEL), order=(1, 0) ) O_block_ptr = tl.make_block_ptr( base=Out + qvk_offset, shape=(N_CTX, BLOCK_DMODEL), strides=(stride_om, stride_on), offsets=(start_m * BLOCK_M, 0), block_shape=(BLOCK_M, BLOCK_DMODEL), order=(1, 0) ) # initialize offsets offs_m = start_m * BLOCK_M + tl.arange(0, BLOCK_M) offs_n = tl.arange(0, BLOCK_N) # initialize pointer to m and l m_i = tl.zeros([BLOCK_M], dtype=tl.float32) - float("inf") l_i = tl.zeros([BLOCK_M], dtype=tl.float32) acc = tl.zeros([BLOCK_M, BLOCK_DMODEL], dtype=tl.float32) # causal check on every loop iteration can be expensive # and peeling the last iteration of the loop does not work well with ptxas # so we have a mode to do the causal check in a separate kernel entirely if MODE == 0: # entire non-causal attention lo, hi = 0, N_CTX if MODE == 1: # entire causal attention lo, hi = 0, (start_m + 1) * BLOCK_M if MODE == 2: # off band-diagonal lo, hi = 0, start_m * BLOCK_M if MODE == 3: # on band-diagonal l_ptrs = L + off_hz * N_CTX + offs_m m_ptrs = M + off_hz * N_CTX + offs_m m_i = tl.load(m_ptrs) l_i = tl.load(l_ptrs) acc += tl.load(O_block_ptr).to(tl.float32) lo, hi = start_m * BLOCK_M, (start_m + 1) * BLOCK_M # credits to: Adam P. Goucher (https://github.com/apgoucher): # scale sm_scale by 1/log_2(e) and use # 2^x instead of exp in the loop because CSE and LICM # don't work as expected with `exp` in the loop qk_scale = sm_scale * 1.44269504 # load q: it will stay in SRAM throughout q = tl.load(Q_block_ptr) q = (q * qk_scale).to(K.dtype.element_ty) # advance block pointers to first iteration of the loop K_block_ptr = tl.advance(K_block_ptr, (0, lo)) V_block_ptr = tl.advance(V_block_ptr, (lo, 0)) # loop over k, v and update accumulator for start_n in range(lo, hi, BLOCK_N): start_n = tl.multiple_of(start_n, BLOCK_N) # -- compute qk ---- k = tl.load(K_block_ptr) qk = tl.zeros([BLOCK_M, BLOCK_N], dtype=tl.float32) qk += tl.dot(q, k, allow_tf32=True) if MODE == 1 or MODE == 3: qk = tl.where(offs_m[:, None] >= (start_n + offs_n[None, :]), qk, float("-inf")) # -- compute m_ij, p, l_ij m_ij = tl.max(qk, 1) p = tl.math.exp2(qk - m_ij[:, None]) l_ij = tl.sum(p, 1) # -- update m_i and l_i m_i_new = tl.maximum(m_i, m_ij) alpha = tl.math.exp2(m_i - m_i_new) beta = tl.math.exp2(m_ij - m_i_new) l_i *= alpha l_i_new = l_i + beta * l_ij # scale p p_scale = beta / l_i_new p = p * p_scale[:, None] # scale acc acc_scale = l_i / l_i_new acc = acc * acc_scale[:, None] # update acc v = tl.load(V_block_ptr) p = p.to(V.dtype.element_ty) acc += tl.dot(p, v, allow_tf32=True) # update m_i and l_i l_i = l_i_new m_i = m_i_new # update pointers K_block_ptr = tl.advance(K_block_ptr, (0, BLOCK_N)) V_block_ptr = tl.advance(V_block_ptr, (BLOCK_N, 0)) # write back l and m l_ptrs = L + off_hz * N_CTX + offs_m m_ptrs = M + off_hz * N_CTX + offs_m tl.store(l_ptrs, l_i) tl.store(m_ptrs, m_i) # write back O tl.store(O_block_ptr, acc.to(K.dtype.element_ty)) @jit def _bwd_preprocess( Out, DO, L, NewDO, Delta, BLOCK_M: tl.constexpr, D_HEAD: tl.constexpr, ): off_m = tl.program_id(0) * BLOCK_M + tl.arange(0, BLOCK_M) off_n = tl.arange(0, D_HEAD) # load o = tl.load(Out + off_m[:, None] * D_HEAD + off_n[None, :]).to(tl.float32) do = tl.load(DO + off_m[:, None] * D_HEAD + off_n[None, :]).to(tl.float32) denom = tl.load(L + off_m).to(tl.float32) # compute do = do / denom[:, None] delta = tl.sum(o * do, axis=1) # write-back tl.store(NewDO + off_m[:, None] * D_HEAD + off_n[None, :], do) tl.store(Delta + off_m, delta) @jit def _bwd_kernel_one_col_block( Q, K, V, sm_scale, qk_scale, Out, DO, DQ, DK, DV, L, M, D, stride_dqa, stride_qz, stride_qh, stride_qm, stride_qk, stride_kz, stride_kh, stride_kn, stride_kk, stride_vz, stride_vh, stride_vk, stride_vn, Z, H, N_CTX, off_hz, start_n, num_block, BLOCK_M: tl.constexpr, BLOCK_DMODEL: tl.constexpr, BLOCK_N: tl.constexpr, SEQUENCE_PARALLEL: tl.constexpr, MODE: tl.constexpr, ): if SEQUENCE_PARALLEL: DQ += stride_dqa.to(tl.int64) * start_n if MODE == 0: lo = 0 else: lo = start_n * BLOCK_M # initialize row/col offsets offs_qm = lo + tl.arange(0, BLOCK_M) offs_n = start_n * BLOCK_M + tl.arange(0, BLOCK_M) offs_m = tl.arange(0, BLOCK_N) offs_k = tl.arange(0, BLOCK_DMODEL) # initialize pointers to value-like data q_ptrs = Q + (offs_qm[:, None] * stride_qm + offs_k[None, :] * stride_qk) k_ptrs = K + (offs_n[:, None] * stride_kn + offs_k[None, :] * stride_kk) v_ptrs = V + (offs_n[:, None] * stride_vk + offs_k[None, :] * stride_vn) do_ptrs = DO + (offs_qm[:, None] * stride_qm + offs_k[None, :] * stride_qk) dq_ptrs = DQ + (offs_qm[:, None] * stride_qm + offs_k[None, :] * stride_qk) # pointer to row-wise quantities in value-like data D_ptrs = D + off_hz * N_CTX m_ptrs = M + off_hz * N_CTX # initialize dv amd dk dv = tl.zeros([BLOCK_M, BLOCK_DMODEL], dtype=tl.float32) dk = tl.zeros([BLOCK_M, BLOCK_DMODEL], dtype=tl.float32) # k and v stay in SRAM throughout k = tl.load(k_ptrs) v = tl.load(v_ptrs) # loop over rows for start_m in range(lo, num_block * BLOCK_M, BLOCK_M): offs_m_curr = start_m + offs_m # load q, k, v, do on-chip q = tl.load(q_ptrs) # recompute p = softmax(qk, dim=-1).T # NOTE: `do` is pre-divided by `l`; no normalization here if MODE == 1: qk = tl.where(offs_m_curr[:, None] >= (offs_n[None, :]), float(0.), float("-inf")) else: qk = tl.zeros([BLOCK_M, BLOCK_N], dtype=tl.float32) qk += tl.dot(q, tl.trans(k)) qk *= qk_scale m = tl.load(m_ptrs + offs_m_curr) p = tl.math.exp2(qk - m[:, None]) # compute dv do = tl.load(do_ptrs) dv += tl.dot(tl.trans(p.to(Q.dtype.element_ty)), do, allow_tf32=True) # compute dp = dot(v, do) Di = tl.load(D_ptrs + offs_m_curr) # dp = tl.zeros([BLOCK_M, BLOCK_N], dtype=tl.float32) - Di[:, None] dp = tl.dot(do, tl.trans(v), allow_tf32=True) # compute ds = p * (dp - delta[:, None]) ds = (p * (dp - Di[:, None]) * sm_scale).to(Q.dtype.element_ty) # compute dk = dot(ds.T, q) dk += tl.dot(tl.trans(ds), q, allow_tf32=True) # compute dq if not SEQUENCE_PARALLEL: dq = tl.load(dq_ptrs) dq += tl.dot(ds, k, allow_tf32=True) tl.store(dq_ptrs, dq) elif SEQUENCE_PARALLEL: # dq = tl.dot(ds, k, allow_tf32=True) dq = tl.trans(tl.dot(tl.trans(k), tl.trans(ds), allow_tf32=True)) tl.store(dq_ptrs, dq) # increment pointers dq_ptrs += BLOCK_M * stride_qm q_ptrs += BLOCK_M * stride_qm do_ptrs += BLOCK_M * stride_qm # write-back dv_ptrs = DV + (offs_n[:, None] * stride_vk + offs_k[None, :] * stride_vn) dk_ptrs = DK + (offs_n[:, None] * stride_kn + offs_k[None, :] * stride_kk) tl.store(dv_ptrs, dv) tl.store(dk_ptrs, dk) @jit def _bwd_kernel( # fmt: off Q, K, V, sm_scale, Out, DO, DQ, DK, DV, L, M, D, stride_dqa, stride_qz, stride_qh, stride_qm, stride_qk, stride_kz, stride_kh, stride_kn, stride_kk, stride_vz, stride_vh, stride_vk, stride_vn, Z, H, N_CTX, BLOCK_M: tl.constexpr, BLOCK_DMODEL: tl.constexpr, BLOCK_N: tl.constexpr, SEQUENCE_PARALLEL: tl.constexpr, MODE: tl.constexpr, # fmt: on ): qk_scale = sm_scale * 1.44269504 off_hz = tl.program_id(0) off_z = off_hz // H off_h = off_hz % H # offset pointers for batch/head Q += off_z * stride_qz + off_h * stride_qh K += off_z * stride_kz + off_h * stride_kh V += off_z * stride_vz + off_h * stride_vh DO += off_z * stride_qz + off_h * stride_qh DQ += off_z * stride_qz + off_h * stride_qh DK += off_z * stride_kz + off_h * stride_kh DV += off_z * stride_vz + off_h * stride_vh num_block_n = tl.cdiv(N_CTX, BLOCK_N) if not SEQUENCE_PARALLEL: for start_n in range(0, num_block_n): _bwd_kernel_one_col_block( Q, K, V, sm_scale, qk_scale, Out, DO, DQ, DK, DV, L, M, D, stride_dqa, stride_qz, stride_qh, stride_qm, stride_qk, stride_kz, stride_kh, stride_kn, stride_kk, stride_vz, stride_vh, stride_vk, stride_vn, Z, H, N_CTX, off_hz, start_n, num_block_n, BLOCK_M=BLOCK_M, BLOCK_DMODEL=BLOCK_DMODEL, BLOCK_N=BLOCK_N, SEQUENCE_PARALLEL=SEQUENCE_PARALLEL, MODE=MODE, ) else: start_n = tl.program_id(1) _bwd_kernel_one_col_block( Q, K, V, sm_scale, qk_scale, Out, DO, DQ, DK, DV, L, M, D, stride_dqa, stride_qz, stride_qh, stride_qm, stride_qk, stride_kz, stride_kh, stride_kn, stride_kk, stride_vz, stride_vh, stride_vk, stride_vn, Z, H, N_CTX, off_hz, start_n, num_block_n, BLOCK_M=BLOCK_M, BLOCK_DMODEL=BLOCK_DMODEL, BLOCK_N=BLOCK_N, SEQUENCE_PARALLEL=SEQUENCE_PARALLEL, MODE=MODE, ) class _attention(torch.autograd.Function): @staticmethod def forward(ctx, q, k, v, causal, sm_scale, sequence_parallel=False): # only support for Ampere now capability = torch.cuda.get_device_capability() if capability[0] < 8: raise RuntimeError("Flash attention currently only supported for compute capability >= 80") BLOCK = 128 # shape constraints Lq, Lk, Lv = q.shape[-1], k.shape[-1], v.shape[-1] assert Lq == Lk and Lk == Lv assert Lk in {16, 32, 64, 128} o = torch.empty_like(q) grid = (cdiv(q.shape[2], BLOCK), q.shape[0] * q.shape[1], 1) L = torch.empty((q.shape[0] * q.shape[1], q.shape[2]), device=q.device, dtype=torch.float32) m = torch.empty((q.shape[0] * q.shape[1], q.shape[2]), device=q.device, dtype=torch.float32) num_warps = 4 if Lk <= 64 else 8 if causal: modes = [1] if q.shape[2] <= 2048 else [2, 3] else: modes = [0] for mode in modes: _fwd_kernel[grid]( q, k, v, sm_scale, L, m, o, q.stride(0), q.stride(1), q.stride(2), q.stride(3), k.stride(0), k.stride(1), k.stride(2), k.stride(3), v.stride(0), v.stride(1), v.stride(2), v.stride(3), o.stride(0), o.stride(1), o.stride(2), o.stride(3), q.shape[0], q.shape[1], q.shape[2], BLOCK_M=128, BLOCK_N=BLOCK, BLOCK_DMODEL=Lk, MODE=mode, num_warps=num_warps, num_stages=2) ctx.save_for_backward(q, k, v, o, L, m) ctx.grid = grid ctx.sm_scale = sm_scale ctx.BLOCK_DMODEL = Lk ctx.causal = causal ctx.sequence_parallel = sequence_parallel return o @staticmethod def backward(ctx, do): BLOCK = 128 q, k, v, o, l, m = ctx.saved_tensors sequence_parallel = ctx.sequence_parallel seq_len_kv = k.shape[2] do = do.contiguous() if sequence_parallel: replicas = cdiv(seq_len_kv, BLOCK) new_dq_shape = (replicas,) + q.shape dq = torch.zeros(new_dq_shape, device=q.device, dtype=q.dtype) else: dq = torch.zeros_like(q, dtype=torch.float32) dk = torch.empty_like(k) dv = torch.empty_like(v) do_scaled = torch.empty_like(do) delta = torch.empty_like(l) if ctx.causal: mode = 1 else: mode = 0 _bwd_preprocess[(ctx.grid[0] * ctx.grid[1], )]( o, do, l, do_scaled, delta, BLOCK_M=BLOCK, D_HEAD=ctx.BLOCK_DMODEL, ) _bwd_kernel[(ctx.grid[1], cdiv(seq_len_kv, BLOCK) if sequence_parallel else 1)]( q, k, v, ctx.sm_scale, o, do_scaled, dq, dk, dv, l, m, delta, o.numel(), q.stride(0), q.stride(1), q.stride(2), q.stride(3), k.stride(0), k.stride(1), k.stride(2), k.stride(3), v.stride(0), v.stride(1), v.stride(2), v.stride(3), q.shape[0], q.shape[1], q.shape[2], BLOCK_M=BLOCK, BLOCK_N=BLOCK, BLOCK_DMODEL=ctx.BLOCK_DMODEL, SEQUENCE_PARALLEL=sequence_parallel, MODE=mode, num_warps=8, num_stages=1, ) if len(dq.shape) == 5: dq = dq.sum(dim=0) return dq, dk, dv, None, None, None attention = _attention.apply
15,056
35.107914
113
py
triton
triton-main/python/triton/ops/cross_entropy.py
import torch from .. import heuristics, jit from .. import language as tl from .. import next_power_of_2 def num_warps(N): if N < 2048: return 4 elif N < 8192: return 8 return 16 @heuristics({'num_warps': lambda nargs: num_warps(nargs['N'])}) @heuristics({'BLOCK': lambda nargs: next_power_of_2(nargs['N'])}) @jit def _forward(LOGITS, PROBS, IDX, LOSS, N, BLOCK: tl.constexpr): row = tl.program_id(0) cols = tl.arange(0, BLOCK) idx = tl.load(IDX + row) # pointers to logit and probs LOGITS = LOGITS + row * N + cols WRIT_PROBS = PROBS + row * N + cols READ_PROBS = PROBS + row * N + idx # write-back negative log-probs logits = tl.load(LOGITS, mask=cols < N, other=-float('inf')) logits = logits.to(tl.float32) logits = logits - tl.max(logits, 0) probs = tl.log(tl.sum(tl.exp(logits), 0)) - logits tl.store(WRIT_PROBS, probs, mask=cols < N) # There is a bug in the compiler, which fails to insert a barrier here. # We add it explicitly for now. Will be fixed soon. tl.debug_barrier() # write-back loss probs = tl.load(READ_PROBS) tl.store(LOSS + row, probs) @heuristics({'num_warps': lambda nargs: num_warps(nargs['N'])}) @heuristics({'BLOCK': lambda nargs: next_power_of_2(nargs['N'])}) @jit def _backward(PROBS, IDX, DPROBS, N, BLOCK: tl.constexpr): row = tl.program_id(0) cols = tl.arange(0, BLOCK) idx = tl.load(IDX + row) # pointers to probs PROBS = PROBS + row * N + cols # We know d(-log(p[i])/dlogit[k] = -id_mat[i,k] + p[k] # and we have -log(p[k]) stored in PROBS, so this is easy probs = -tl.load(PROBS, mask=cols < N, other=float('inf')) probs = tl.exp(probs.to(tl.float32)) delta = cols == idx # write result in-place in PROBS dout = tl.load(DPROBS + row) din = (probs - delta) * dout tl.store(PROBS, din.to(PROBS.dtype.element_ty), mask=cols < N) class _cross_entropy(torch.autograd.Function): @classmethod def forward(cls, ctx, logits, indices): # make sure we can use triton assert (indices.dtype == torch.int64), "Indices are expected to be of type long." # make kernel device, dtype = logits.device, logits.dtype n_cols = logits.shape[-1] # run the kernel result = torch.empty_like(indices, dtype=dtype, device=device) neg_logprobs = torch.empty_like(logits, dtype=dtype, device=device) grid = lambda opt: (logits.numel() // n_cols, ) _forward[grid](logits, neg_logprobs, indices, result, n_cols) # save for backward ctx.save_for_backward(neg_logprobs, indices) return result @classmethod def backward(cls, ctx, dneg_logprobs): """We know d(-log(p[i])/dlogit[k] = -id_mat[i,k] + p[k] so we initialize the gradient as neg_logprobs, so we can just exponentiate to get p[k], which is most of what we need... neg_logprobs will be modified in place to become the gradient we want """ # load saved tensors neg_logprobs, indices = ctx.saved_tensors # run the kernel # neg_logprobs will be modified in place to become our gradient: n_cols = neg_logprobs.shape[-1] grid = lambda opt: (neg_logprobs.numel() // n_cols, ) _backward[grid](neg_logprobs, indices, dneg_logprobs, n_cols) return neg_logprobs, None cross_entropy = _cross_entropy.apply
3,450
34.947917
89
py
triton
triton-main/python/triton/ops/matmul.py
import torch from .. import Config, autotune, cdiv, heuristics, jit from .. import language as tl from .matmul_perf_model import early_config_prune, estimate_matmul_time _ordered_datatypes = [torch.float16, torch.bfloat16, torch.float32] def get_higher_dtype(a, b): if a is b: return a assert a in _ordered_datatypes assert b in _ordered_datatypes for d in _ordered_datatypes: if a is d: return b if b is d: return a def init_to_zero(name): return lambda nargs: nargs[name].zero_() def get_configs_io_bound(): configs = [] for num_stages in [2, 3, 4, 5, 6]: for block_m in [16, 32]: for block_k in [32, 64]: for block_n in [32, 64, 128, 256]: num_warps = 2 if block_n <= 64 else 4 configs.append( Config({'BLOCK_M': block_m, 'BLOCK_N': block_n, 'BLOCK_K': block_k, 'SPLIT_K': 1}, num_stages=num_stages, num_warps=num_warps)) # split_k for split_k in [2, 4, 8, 16]: configs.append(Config({'BLOCK_M': block_m, 'BLOCK_N': block_n, 'BLOCK_K': block_k, 'SPLIT_K': split_k}, num_stages=num_stages, num_warps=num_warps, pre_hook=init_to_zero('C'))) return configs @autotune( configs=[ # basic configs for compute-bound matmuls Config({'BLOCK_M': 128, 'BLOCK_N': 256, 'BLOCK_K': 32, 'SPLIT_K': 1}, num_stages=3, num_warps=8), Config({'BLOCK_M': 256, 'BLOCK_N': 128, 'BLOCK_K': 32, 'SPLIT_K': 1}, num_stages=3, num_warps=8), Config({'BLOCK_M': 256, 'BLOCK_N': 64, 'BLOCK_K': 32, 'SPLIT_K': 1}, num_stages=4, num_warps=4), Config({'BLOCK_M': 64, 'BLOCK_N': 256, 'BLOCK_K': 32, 'SPLIT_K': 1}, num_stages=4, num_warps=4), Config({'BLOCK_M': 128, 'BLOCK_N': 128, 'BLOCK_K': 32, 'SPLIT_K': 1}, num_stages=4, num_warps=4), Config({'BLOCK_M': 128, 'BLOCK_N': 64, 'BLOCK_K': 32, 'SPLIT_K': 1}, num_stages=4, num_warps=4), Config({'BLOCK_M': 64, 'BLOCK_N': 128, 'BLOCK_K': 32, 'SPLIT_K': 1}, num_stages=4, num_warps=4), Config({'BLOCK_M': 128, 'BLOCK_N': 32, 'BLOCK_K': 32, 'SPLIT_K': 1}, num_stages=4, num_warps=4), Config({'BLOCK_M': 64, 'BLOCK_N': 32, 'BLOCK_K': 32, 'SPLIT_K': 1}, num_stages=5, num_warps=2), # good for int8 Config({'BLOCK_M': 128, 'BLOCK_N': 256, 'BLOCK_K': 128, 'SPLIT_K': 1}, num_stages=3, num_warps=8), Config({'BLOCK_M': 256, 'BLOCK_N': 128, 'BLOCK_K': 128, 'SPLIT_K': 1}, num_stages=3, num_warps=8), Config({'BLOCK_M': 256, 'BLOCK_N': 64, 'BLOCK_K': 128, 'SPLIT_K': 1}, num_stages=4, num_warps=4), Config({'BLOCK_M': 64, 'BLOCK_N': 256, 'BLOCK_K': 128, 'SPLIT_K': 1}, num_stages=4, num_warps=4), Config({'BLOCK_M': 128, 'BLOCK_N': 128, 'BLOCK_K': 128, 'SPLIT_K': 1}, num_stages=4, num_warps=4), Config({'BLOCK_M': 128, 'BLOCK_N': 64, 'BLOCK_K': 64, 'SPLIT_K': 1}, num_stages=4, num_warps=4), Config({'BLOCK_M': 64, 'BLOCK_N': 128, 'BLOCK_K': 64, 'SPLIT_K': 1}, num_stages=4, num_warps=4), Config({'BLOCK_M': 128, 'BLOCK_N': 32, 'BLOCK_K': 64, 'SPLIT_K': 1}, num_stages=4, num_warps=4), Config({'BLOCK_M': 64, 'BLOCK_N': 32, 'BLOCK_K': 64, 'SPLIT_K': 1}, num_stages=5, num_warps=2), ] + get_configs_io_bound(), key=['M', 'N', 'K'], prune_configs_by={ 'early_config_prune': early_config_prune, 'perf_model': estimate_matmul_time, 'top_k': 10 }, ) @heuristics({ 'EVEN_K': lambda args: args['K'] % (args['BLOCK_K'] * args['SPLIT_K']) == 0, }) @jit def _kernel(A, B, C, M, N, K, stride_am, stride_ak, stride_bk, stride_bn, stride_cm, stride_cn, dot_out_dtype: tl.constexpr, BLOCK_M: tl.constexpr, BLOCK_N: tl.constexpr, BLOCK_K: tl.constexpr, GROUP_M: tl.constexpr, SPLIT_K: tl.constexpr, EVEN_K: tl.constexpr, ): # matrix multiplication pid = tl.program_id(0) pid_z = tl.program_id(1) grid_m = tl.cdiv(M, BLOCK_M) grid_n = tl.cdiv(N, BLOCK_N) # re-order program ID for better L2 performance width = GROUP_M * grid_n group_id = pid // width group_size = min(grid_m - group_id * GROUP_M, GROUP_M) pid_m = group_id * GROUP_M + (pid % group_size) pid_n = (pid % width) // (group_size) # do matrix multiplication rm = pid_m * BLOCK_M + tl.arange(0, BLOCK_M) rn = pid_n * BLOCK_N + tl.arange(0, BLOCK_N) ram = tl.max_contiguous(tl.multiple_of(rm % M, BLOCK_M), BLOCK_M) rbn = tl.max_contiguous(tl.multiple_of(rn % N, BLOCK_N), BLOCK_N) rk = pid_z * BLOCK_K + tl.arange(0, BLOCK_K) # pointers A = A + (ram[:, None] * stride_am + rk[None, :] * stride_ak) B = B + (rk[:, None] * stride_bk + rbn[None, :] * stride_bn) acc = tl.zeros((BLOCK_M, BLOCK_N), dtype=dot_out_dtype) for k in range(0, tl.cdiv(K, BLOCK_K * SPLIT_K)): if EVEN_K: a = tl.load(A) b = tl.load(B) else: k_remaining = K - k * (BLOCK_K * SPLIT_K) _0 = tl.zeros((1, 1), dtype=C.dtype.element_ty) a = tl.load(A, mask=rk[None, :] < k_remaining, other=_0) b = tl.load(B, mask=rk[:, None] < k_remaining, other=_0) a = a.to(C.dtype.element_ty) b = b.to(C.dtype.element_ty) acc += tl.dot(a, b, out_dtype=dot_out_dtype) A += BLOCK_K * SPLIT_K * stride_ak B += BLOCK_K * SPLIT_K * stride_bk acc = acc.to(C.dtype.element_ty) # rematerialize rm and rn to save registers rm = pid_m * BLOCK_M + tl.arange(0, BLOCK_M) rn = pid_n * BLOCK_N + tl.arange(0, BLOCK_N) C = C + (rm[:, None] * stride_cm + rn[None, :] * stride_cn) mask = (rm < M)[:, None] & (rn < N)[None, :] # handles write-back with reduction-splitting if SPLIT_K == 1: tl.store(C, acc, mask=mask) else: tl.atomic_add(C, acc, mask=mask) class _matmul(torch.autograd.Function): kernel = _kernel _locks = {} @staticmethod def _call(a, b, dot_out_dtype): device = a.device # handle non-contiguous inputs if necessary if a.stride(0) > 1 and a.stride(1) > 1: a = a.contiguous() if b.stride(0) > 1 and b.stride(1) > 1: b = b.contiguous() # checks constraints assert a.shape[1] == b.shape[0], "incompatible dimensions" M, K = a.shape _, N = b.shape # allocates output if a.dtype in [tl.float8e4, tl.float8e4b15, tl.float8e5] or\ b.dtype in [tl.float8e4, tl.float8e4b15, tl.float8e5]: c_dtype = torch.float16 else: c_dtype = get_higher_dtype(a.dtype, b.dtype) c = torch.empty((M, N), device=device, dtype=c_dtype) if dot_out_dtype is None: if c_dtype in [torch.float16, torch.float32, torch.bfloat16]: dot_out_dtype = tl.float32 else: dot_out_dtype = tl.int32 else: assert isinstance(dot_out_dtype, torch.dtype), "dot_out_dtype must be a torch.dtype" if dot_out_dtype == torch.float16: dot_out_dtype = tl.float16 elif dot_out_dtype in [torch.float32, torch.bfloat16]: dot_out_dtype = tl.float32 else: dot_out_dtype = tl.int32 # launch kernel grid = lambda META: (cdiv(M, META['BLOCK_M']) * cdiv(N, META['BLOCK_N']), META['SPLIT_K']) _kernel[grid](a, b, c, M, N, K, a.stride(0), a.stride(1), b.stride(0), b.stride(1), c.stride(0), c.stride(1), dot_out_dtype=dot_out_dtype, GROUP_M=8) return c @staticmethod def forward(ctx, a, b, dot_out_dtype=None): return _matmul._call(a, b, dot_out_dtype=dot_out_dtype) matmul = _matmul.apply
8,026
41.696809
127
py
triton
triton-main/python/triton/ops/matmul_perf_model.py
import heapq import torch from .. import cdiv from .._C.libtriton.triton import runtime from ..runtime import driver from ..testing import get_dram_gbps, get_max_simd_tflops, get_max_tensorcore_tflops def get_tensorcore_tflops(backend, device, num_ctas, num_warps, dtype): ''' return compute throughput in TOPS ''' total_warps = num_ctas * min(num_warps, 4) num_subcores = driver.utils.get_device_properties(device)["multiprocessor_count"] * 4 # on recent GPUs tflops = min(num_subcores, total_warps) / num_subcores * get_max_tensorcore_tflops(dtype, backend, device) return tflops def get_simd_tflops(backend, device, num_ctas, num_warps, dtype): ''' return compute throughput in TOPS ''' total_warps = num_ctas * min(num_warps, 4) num_subcores = driver.utils.get_device_properties(device)["multiprocessor_count"] * 4 # on recent GPUs tflops = min(num_subcores, total_warps) / num_subcores * get_max_simd_tflops(dtype, backend, device) return tflops def get_tflops(backend, device, num_ctas, num_warps, dtype): capability = torch.cuda.get_device_capability(device) if capability[0] < 8 and dtype == torch.float32: return get_simd_tflops(backend, device, num_ctas, num_warps, dtype) return get_tensorcore_tflops(backend, device, num_ctas, num_warps, dtype) def estimate_matmul_time( # backend, device, num_warps, num_stages, A, B, C, M, N, K, BLOCK_M, BLOCK_N, BLOCK_K, SPLIT_K, debug=False, **kwargs ): ''' return estimated running time in ms = max(compute, loading) + store ''' backend = runtime.backend.CUDA device = torch.cuda.current_device() dtype = A.dtype dtsize = A.element_size() num_cta_m = cdiv(M, BLOCK_M) num_cta_n = cdiv(N, BLOCK_N) num_cta_k = SPLIT_K num_ctas = num_cta_m * num_cta_n * num_cta_k # If the input is smaller than the block size M, N = max(M, BLOCK_M), max(N, BLOCK_N) # time to compute total_ops = 2 * M * N * K / (1024 * 1024 * 1024) # GOPS tput = get_tflops(backend, device, num_ctas, num_warps, dtype) compute_ms = total_ops / tput # time to load data num_sm = driver.utils.get_device_properties(device)["multiprocessor_count"] active_cta_ratio = min(1, num_ctas / num_sm) active_cta_ratio_bw1 = min(1, num_ctas / 32) # 32 active ctas are enough to saturate active_cta_ratio_bw2 = max(min(1, (num_ctas - 32) / (108 - 32)), 0) # 32-108, remaining 5% dram_bw = get_dram_gbps(backend, device) * (active_cta_ratio_bw1 * 0.95 + active_cta_ratio_bw2 * 0.05) # in GB/s l2_bw = dram_bw * 4 # rough estimation (should be 4.7 for A100?) # assume 80% of (following) loads are in L2 cache load_a_dram = M * K * dtsize * (1 + 0.2 * (num_cta_n - 1)) load_a_l2 = M * K * dtsize * 0.8 * (num_cta_n - 1) load_b_dram = N * K * dtsize * (1 + 0.2 * (num_cta_m - 1)) load_b_l2 = N * K * dtsize * 0.8 * (num_cta_m - 1) # total total_dram = (load_a_dram + load_b_dram) / (1024 * 1024) # MB total_l2 = (load_a_l2 + load_b_l2) / (1024 * 1024) # loading time in ms load_ms = total_dram / dram_bw + total_l2 / l2_bw # estimate storing time store_bw = dram_bw * 0.6 # :o store_c_dram = M * N * dtsize * SPLIT_K / (1024 * 1024) # MB if SPLIT_K == 1: store_ms = store_c_dram / store_bw else: reduce_bw = store_bw store_ms = store_c_dram / reduce_bw # c.zero_() zero_ms = M * N * 2 / (1024 * 1024) / store_bw store_ms += zero_ms total_time_ms = max(compute_ms, load_ms) + store_ms if debug: print(f'Total time: {total_time_ms}ms, compute time: {compute_ms}ms, ' f'loading time: {load_ms}ms, store time: {store_ms}ms, ' f'Activate CTAs: {active_cta_ratio*100}%') return total_time_ms def early_config_prune(configs, named_args): device = torch.cuda.current_device() capability = torch.cuda.get_device_capability() # BLOCK_M, BLOCK_N, BLOCK_K, SPLIT_K, num_warps, num_stages dtsize = named_args['A'].element_size() dtype = named_args['A'].dtype # 1. make sure we have enough smem pruned_configs = [] for config in configs: kw = config.kwargs BLOCK_M, BLOCK_N, BLOCK_K, num_stages = \ kw['BLOCK_M'], kw['BLOCK_N'], kw['BLOCK_K'], config.num_stages max_shared_memory = driver.utils.get_device_properties(device)["max_shared_mem"] required_shared_memory = (BLOCK_M + BLOCK_N) * BLOCK_K * num_stages * dtsize if required_shared_memory <= max_shared_memory: pruned_configs.append(config) configs = pruned_configs # Some dtypes do not allow atomic_add if dtype not in [torch.float16, torch.float32]: configs = [config for config in configs if config.kwargs['SPLIT_K'] == 1] # group configs by (BLOCK_M,_N,_K, SPLIT_K, num_warps) configs_map = {} for config in configs: kw = config.kwargs BLOCK_M, BLOCK_N, BLOCK_K, SPLIT_K, num_warps, num_stages = \ kw['BLOCK_M'], kw['BLOCK_N'], kw['BLOCK_K'], kw['SPLIT_K'], config.num_warps, config.num_stages key = (BLOCK_M, BLOCK_N, BLOCK_K, SPLIT_K, num_warps) if key in configs_map: configs_map[key].append((config, num_stages)) else: configs_map[key] = [(config, num_stages)] pruned_configs = [] for k, v in configs_map.items(): BLOCK_M, BLOCK_N, BLOCK_K, SPLIT_K, num_warps = k if capability[0] >= 8: # compute cycles (only works for ampere GPUs) mmas = BLOCK_M * BLOCK_N * BLOCK_K / (16 * 8 * 16) mma_cycles = mmas / min(4, num_warps) * 8 ldgsts_latency = 300 # Does this matter? optimal_num_stages = ldgsts_latency / mma_cycles # nearest stages, prefer large #stages nearest = heapq.nsmallest(2, v, key=lambda x: 10 + abs(x[1] - optimal_num_stages) if (x[1] - optimal_num_stages) < 0 else x[1] - optimal_num_stages) for n in nearest: pruned_configs.append(n[0]) else: # Volta & Turing only supports num_stages <= 2 random_config = v[0][0] random_config.num_stages = 2 pruned_configs.append(random_config) return pruned_configs
6,369
39.062893
117
py
triton
triton-main/python/triton/ops/blocksparse/softmax.py
import torch from ... import jit from ... import language as tl from ... import next_power_of_2 def num_warps(n): if n <= 128: return 1 if n <= 256: return 2 if n <= 512: return 4 if n <= 4096: return 8 return 16 @jit def _blocksparse_softmax_fwd( Out, A, stride_xz, LUT, R, extent, stride_zr, stride_hr, # relative attention scale, is_causal, ROW_SIZE: tl.constexpr, BLOCK_SIZE: tl.constexpr, IS_DENSE: tl.constexpr, ): h = tl.program_id(0) m = tl.program_id(1) z = tl.program_id(2) # create index ranges hm = h * tl.num_programs(1) + m lane_n = tl.arange(0, ROW_SIZE) % BLOCK_SIZE block_n = tl.arange(0, ROW_SIZE) // BLOCK_SIZE # extract information from LUT header = LUT + (hm // BLOCK_SIZE) * 2 size = tl.load(header + 0) offset = tl.load(header + 1) # pointer offset off_a = z * stride_xz off_a += (offset + block_n) * BLOCK_SIZE * BLOCK_SIZE # block indx off_a += (m % BLOCK_SIZE) * BLOCK_SIZE # row indx # do not need to read column indices in the dense case if IS_DENSE: ns = tl.arange(0, ROW_SIZE) else: off_lut = offset + 2 * tl.num_programs(0) * tl.num_programs(1) // BLOCK_SIZE start_n = tl.load(LUT + off_lut + block_n, mask=block_n < size, other=0) ns = start_n * BLOCK_SIZE + lane_n # load X mask = block_n < size a = tl.load(A + off_a + lane_n, mask=mask, other=-float("inf")) a = a.to(tl.float32) # compute out = a out *= scale # apply relative attention if R is not None: R += z * stride_zr R += h * stride_hr off_lo = (extent - m - 1) + ns mask_lo = (off_lo >= 0) & (off_lo < extent) rel_logits = tl.load(R + m * extent + off_lo, mask=mask_lo, other=0.0) out += rel_logits out = out.to(tl.float32) # apply causal mask out = tl.where((ns > m) & is_causal, -float("inf"), out) # computation out = tl.softmax(out) # write-back tl.store(Out + off_a + lane_n, out, mask=mask) @jit def _blocksparse_softmax_bwd( DA, stride_zdx, DOut, stride_zdout, Out, stride_zout, scale, LUT, DR, extent, stride_zr, stride_hr, stride_er, is_causal, ROW_SIZE: tl.constexpr, BLOCK_SIZE: tl.constexpr, IS_DENSE: tl.constexpr, ): h = tl.program_id(0) m = tl.program_id(1) z = tl.program_id(2) # create index ranges hm = h * tl.num_programs(1) + m lane_n = tl.arange(0, ROW_SIZE) % BLOCK_SIZE block_n = tl.arange(0, ROW_SIZE) // BLOCK_SIZE # extract information from LUT header = LUT + (hm // BLOCK_SIZE) * 2 size = tl.load(header + 0) offset = tl.load(header + 1) # row-col offset off_mn = (offset + block_n) * BLOCK_SIZE * BLOCK_SIZE off_mn += (m % BLOCK_SIZE) * BLOCK_SIZE mask = block_n < size # pointers As = Out + z * stride_zout + off_mn DOuts = DOut + z * stride_zdout + off_mn # do not need to read column indices in the dense case if IS_DENSE: ns = tl.arange(0, ROW_SIZE) else: off_lut = offset + 2 * tl.num_programs(0) * tl.num_programs(1) // BLOCK_SIZE start_n = tl.load(LUT + off_lut + block_n, mask=mask, other=0) ns = start_n * BLOCK_SIZE + lane_n # load data a = tl.load(As + lane_n, mask=mask, other=0.0) a = a.to(tl.float32) dout = tl.load(DOuts + lane_n, mask=mask, other=0.0) dout = dout.to(tl.float32) # compute a = tl.where((ns > m) & is_causal & (a == a), 0., a) da = a * (dout - tl.sum(a * dout, 0)) # apply relative attention if DR is not None: DR += z * stride_zr DR += h * stride_hr off_lo = (extent - m - 1) + ns mask_lo = (off_lo >= 0) & (off_lo < extent) & mask tl.store(DR + m * extent + off_lo, da, mask=mask_lo) da = da * scale # convert da # write-back DAs = DA + z * stride_zdx + off_mn tl.store(DAs + lane_n, da, mask=mask) class _softmax(torch.autograd.Function): @staticmethod def make_lut(layout, block, device): _empty = torch.tensor([], dtype=torch.int64, device=layout.device) sizes = _empty.clone() # sizes along rows for h in range(layout.shape[0]): sizes = torch.cat((sizes, layout[h, :, :].sum(-1))) total_sizes = sizes * block # offsets in block format offsets = torch.zeros_like(sizes) offsets[1:] = torch.cumsum(sizes[:-1], dim=0) # block indices columns = layout.nonzero(as_tuple=False)[:, 2] header = torch.stack((sizes, offsets), dim=1).view(-1) lut = torch.cat((header, columns)).type(torch.int32).to(device) return lut, int(total_sizes.max()) @staticmethod def forward( ctx, a, scale, rel_logits, is_causal, spdims, block, lut, maxlut, is_dense ): if scale is not None and isinstance(scale, torch.Tensor): assert scale.device.type == "cpu" scale = scale.item() M = a.shape[0] grid = [spdims[0], spdims[1] * block, M] rel_shape = (1, 1, 1, 1) if rel_logits is None else rel_logits.shape rel_strides = (1, 1, 1, 1) if rel_logits is None else rel_logits.stride() # enqueue kernel out = torch.empty_like(a) _blocksparse_softmax_fwd[grid]( out, a, a.stride(0), lut, rel_logits, rel_shape[-1], rel_strides[0], rel_strides[1], # relative attn scale, is_causal, BLOCK_SIZE=block, ROW_SIZE=next_power_of_2(maxlut), IS_DENSE=is_dense, num_warps=num_warps(maxlut) ) # save to context # ctx.mark_dirty(x) ctx.save_for_backward(out, lut) ctx.spdims = spdims ctx.block = block ctx.maxlut = maxlut ctx.scale = scale ctx.rel_shape = rel_shape ctx.rel_strides = rel_strides ctx.rel_dtype = a.dtype ctx.is_dense = is_dense ctx.is_causal = is_causal return out @staticmethod def backward(ctx, dout): # retrieve from context out, lut = ctx.saved_tensors # relative logits gradients dr = None if ctx.needs_input_grad[3]: dr = torch.zeros(ctx.rel_shape, dtype=ctx.rel_dtype, device=out.device) # run kernel M = out.shape[0] grid = (ctx.spdims[0], ctx.spdims[1] * ctx.block, M) da = torch.empty_like(dout) _blocksparse_softmax_bwd[grid]( da, da.stride(0), dout, dout.stride(0), out, out.stride(0), ctx.scale, lut, dr, ctx.rel_shape[-1], ctx.rel_strides[0], ctx.rel_strides[1], ctx.rel_strides[2], ctx.is_causal, BLOCK_SIZE=ctx.block, ROW_SIZE=next_power_of_2(ctx.maxlut), IS_DENSE=ctx.is_dense, num_warps=num_warps(ctx.maxlut) ) return (da, None, None, dr, None, None, None, None, None, None, None, None, None, None, None, None, None, None ) class softmax: def __init__(self, layout, block, device, is_dense=False): self.spdims = layout.shape self.layout = layout self.block = block self.lut, self.maxlut = _softmax.make_lut(self.layout, self.block, device) self.is_dense = is_dense def __call__(self, a, *, scale=1.0, rel_logits=None, is_causal=False): if rel_logits is not None and rel_logits.dtype != a.dtype: raise ValueError(f"relative position embedding must be {a.dtype}") a = _softmax.apply( a, scale, rel_logits, is_causal, self.spdims, self.block, self.lut, self.maxlut, self.is_dense, ) return a
7,905
31.804979
94
py
triton
triton-main/python/triton/ops/blocksparse/matmul.py
import torch from ... import cdiv, heuristics, jit from ... import language as tl # ******************************************************** # -------------------------------------------------------- # Sparse = Dense x Dense (SDD) # This operation uses super-blocking to make sure that # it's done efficiently when small blocks can be grouped # together # -------------------------------------------------------- # ******************************************************** @heuristics({ 'EVEN_K': lambda nargs: nargs['K'] % nargs['TILE_K'] == 0, }) @jit def _sdd_kernel( A, B, C, stride_za, stride_ha, stride_ma, stride_ak, stride_zb, stride_hb, stride_bk, stride_nb, stride_zc, stride_hc, stride_mc, stride_nc, K, grid_offset, lut, TILE_M: tl.constexpr, TILE_N: tl.constexpr, TILE_K: tl.constexpr, BLOCK: tl.constexpr, EVEN_K: tl.constexpr ): # ------------ # # - Prologue - # # ------------ # block_id = tl.program_id(0) + grid_offset lut += block_id * 3 # offsets off_z = tl.program_id(2) # batch off_h = tl.load(lut + 0) # head # initialize pointers to A start_am = tl.load(lut + 1) offs_am = start_am * BLOCK + (tl.arange(0, TILE_M) % BLOCK) offs_ak = tl.arange(0, TILE_K) a_ptrs = A \ + off_z * stride_za \ + off_h * stride_ha \ + offs_am[:, None] * stride_ma \ + offs_ak[None, :] * stride_ak # initialize pointers to B start_bn = tl.load(lut + 2) offs_bn = start_bn * BLOCK + (tl.arange(0, TILE_N) % BLOCK) offs_bk = tl.arange(0, TILE_K) b_ptrs = B \ + off_z * stride_zb \ + off_h * stride_hb \ + offs_bn[None, :] * stride_nb \ + offs_bk[:, None] * stride_bk # ---------------- # # Inner Loop # # ---------------- # acc = tl.zeros((TILE_M, TILE_N), dtype=tl.float32) for k in range(K, 0, -TILE_K): if EVEN_K: a = tl.load(a_ptrs) b = tl.load(b_ptrs) else: a = tl.load(a_ptrs, mask=offs_ak[None, :] < k, other=0.) b = tl.load(b_ptrs, mask=offs_bk[:, None] < k, other=0.) acc += tl.dot(a, b, out_dtype=tl.float32) a_ptrs += TILE_K * stride_ak b_ptrs += TILE_K * stride_bk c = acc.to(C.dtype.element_ty) # ---------------- # # Epilogue # # ---------------- # offs_cm = tl.arange(0, TILE_M) % BLOCK offs_cn = tl.arange(0, TILE_N) % BLOCK pc = C \ + off_z * stride_zc \ + block_id * stride_hc \ + offs_cm[:, None] * stride_mc \ + offs_cn[None, :] * stride_nc tl.store(pc, c, mask=True) def sdd_matmul(a, b, trans_a, trans_b, trans_c, spdims, block, lut, widths, out=None): if a.stride(2) != 1 and a.stride(3) != 1: a = a.contiguous() if b.stride(2) != 1 and b.stride(3) != 1: b = b.contiguous() # (A * B)^T = B^T * A^T if trans_c: a, b = b, a trans_a, trans_b = not trans_b, not trans_a # shape constraints a_dim = -2 if trans_a else -1 b_dim = -1 if trans_b else -2 Ka, Kb = a.shape[a_dim], b.shape[b_dim] if Ka != Kb: raise ValueError(f"Inner dimension mismatch (A: {Ka} vs B: {Kb})") # allocate output if out is None: c = torch.empty((a.shape[0], lut.shape[0], block, block), dtype=a.dtype, device=a.device) else: assert out.shape == (a.shape[0], lut.shape[0], block, block) c = out grid = [c.shape[1], 1, c.shape[0]] _sdd_kernel[grid]( a, b, c, a.stride(0), a.stride(1), a.stride(3 if trans_a else 2), a.stride(2 if trans_a else 3), b.stride(0), b.stride(1), b.stride(3 if trans_b else 2), b.stride(2 if trans_b else 3), c.stride(0), c.stride(1), c.stride(2), c.stride(3), Ka, 0, lut, TILE_M=block, TILE_N=block, TILE_K=32, BLOCK=block, num_stages=4, num_warps=4, ) return c def sdd_lut(layout, block, device): lut = layout.nonzero(as_tuple=False).to(device).int() lut = lut.contiguous() return lut, None # ----------------------------- # Dense = Sparse x Dense (DSD) # This operation uses a look-up table that contains pre-computed pointer increments # in order to minimize computations in the inner loop of the matmul kernel. # ----------------------------- @jit def _dsd_kernel( A, B, C, stride_az, stride_ha, stride_am, stride_ak, stride_zb, stride_hb, stride_bk, stride_bn, stride_zc, stride_hc, stride_cm, stride_cn, DS0, DS1, lut, TILE_M: tl.constexpr, TILE_N: tl.constexpr, TILE_K: tl.constexpr, GROUP_SIZE_M: tl.constexpr, BLOCK: tl.constexpr ): # ------------ # # - Prologue - # # ------------ # pid_m = tl.program_id(0) pid_n = tl.program_id(1) num_pid_m = tl.num_programs(0) num_pid_n = tl.num_programs(1) pid_n, pid_m = tl.swizzle2d(pid_n, pid_m, num_pid_n, num_pid_m, GROUP_SIZE_M) pidz = tl.program_id(2) header = lut + pid_n * 4 offset = tl.load(header + 0) K = tl.load(header + 1) column = tl.load(header + 2) off_h = tl.load(header + 3) pinc = lut + offset # initialize pointers to A (sparse) block_id = tl.load(pinc + 1) block_id = tl.multiple_of(block_id, 8) # compiler hint offs_am = tl.arange(0, TILE_M) offs_ak = tl.arange(0, TILE_K) pa = A + pidz * stride_az \ + block_id * stride_ha \ + offs_am[:, None] * stride_am \ + offs_ak[None, :] * stride_ak # initialize pointers to B (dense) offs_bn = pid_m * TILE_N + tl.arange(0, TILE_N) offs_bn = tl.max_contiguous(tl.multiple_of(offs_bn % DS0, TILE_N), TILE_N) start_bk = tl.load(pinc) start_bk = tl.multiple_of(start_bk, 8) # compiler hint offs_bk = start_bk + tl.arange(0, TILE_K) pb = B + pidz * stride_zb \ + off_h * stride_hb \ + offs_bn[None, :] * stride_bn \ + offs_bk[:, None] * stride_bk # ---------------- # # Inner Loop # # ---------------- # acc = tl.zeros((TILE_M, TILE_N), dtype=tl.float32) pinc += 2 inc_a = tl.load(pinc + 1) inc_a = tl.multiple_of(inc_a, 8) inc_b = tl.load(pinc) inc_b = tl.multiple_of(inc_b, 8) for k in range(K, 0, -TILE_K): a = tl.load(pa) b = tl.load(pb) acc += tl.dot(a, b, out_dtype=tl.float32) pa += inc_a pb += inc_b * stride_bk pinc += 2 inc_a = tl.load(pinc + 1) inc_a = tl.multiple_of(inc_a, 8) inc_b = tl.load(pinc) inc_b = tl.multiple_of(inc_b, 8) c = acc.to(C.dtype.element_ty) # initialize pointers to C offs_cm = column * TILE_M + tl.arange(0, TILE_M) offs_cn = pid_m * TILE_N + tl.arange(0, TILE_N) pc = C \ + off_h * stride_hc \ + pidz * stride_zc \ + offs_cm[:, None] * stride_cm \ + offs_cn[None, :] * stride_cn tl.store(pc, c, mask=offs_cn[None, :] < DS0) def dsd_matmul(a, b, trans_a, trans_b, trans_c, spdims, block, lut, width, out=None): if a.stride(2) != 1 and a.stride(3) != 1: a = a.contiguous() if b.stride(2) != 1 and b.stride(3) != 1: b = b.contiguous() # shapes / dtypes AS1 = block * spdims[2 if trans_a else 1] BS0 = b.size(0) BS1 = b.size(1) BS3 = b.size(2 if trans_b else 3) dtype = a.dtype # allocate output CS0 = BS0 CS1 = BS1 CS2 = BS3 if trans_c else AS1 CS3 = AS1 if trans_c else BS3 if out is None: c = torch.empty((CS0, CS1, CS2, CS3), dtype=dtype, device=a.device) else: assert out.shape == (CS0, CS1, CS2, CS3) c = out # meta-parameter heuristics TILE_N = 128 # compute output grid = lambda meta: [cdiv(BS3, meta['TILE_N']), width, BS0] _dsd_kernel[grid]( a, b, c, a.stride(0), a.stride(1), a.stride(3 if trans_a else 2), a.stride(2 if trans_a else 3), b.stride(0), b.stride(1), b.stride(3 if trans_b else 2), b.stride(2 if trans_b else 3), c.stride(0), c.stride(1), c.stride(3 if trans_c else 2), c.stride(2 if trans_c else 3), BS3, AS1, lut, TILE_M=block, TILE_N=TILE_N, TILE_K=min(block, 32), BLOCK=block, num_stages=4, num_warps=4, GROUP_SIZE_M=4, ) # exit() return c def dsd_lut(layout, block, step, trans, device): """ Generates the look-up table for incrementing pointers in the DSD/DDS matmul. Example (BLOCK=32, STEP=16) [[1, 0, 0, 1, 0], [0, 1, 1, 0, 1], [1, 0, 1, 0, 0]] Then the offsets for A are [0 , 16, 32, 48] <- row 0 \\----/ \\----/ col=0 col=3 [64, 80, 96, 112, 128, 144] <- row 1 \\----/ \\----/ \\------/ col=1 col=2 col=3 [160, 176, 192, 208] which leads to increments table [0, 16, 16, 16, || 64, 16, 16, 16, 16, 16, || 160, 16, 16, 16] Because B is dense, the offsets are [0, 16, 96, 112] <- row 0 [32, 48, 64, 80] <- row 1 [0, 16, 64, 80] <- row 2 """ sizes = torch.sum(layout, 2 if trans else 1) head_id, col_id = torch.ones_like(sizes).nonzero(as_tuple=True) sizes = sizes.flatten() segments = sizes * step # pointer increments if trans: nnz = layout.nonzero(as_tuple=False) else: nnz = layout.transpose(1, 2).nonzero(as_tuple=False) num_blocks = nnz.size(0) offsets = torch.zeros_like(sizes) offsets[1:] = torch.cumsum(sizes[:-1], dim=0) offsets = torch.min(offsets, (num_blocks - 1) * torch.ones_like(offsets)) # ------------------------------- # dense input pointer increments # ------------------------------- # Note that the inner loop matmul kernel may have a fixed step size (e.g., TILE_K) # that is smaller than the block size, so we need to do a bit of extra work # to handle this case B_idx = nnz[:, 2] * block B_incs = B_idx.clone() B_incs[1:] -= B_idx[:-1] div = block // step B_incs = B_incs.view(-1, 1).repeat(1, div) B_incs[:, 1:] = step B_incs[:, 0] -= (div - 1) * step # first increment for each reduction is actually the offset B_incs[offsets[segments > 0], 0] = B_idx[offsets[segments > 0]] B_incs = B_incs.view(-1) # ------------------------------- # sparse input pointer increments # ------------------------------- # same as above, except that the increments are in the sparse memory layout if trans: A_idx = torch.arange(num_blocks, device=layout.device) else: A_idx = torch.tensor([], dtype=torch.int64, device=layout.device) current_offset = 0 for z in range(layout.size(0)): layoutw = layout[z, :, :].clone().long() msum = layoutw.sum() layoutw[layoutw > 0] = 1 + torch.arange(msum, device=layout.device) A_idx = torch.cat((A_idx, current_offset + layoutw.T[layoutw.T > 0] - 1)) current_offset += msum A_incs = A_idx * block * block A_incs[1:] -= A_idx[:-1] * block * block A_incs = A_incs.view(-1, 1).repeat(1, div) if trans: A_incs[:, 1:] = step A_incs[:, 0] -= (div - 1) * step else: A_incs[:, 1:] = step * block A_incs[:, 0] -= (div - 1) * step * block A_incs[offsets[segments > 0], 0] = A_idx[offsets[segments > 0]] A_incs = A_incs.view(-1) # create header width = col_id.size(0) offsets = offsets * 2 * div + 4 * width segments = segments * div header = torch.stack((offsets, segments, col_id, head_id), dim=1).view(-1).contiguous() # create increments incs = torch.stack((B_incs, A_incs), dim=1).view(-1).contiguous() # pad by a factor 2*MAX_NUM_STAGES # to accommodate pre-fetching inside the kernel pad = torch.zeros(20, device=incs.device, dtype=incs.dtype) incs = torch.cat((incs, pad)) # create lut lut = torch.cat((header, incs)) lut = lut.type(torch.int32).to(device) # create locks return lut, width # ----------------------------- # Dense = Dense x Sparse (DDS) # ----------------------------- # AB = (B^T A^T)^T def dds_matmul(a, b, trans_a, trans_b, trans_c, spdims, block, lut, width, out=None): return dsd_matmul(b, a, not trans_b, not trans_a, not trans_c, spdims, block, lut, width, out=out) ############## # MAIN API # ############## class _matmul(torch.autograd.Function): fn = {'sdd': sdd_matmul, 'dsd': dsd_matmul, 'dds': dds_matmul} @staticmethod def forward( ctx, a, b, trans_a, trans_b, trans_c, mode, spdims, block, c_lut, c_width, da_lut, da_width, db_lut, db_width, out ): c = _matmul.fn[mode](a, b, trans_a, trans_b, trans_c, spdims, block, c_lut, c_width, out=out) # save for backward ctx.save_for_backward(a, b) ctx.da_lut = da_lut ctx.da_width = da_width ctx.db_lut = db_lut ctx.db_width = db_width ctx.mode = mode ctx.spdims = spdims ctx.block = block ctx.trans_a = trans_a ctx.trans_b = trans_b ctx.trans_c = trans_c ctx.has_out = out is not None return c @staticmethod def backward(ctx, dc): # saved for backward a, b = ctx.saved_tensors da, db = None, None mode = ctx.mode # gradients w.r.t. a if ctx.needs_input_grad[0]: mode_da = mode[1] + mode[0] + mode[2] da = _matmul.fn[mode_da]( dc, b, ctx.trans_c, not ctx.trans_b, ctx.trans_a, ctx.spdims, ctx.block, ctx.da_lut, ctx.da_width, ) # gradients w.r.t. b if ctx.needs_input_grad[1]: mode_db = mode[2] + mode[1] + mode[0] db = _matmul.fn[mode_db]( a, dc, not ctx.trans_a, ctx.trans_c, ctx.trans_b, ctx.spdims, ctx.block, ctx.db_lut, ctx.db_width, ) dout = dc if ctx.has_out else None return da, db, None, None, None,\ None, None, None, None,\ None, None, None, None, None, dout class matmul: def __init__(self, layout, block, mode, device, trans_a=False, trans_b=False, trans_c=False): if mode not in ['sdd', 'dsd', 'dds']: raise NotImplementedError('Supported modes are: sdd, dsd, dds') self.block = block self.mode = mode self.trans_a = trans_a self.trans_b = trans_b self.trans_c = trans_c self.layout = layout self.spdims = layout.shape step = min(block, 32) if self.mode == 'sdd': self.c_lut, self.c_width = sdd_lut(layout, block, device) self.da_lut, self.da_width = dsd_lut(layout, block, step, True, device) self.db_lut, self.db_width = dsd_lut(layout, block, step, False, device) if self.mode == 'dsd': self.c_lut, self.c_width = dsd_lut(layout, block, step, not self.trans_a, device) self.da_lut, self.da_width = sdd_lut(layout, block, device) self.db_lut, self.db_width = dsd_lut(layout, block, step, self.trans_a, device) if self.mode == 'dds': self.c_lut, self.c_width = dsd_lut(layout, block, step, self.trans_b, device) self.da_lut, self.da_width = dsd_lut(layout, block, step, not self.trans_b, device) self.db_lut, self.db_width = sdd_lut(layout, block, device) def __call__(self, a, b, out=None): c = _matmul.apply( a, b, self.trans_a, self.trans_b, self.trans_c, self.mode, self.spdims, self.block, self.c_lut, self.c_width, self.da_lut, self.da_width, self.db_lut, self.db_width, out ) return c
15,615
34.652968
114
py
G-PATE
G-PATE-master/rdp_utils.py
import numpy as np import math import sys from sklearn.preprocessing import normalize from pate_core import * from numpy import linalg as LA EPS = sys.float_info.epsilon # Algorithm 1 in 'Scalable Private Learning with PATE' def gnmax_thresh_aggregator(counts, thresh_cnt, sigma_thresh, sigma, orders): log_pr_answered = compute_logpr_answered(thresh_cnt, sigma_thresh, counts) rdp_budget = compute_rdp_threshold(log_pr_answered, sigma_thresh, orders) # print("Threshold budget:" + str(rdp_budget)) if np.random.normal(np.max(counts), sigma_thresh) >= thresh_cnt: logq = compute_logq_gaussian(counts, sigma) res = np.argmax(np.random.normal(counts, sigma)) g_rdp_budget = rdp_gaussian(logq, sigma, orders) rdp_budget += g_rdp_budget else: # do not return result if teacher models do not agree res = -1 return res, rdp_budget def gnmax_aggregator(counts, sigma, orders): logq = compute_logq_gaussian(counts, sigma) dir_index = np.argmax(np.random.normal(counts, sigma)) rdp_budget = rdp_gaussian(logq, sigma, orders) return dir_index, rdp_budget def rdp_percentile(arr_list, q, orders, vmin, vmax, lmbd, axis=0): arr_length = len(arr_list) arr_size = arr_list[0].size input_shape = arr_list[0].shape arr_reshaped = np.vstack([arr.reshape([1, arr_size]) for arr in arr_list]) arr_ordered = np.sort(arr_reshaped, axis=0) arr_ordered = arr_ordered.clip(min=vmin, max=vmax) arr_ordered_new = np.vstack([np.ones([1, arr_size]) * vmin, arr_ordered, np.ones([1, arr_size]) * vmax]) arr_ordered_new[np.abs(arr_ordered_new) < sys.float_info.epsilon] = 0 n_teachers, n_feature = arr_reshaped.shape arr_prob = np.zeros([n_teachers + 1, n_feature]) for i in range(arr_length + 1): diff = arr_ordered_new[i + 1, :] - arr_ordered_new[i, :] diff = diff.clip(min=0) arr_prob[i] = diff * np.exp(-0.5 / lmbd * abs(i - q / 100 * arr_length)) # arr_prob[i] = np.exp(np.log(diff) - 0.5/lmbd * abs(i - q/100 * arr_length)) # arr_prob = normalize(arr_prob, norm='l1', axis=0) if np.min(arr_prob) < 0: print(arr_prob) exit() low = np.zeros([1, arr_size]) high = np.zeros([1, arr_size]) for i in range(arr_size): prob = arr_prob[:, i] / np.sum(arr_prob[:, i]) rindex = np.random.choice(arr_length + 1, p=prob) # print(rindex) low[0, i] = arr_ordered_new[rindex, i] high[0, i] = arr_ordered_new[rindex + 1, i] output_q = np.random.uniform(low=low, high=high, size=[1, arr_size]) output_q = output_q.reshape(input_shape) rdp_budget = arr_size * np.multiply( 1 / (orders - 1), np.log( np.multiply(np.divide(orders, 2 * orders - 1), np.exp((orders - 1) / lmbd)) \ + np.multiply(np.divide(orders - 1, 2 * orders - 1), np.exp(-orders / lmbd)) ) ) return output_q, rdp_budget def rdp_winsorized_mean(arr_list, step_size, sigma_mean, sigma_percentile, orders, pca_mat=None): vmin = -step_size vmax = step_size flatten_arr = np.asarray([arr.flatten() for arr in arr_list]) n_teachers, n_features = flatten_arr.shape if pca_mat is not None: # project to principal components flatten_arr = np.matmul(flatten_arr, pca_mat) n_features = flatten_arr.shape[1] q25, q25_budget = rdp_percentile(flatten_arr, 25, orders, vmin=vmin, vmax=vmax, lmbd=sigma_percentile) q75, q75_budget = rdp_percentile(flatten_arr, 75, orders, vmin=vmin, vmax=vmax, lmbd=sigma_percentile) arr_mean = np.mean(flatten_arr.clip(min=q25, max=q75), axis=0) arr_mean[np.sign(q75) != np.sign(q25)] = 0 # when 75 percentile is smaller, update the model with the average of 75 and 25 percentile # quantile_mean = (q75 + q25) / 2 arr_mean[q75 < q25] = 0 update_index = np.nonzero(np.logical_and(np.sign(q75) == np.sign(q25), q75 > q25)) q_range = q75 - q25 sensitivity = LA.norm(q_range[update_index] / len(arr_list)) gaussian_noise, mean_budget = gaussian_rdp(arr_mean[update_index], sensitivity, orders, sigma_mean) arr_mean[update_index] += gaussian_noise arr_mean[update_index] = arr_mean[update_index].clip(min=q25[update_index], max=q75[update_index]) # for testing only # update_ratio = gaussian_noise.size / arr_mean.size # print("Update ratio: %.8f, norm: %.8f" % (update_ratio, sensitivity)) rdp_budget = q25_budget + q75_budget + mean_budget if pca_mat is not None: # project res direction back to original axis arr_mean = np.matmul(arr_mean, np.transpose(pca_mat)) return arr_mean.reshape(arr_list[0].shape), rdp_budget def gradient_voting_nonprivate(output_list, step_size, nbins=10): n = len(output_list) flatten_arr = np.asarray([arr.flatten() for arr in output_list]) n_teachers, n_features = flatten_arr.shape flatten_arr = flatten_arr.clip(min=-step_size, max=step_size) bins = np.arange(-step_size, step_size, (step_size * 2 / nbins)) bins = np.hstack([bins, step_size]) result = np.zeros([1, n_features]) for i in range(n_features): votes_arr, _ = np.histogram(flatten_arr[:, i], bins) res_idx = np.argmax(votes_arr) result[:, i] = (bins[res_idx] + bins[res_idx + 1]) / 2 return result.reshape(output_list[0].shape) def gradient_voting_rdp(output_list, step_size, sigma, sigma_thresh, orders, pca_mat=None, nbins=10, thresh=0.9): import time st = time.time() n = len(output_list) use_gpu = False # turn it on if you are running a huge matrix and the bottleneck lies on CPU matmul if use_gpu: # have to use torch==1.2.0 and torchvision==0.4.0 to run tensorflow-gpu==1.4.0 import torch flatten_arr = torch.tensor([arr.flatten() for arr in output_list], device='cuda:0') else: flatten_arr = np.asarray([arr.flatten() for arr in output_list]) n_teachers, n_features = flatten_arr.shape if pca_mat is not None: # project to principal components if use_gpu: pca_mat_tensor = torch.from_numpy(pca_mat).float().to('cuda:0') flatten_arr = torch.matmul(flatten_arr, pca_mat_tensor) flatten_arr = flatten_arr.cpu().numpy() else: flatten_arr = np.matmul(flatten_arr, pca_mat) n_features = flatten_arr.shape[1] flatten_arr = flatten_arr.clip(min=-step_size, max=step_size) bins = np.arange(-step_size, step_size, (step_size * 2 / nbins)) bins = np.hstack([bins, step_size]) result = np.zeros([1, n_features]) rdp_budget = 0 skipped_cnt = 0 for i in range(n_features): votes_arr, _ = np.histogram(flatten_arr[:, i], bins) print(votes_arr) res_idx, cur_budget = gnmax_thresh_aggregator(votes_arr, thresh * n_teachers, sigma_thresh, sigma, orders) rdp_budget += cur_budget if res_idx < 0: skipped_cnt += 1 else: result[:, i] = (bins[res_idx] + bins[res_idx + 1]) / 2 print("Skipped %d feaatures out of %d" % (skipped_cnt, n_features)) if pca_mat is not None: # project res direction back to original axis result = np.matmul(result, np.transpose(pca_mat)) return result.reshape(output_list[0].shape), rdp_budget def gradient_voting_rdp_multiproj(output_list, step_size, sigma, sigma_thresh, orders, pca_mats=None, nbins=10, thresh=0.9): n = len(output_list) flatten_arr = np.asarray([arr.flatten() for arr in output_list]) n_teachers, n_features = flatten_arr.shape print("flatten arr shape", flatten_arr.shape) if pca_mats is not None: # project to principal components split_flatten_arr = np.split(flatten_arr, len(pca_mats), axis=1) reduced_flatten_arr = [] for pca_mat, arr in zip(pca_mats, split_flatten_arr): print("arr shape", arr.shape) print("pca shape", pca_mat.shape) arr = np.matmul(arr, pca_mat) reduced_flatten_arr.append(arr) flatten_arr = np.concatenate(reduced_flatten_arr, axis=1) n_features = flatten_arr.shape[1] flatten_arr = flatten_arr.clip(min=-step_size, max=step_size) bins = np.arange(-step_size, step_size, (step_size * 2 / nbins)) bins = np.hstack([bins, step_size]) result = np.zeros([1, n_features]) rdp_budget = 0 skipped_cnt = 0 for i in range(n_features): votes_arr, _ = np.histogram(flatten_arr[:, i], bins) print(votes_arr) res_idx, cur_budget = gnmax_thresh_aggregator(votes_arr, thresh * n_teachers, sigma_thresh, sigma, orders) rdp_budget += cur_budget if res_idx < 0: skipped_cnt += 1 else: result[:, i] = (bins[res_idx] + bins[res_idx + 1]) / 2 print("Skipped %d feaatures out of %d" % (skipped_cnt, n_features)) if pca_mat is not None: # project res direction back to original axis split_results = np.split(result, len(pca_mats), axis=1) final_results = [] for split_result, pca_mat in zip(split_results, pca_mats): final_results.append(np.matmul(split_result, np.transpose(pca_mat))) final_results = np.concatenate(final_results, axis=1) return final_results.reshape(output_list[0].shape), rdp_budget def gradient_sign_rdp(output_list, step_size, sigma, sigma_thresh, orders, pca_mat=None, thresh=0.9): n = len(output_list) flatten_arr = np.asarray([arr.flatten() for arr in output_list]) n_teachers, n_features = flatten_arr.shape if pca_mat is not None: # project to principal components flatten_arr = np.matmul(flatten_arr, pca_mat) n_features = flatten_arr.shape[1] # first line for positive votes, second line for negative votes votes_arr = np.zeros([2, n_features]) votes_sign = np.sign(flatten_arr) # counts for positive votes votes_arr[0, :] = np.sum(votes_sign[votes_sign > 0], axis=0) # counts for negative votes votes_arr[1, :] = -np.sum(votes_sign[votes_sign < 0], axis=0) res_dir = np.zeros([1, n_features]) rdp_budget = 0 skipped_cnt = 0 for i in range(n_features): dir_index, cur_budget = gnmax_thresh_aggregator(votes_arr[:, i], thresh * n_teachers, sigma_thresh, sigma, orders) if dir_index == 0: res_dir[0, i] = step_size elif dir_index == 1: res_dir[0, i] = -step_size else: skipped_cnt += 1 rdp_budget += cur_budget print("Skipped %d feaatures out of %d" % (skipped_cnt, n_features)) if pca_mat is not None: # project res direction back to original axis res_dir = np.matmul(res_dir, np.transpose(pca_mat)) return res_dir.reshape(output_list[0].shape), rdp_budget def gradient_rdp(output_list, step_size, sigma, orders, pca_mat=None, thresh=None, sigma_thresh=1): n = len(output_list) flatten_arr = np.asarray([arr.flatten() for arr in output_list]) n_teachers, n_features = flatten_arr.shape if pca_mat is not None: # project to principal components flatten_arr = np.matmul(flatten_arr, pca_mat) n_features = flatten_arr.shape[1] # first half votes for positive direction, second half votes for negative direction votes_arr = np.zeros([n_teachers, n_features * 2]) max_index = np.argmax(np.abs(flatten_arr), axis=1) for i in range(n_teachers): if flatten_arr[i, max_index[i]] > 0: votes_arr[i, max_index[i]] = 1 else: votes_arr[i, max_index[i] + n_features] = 1 votes_count = np.sum(votes_arr, axis=0) if thresh is None: dir_index, rdp_budget = gnmax_aggregator(votes_count, sigma, orders) else: dir_index, rdp_budget = gnmax_thresh_aggregator(votes_count, thresh * n_teachers, sigma_thresh, sigma, orders) max_votes = np.max(votes_count) selected_votes = votes_count[dir_index] # print("Max cnt: %d, selected cnt: %d" % (max_votes, selected_votes)) res_dir = np.zeros([1, n_features]) if dir_index < n_features and dir_index >= 0: res_dir[0, dir_index] = step_size elif dir_index >= n_features: res_dir[0, dir_index - n_features] = -step_size else: print("Teachers don't agree. Skip...") if pca_mat is not None: # project res direction back to original axis res_dir = np.matmul(res_dir, np.transpose(pca_mat)) return res_dir.reshape(output_list[0].shape), rdp_budget def gaussian_rdp(arr, sensitivity, orders, sigma): gaussian_noise = np.random.normal(loc=np.zeros(arr.shape), scale=sigma * sensitivity, size=arr.shape) # Table 2 @ https://arxiv.org/pdf/1702.07476.pdf rdp_budget = [o / ((2 * sigma) ** 2) for o in orders] return gaussian_noise, rdp_budget
12,952
36.436416
124
py
G-PATE
G-PATE-master/model.py
from __future__ import division import os import time import math from glob import glob import tensorflow as tf import numpy as np from six.moves import xrange import json import sys from keras.datasets import cifar10 from ops import * from utils import * from rdp_utils import * from pate_core import * import pickle from keras.utils import np_utils # import pandas as pd import torch import torch.utils.data import torchvision import torchvision.transforms as transforms import scipy from dp_pca import ComputeDPPrincipalProjection from sklearn.random_projection import GaussianRandomProjection from utils import pp, visualize, to_json, show_all_variables, mkdir from gen_data import batch2str from PIL import Image def partition_dataset(data, labels, nb_teachers, teacher_id): """ Simple partitioning algorithm that returns the right portion of the data needed by a given teacher out of a certain nb of teachers :param data: input data to be partitioned :param labels: output data to be partitioned :param nb_teachers: number of teachers in the ensemble (affects size of each partition) :param teacher_id: id of partition to retrieve :return: """ # Sanity check assert(int(teacher_id) < int(nb_teachers)) # This will floor the possible number of batches batch_len = int(len(data) / nb_teachers) # Compute start, end indices of partition start = teacher_id * batch_len end = (teacher_id+1) * batch_len # Slice partition off partition_data = data[start:end] if labels is not None: partition_labels = labels[start:end] else: partition_labels = None return partition_data, partition_labels def conv_out_size_same(size, stride): return int(math.ceil(float(size) / float(stride))) def sigmoid_cross_entropy_with_logits(x, y): try: return tf.nn.sigmoid_cross_entropy_with_logits(logits=x, labels=y) except: return tf.nn.sigmoid_cross_entropy_with_logits(logits=x, targets=y) class DCGAN(object): def __init__(self, sess, input_height=32, input_width=32, crop=False, batch_size=64, sample_num=64, output_height=32, output_width=32, y_dim=10, z_dim=100, gf_dim=64, df_dim=32, sample_step=800, gfc_dim=1024, dfc_dim=256, c_dim=3, dataset_name='default', input_fname_pattern='*.jpg', checkpoint_dir=None, teacher_dir=None, generator_dir=None, sample_dir=None, data_dir='./data', batch_teachers=10, teachers_batch=2, orders=None, thresh=None, dp_delta=1e-5, pca=False, pca_dim=5, non_private=False, random_proj=False, wgan=False, wgan_scale=10, small=False, config=None): """ Args: sess: TensorFlow session batch_size: The size of batch. Should be specified before training. z_dim: (optional) Dimension of dim for Z. [100] gf_dim: (optional) Dimension of gen filters in first conv layer. [64] df_dim: (optional) Dimension of discrim filters in first conv layer. [64] gfc_dim: (optional) Dimension of gen units for for fully connected layer. [1024] dfc_dim: (optional) Dimension of discrim units for fully connected layer. [1024] c_dim: (optional) Dimension of image color. For grayscale input, set to 1. [3] batch_teachers: Number of teacher models in one batch. Default 10. teachers_batch: Batches of training teacher models. Default 1. """ self.config = config self.small = small self.wgan = wgan self.wgan_scale = wgan_scale self.sample_step = sample_step self.pca = pca self.pca_dim = pca_dim self.random_proj = random_proj self.dp_eps_list = [] self.rdp_eps_list = [] self.rdp_order_list = [] self.thresh = thresh self.dp_delta = dp_delta self.sample_dir = sample_dir self.dataset = dataset_name self.batch_teachers = batch_teachers self.teachers_batch = teachers_batch self.overall_teachers = batch_teachers * teachers_batch self.sess = sess self.crop = crop self.batch_size = batch_size self.sample_num = sample_num self.input_height = input_height self.input_width = input_width self.output_height = output_height self.output_width = output_width self.z_dim = z_dim self.y_dim = y_dim self.gf_dim = gf_dim self.df_dim = df_dim self.gfc_dim = gfc_dim self.dfc_dim = dfc_dim # batch normalization : deals with poor initialization helps gradient flow self.d_bn1 = batch_norm(name='d_bn1') self.d_bn2 = batch_norm(name='d_bn2') self.d_bn3 = batch_norm(name='d_bn3') self.g_bn0 = batch_norm(name='g_bn0') self.g_bn1 = batch_norm(name='g_bn1') self.g_bn2 = batch_norm(name='g_bn2') self.g_bn3 = batch_norm(name='g_bn3') self.dataset_name = dataset_name self.input_fname_pattern = input_fname_pattern self.checkpoint_dir = checkpoint_dir self.teacher_dir = teacher_dir self.generator_dir = generator_dir self.data_dir = data_dir if orders is not None: self.orders = np.asarray(orders) else: self.orders = np.hstack([1.1, np.arange(2, config.orders)]) self.rdp_counter = np.zeros(self.orders.shape) # Load the dataset, ignore test data for now if self.dataset_name == 'mnist': self.data_X, self.data_y = self.load_mnist() self.c_dim = self.data_X[0].shape[-1] self.grayscale = (self.c_dim == 1) self.input_height = self.input_width = 28 self.output_height = self.output_width = 28 elif self.dataset_name == 'fashion_mnist': self.data_X, self.data_y = self.load_fashion_mnist() self.c_dim = self.data_X[0].shape[-1] # = (self.c_dim == 1) self.input_height = self.input_width = 28 self.output_height = self.output_width = 28 if self.config.random_label: np.random.shuffle(self.data_y) elif self.dataset_name == 'cifar': self.data_X, self.data_y = self.load_cifar() self.c_dim = self.data_X[0].shape[-1] self.grayscale = (self.c_dim == 3) elif 'small-celebA-gender' in self.dataset_name: mode = self.dataset_name.split('-')[-1] self.y_dim = 2 self.input_size = self.input_height = self.input_width = 32 self.output_size = self.output_height = self.output_width = 32 self.data_X, self.data_y = self.load_small_celebA_gender(mode) self.c_dim = self.data_X[0].shape[-1] self.grayscale = (self.c_dim == 1) if self.config.random_label: np.random.shuffle(self.data_y) elif 'celebA-hair' in self.dataset_name: mode = self.dataset_name.split('-')[-1] self.y_dim = 3 self.input_size = self.input_height = self.input_width = 64 self.output_size = self.output_height = self.output_width = 64 self.data_X, self.data_y = self.load_celebA_hair(mode) self.c_dim = self.data_X[0].shape[-1] self.grayscale = (self.c_dim == 1) if self.config.random_label: np.random.shuffle(self.data_y) elif 'celebA-gender' in self.dataset_name: mode = self.dataset_name.split('-')[-1] self.y_dim = 2 self.input_size = self.input_height = self.input_width = 64 self.output_size = self.output_height = self.output_width = 64 self.data_X, self.data_y = self.load_celebA_gender(mode) self.c_dim = self.data_X[0].shape[-1] self.grayscale = (self.c_dim == 1) if self.config.random_label: np.random.shuffle(self.data_y) elif self.dataset_name == 'cinic': self.data_X, self.data_y = self.load_cinic() self.c_dim = self.data_X[0].shape[-1] self.grayscale = (self.c_dim == 3) elif self.dataset_name == 'slt': self.data_X, self.data_y = self.slt() self.c_dim = self.data_X[0].shape[-1] self.grayscale = (self.c_dim == 3) print(self.data_X.shape) elif 'isolet' in self.dataset_name: self.data_X, self.data_y = self.load_isolet() self.train_size, self.input_size = self.data_X.shape self.output_size = self.input_size # self.y_dim = None # self.crop = False if self.pca_dim > self.input_size: self.pca_dim = self.input_size elif 'fire-small' in self.dataset_name: self.data_X = self.load_fire_data() self.data_y = None self.train_size, self.input_size = self.data_X.shape self.output_size = self.input_size self.y_dim = None self.crop = False if self.pca_dim > self.input_size: self.pca_dim = self.input_size elif 'census' in self.dataset_name: self.data_X = self.load_census_data() self.data_y = None self.train_size, self.input_size = self.data_X.shape self.output_size = self.input_size self.y_dim = None self.crop = False if self.pca_dim > self.input_size: self.pca_dim = self.input_size else: raise Exception("Check value of dataset flag") self.train_data_list = [] self.train_label_list = [] # if non_private: # for i in range(self.overall_teachers): # partition_data, partition_labels = partition_dataset(self.data_X, self.data_y, 1, i) # self.train_data_list.append(partition_data) # self.train_label_list.append(partition_labels) # else: if config.shuffle: from sklearn.utils import shuffle self.data_X, self.data_y = shuffle(self.data_X, self.data_y) from collections import defaultdict self.save_dict = defaultdict(lambda: False) for i in range(self.overall_teachers): partition_data, partition_labels = partition_dataset(self.data_X, self.data_y, self.overall_teachers, i) self.train_data_list.append(partition_data) self.train_label_list.append(partition_labels) # print(self.train_label_list) self.train_size = len(self.train_data_list[0]) if self.train_size < self.batch_size: self.batch_size = self.train_size print('adjusted batch size:', self.batch_size) # raise Exception("[!] Entire dataset size (%d) is less than the configured batch_size (%d) " % ( # self.train_size, self.batch_size)) self.build_model() def aggregate_results(self, output_list, config, thresh=None, epoch=None): if self.pca: res, rdp_budget = gradient_voting_rdp( output_list, config.step_size, config.sigma, config.sigma_thresh, self.orders, pca_mat=self.pca_components, thresh=thresh ) elif self.random_proj: orig_dim = 1 for dd in self.image_dims: orig_dim = orig_dim * dd if epoch is not None: proj_dim = min(epoch + 1, self.pca_dim) else: proj_dim = self.pca_dim n_data = output_list[0].shape[0] if config.proj_mat > 1: proj_dim_ = proj_dim // config.proj_mat n_data_ = n_data // config.proj_mat orig_dim_ = orig_dim // config.proj_mat print("n_data:", n_data) print("orig_dim:", orig_dim) transformers = [GaussianRandomProjection(n_components=proj_dim_) for _ in range(config.proj_mat)] for transformer in transformers: transformer.fit(np.zeros([n_data_, orig_dim_])) print(transformer.components_.shape) proj_matrices = [np.transpose(transformer.components_) for transformer in transformers] res, rdp_budget = gradient_voting_rdp_multiproj( output_list, config.step_size, config.sigma, config.sigma_thresh, self.orders, pca_mats=proj_matrices, thresh=thresh ) else: transformer = GaussianRandomProjection(n_components=proj_dim) transformer.fit(np.zeros([n_data, orig_dim])) # only the shape of output_list[0] is used proj_matrix = np.transpose(transformer.components_) # proj_matrix = np.random.normal(loc=np.zeros([orig_dim, proj_dim]), scale=1/float(proj_dim), size=[orig_dim, proj_dim]) res, rdp_budget = gradient_voting_rdp( output_list, config.step_size, config.sigma, config.sigma_thresh, self.orders, pca_mat=proj_matrix, thresh=thresh ) else: res, rdp_budget = gradient_voting_rdp(output_list, config.step_size, config.sigma, config.sigma_thresh, self.orders, thresh=thresh) return res, rdp_budget def non_private_aggregation(self, output_list, config): # TODO update nonprivate aggregation sum_arr = np.zeros(output_list[0].shape) for arr in output_list: sum_arr += arr return sum_arr / len(output_list) def load_fire_data(self): dataset_name = os.path.join(self.data_dir, self.dataset_name) dataset_name += '.csv' X = np.loadtxt(dataset_name) seed = 307 np.random.seed(seed) np.random.shuffle(X) return X def load_census_data(self): dataset_name = os.path.join(self.data_dir, self.dataset_name) dataset_name += '.pkl' with open(dataset_name, "rb") as f: X = pickle.load(f) seed = 37 np.random.seed(seed) np.random.shuffle(X) return X def load_isolet(self): dataset_name = os.path.join(self.data_dir, self.dataset_name) dataset_name += '.csv' X = np.loadtxt(dataset_name) # print(X.shape) seed = 37 np.random.seed(seed) np.random.shuffle(X) X = np.hsplit(X, [-1]) x = X[0] # print(X.shape) y = X[1] # print(y.shape) y = np_utils.to_categorical(y, 2) # print(y.shape) return x, y def load_cifar(self): # dataset_name = os.path.join(self.data_dir, self.dataset_name) # dataset_name += '.csv' (x_train, y_train), (x_test, y_test) = cifar10.load_data() y_train = np_utils.to_categorical(y_train, 10) x_train = x_train.reshape(x_train.shape[0], 32, 32, 3) x_train = x_train.astype('float32') / 255. return x_train, y_train def slt(self): path_to_data = '../../data/stl10_binary/unlabeled_X.bin' with open(path_to_data, 'rb') as f: # read whole file in uint8 chunks everything = np.fromfile(f, dtype=np.uint8) # We force the data into 3x96x96 chunks, since the # images are stored in "column-major order", meaning # that "the first 96*96 values are the red channel, # the next 96*96 are green, and the last are blue." # The -1 is since the size of the pictures depends # on the input file, and this way numpy determines # the size on its own. images = np.reshape(everything, (-1, 3, 96, 96)) # Now transpose the images into a standard image format # readable by, for example, matplotlib.imshow # You might want to comment this line or reverse the shuffle # if you will use a learning algorithm like CNN, since they like # their channels separated. images = np.transpose(images, (0, 3, 2, 1)) X_resized = np.zeros((100000, 32, 32, 3)) for i in range(0, 100000): img = images[i] img = Image.fromarray(img) img = np.array(img.resize((32, 32), Image.BICUBIC)) # 修改分辨率,再转为array类 X_resized[i, :, :, :] = img y = np.random.randint(10, size=(100000, 1)) y = np_utils.to_categorical(y, 10) X_resized /= 255 print(X_resized) return X_resized, y def load_cinic(self): cinic_directory = '../../data/cinic' # cinic_mean = [0.47889522, 0.47227842, 0.43047404] # cinic_std = [0.24205776, 0.23828046, 0.25874835] image_folder = torchvision.datasets.ImageFolder(cinic_directory + '/train/', # transform=transforms.Compose([transforms.ToTensor(), # transforms.Normalize(mean=cinic_mean,std=cinic_std)])), transform=transforms.ToTensor()) cinic_train = torch.utils.data.DataLoader(image_folder, batch_size=180000, shuffle=True) for batch_ndx, sample in enumerate(cinic_train): x = np.asarray(sample[0]) y = np.asarray(sample[1]) x = np.reshape(x, [x.shape[0], 32, 32, 3]) y = np_utils.to_categorical(y, 10) return x, y def load_celebA_gender(self, mode='train'): celebA_directory = '../../data/celebA/' import joblib if mode == 'train': train_x = joblib.load(celebA_directory + 'celebA-trn-x-lg-ups.pkl') train_y = joblib.load(celebA_directory + 'celebA-trn-gender-lg-ups.pkl') train_y = np_utils.to_categorical(train_y, 2) val_x = joblib.load(celebA_directory + 'celebA-val-x-lg-ups.pkl') val_y = joblib.load(celebA_directory + 'celebA-val-gender-lg-ups.pkl') val_y = np_utils.to_categorical(val_y, 2) return np.vstack((train_x, val_x)), np.vstack((train_y, val_y)) elif mode == 'val': val_x = joblib.load(celebA_directory + 'celebA-val-x-lg-ups.pkl') val_y = joblib.load(celebA_directory + 'celebA-val-gender-lg-ups.pkl') val_y = np_utils.to_categorical(val_y, 2) return val_x, val_y elif mode == 'tst': tst_x = joblib.load(celebA_directory + 'celebA-tst-x.pkl') tst_y = joblib.load(celebA_directory + 'celebA-tst-gender.pkl') tst_y = np_utils.to_categorical(tst_y, 2) return tst_x, tst_y else: raise Exception("Mode {} Not support".format(mode)) def load_celebA_hair(self, mode='trn'): celebA_directory = '../../data/celebA/' import joblib if mode == 'trn': train_x = joblib.load(celebA_directory + 'celeb-trn-ups-hair-x.pkl') train_y = joblib.load(celebA_directory + 'celeb-trn-ups-hair-y.pkl') train_y = np_utils.to_categorical(train_y, 3) val_x = joblib.load(celebA_directory + 'celeb-val-ups-hair-x.pkl') val_y = joblib.load(celebA_directory + 'celeb-val-ups-hair-y.pkl') val_y = np_utils.to_categorical(val_y, 3) return np.vstack((train_x, val_x)), np.vstack((train_y, val_y)) elif mode == 'val': val_x = joblib.load(celebA_directory + 'celeb-val-ups-hair-x.pkl') val_y = joblib.load(celebA_directory + 'celeb-val-ups-hair-y.pkl') val_y = np_utils.to_categorical(val_y, 3) return val_x, val_y elif mode == 'tst': tst_x = joblib.load(celebA_directory + 'celeb-tst-ups-hair-x.pkl') tst_y = joblib.load(celebA_directory + 'celeb-tst-ups-hair-y.pkl') tst_y = np_utils.to_categorical(tst_y, 3) return tst_x, tst_y else: raise Exception("Mode {} Not support".format(mode)) def load_small_celebA_gender(self, mode='train'): celebA_directory = '../../data/celebA/' import joblib if mode == 'train': train_x = joblib.load(celebA_directory + 'celebA-trn-x-small-ups.pkl') train_y = joblib.load(celebA_directory + 'celebA-trn-gender-ups.pkl') train_y = np_utils.to_categorical(train_y, 2) val_x = joblib.load(celebA_directory + 'celebA-val-x-small-ups.pkl') val_y = joblib.load(celebA_directory + 'celebA-val-gender-ups.pkl') val_y = np_utils.to_categorical(val_y, 2) return np.vstack((train_x, val_x)), np.vstack((train_y, val_y)) elif mode == 'val': val_x = joblib.load(celebA_directory + 'celebA-val-x-small-ups.pkl') val_y = joblib.load(celebA_directory + 'celebA-val-gender-ups.pkl') val_y = np_utils.to_categorical(val_y, 2) return val_x, val_y elif mode == 'tst': tst_x = joblib.load(celebA_directory + 'celebA-tst-x-small.pkl') tst_y = joblib.load(celebA_directory + 'celebA-tst-gender.pkl') tst_y = np_utils.to_categorical(tst_y, 2) return tst_x, tst_y else: raise Exception("Mode {} Not support".format(mode)) def load_fashion_mnist(self): data_dir = os.path.join(self.data_dir, self.dataset_name) fd = open(os.path.join(data_dir, 'train-images-idx3-ubyte')) loaded = np.fromfile(file=fd, dtype=np.uint8) trX = loaded[16:].reshape((60000, 28, 28, 1)).astype(np.float) fd = open(os.path.join(data_dir, 'train-labels-idx1-ubyte')) loaded = np.fromfile(file=fd, dtype=np.uint8) trY = loaded[8:].reshape((60000)).astype(np.int) # fd = open(os.path.join(data_dir,'t10k-images-idx3-ubyte')) # loaded = np.fromfile(file=fd,dtype=np.uint8) # teX = loaded[16:].reshape((10000,28,28,1)).astype(np.float) # fd = open(os.path.join(data_dir,'t10k-labels-idx1-ubyte')) # loaded = np.fromfile(file=fd,dtype=np.uint8) # teY = loaded[8:].reshape((10000)).astype(np.int) trY = np.asarray(trY) # teY = np.asarray(teY) # X = np.concatenate((trX, teX), axis=0) # y = np.concatenate((trY, teY), axis=0).astype(np.int) X = trX y = trY.astype(np.int) seed = 307 np.random.seed(seed) np.random.shuffle(X) np.random.seed(seed) np.random.shuffle(y) y_vec = np.zeros((len(y), self.y_dim), dtype=np.float) for i, label in enumerate(y): y_vec[i, y[i]] = 1.0 return X / 255., y_vec def load_mnist(self): data_dir = os.path.join(self.data_dir, self.dataset_name) fd = open(os.path.join(data_dir, 'train-images-idx3-ubyte')) loaded = np.fromfile(file=fd, dtype=np.uint8) trX = loaded[16:].reshape((60000, 28, 28, 1)).astype(np.float) fd = open(os.path.join(data_dir, 'train-labels-idx1-ubyte')) loaded = np.fromfile(file=fd, dtype=np.uint8) trY = loaded[8:].reshape((60000)).astype(np.int) # fd = open(os.path.join(data_dir,'t10k-images-idx3-ubyte')) # loaded = np.fromfile(file=fd,dtype=np.uint8) # teX = loaded[16:].reshape((10000,28,28,1)).astype(np.float) # fd = open(os.path.join(data_dir,'t10k-labels-idx1-ubyte')) # loaded = np.fromfile(file=fd,dtype=np.uint8) # teY = loaded[8:].reshape((10000)).astype(np.int) trY = np.asarray(trY) # teY = np.asarray(teY) # X = np.concatenate((trX, teX), axis=0) # y = np.concatenate((trY, teY), axis=0).astype(np.int) X = trX y = trY.astype(np.int) seed = 307 np.random.seed(seed) np.random.shuffle(X) np.random.seed(seed) np.random.shuffle(y) y_vec = np.zeros((len(y), self.y_dim), dtype=np.float) for i, label in enumerate(y): y_vec[i, y[i]] = 1.0 return X / 255., y_vec def build_model(self): if self.crop: image_dims = [self.output_height, self.output_width, self.c_dim] else: image_dims = [self.input_height, self.input_width, self.c_dim] self.inputs = tf.placeholder( tf.float32, [self.batch_size] + [self.input_height, self.input_width, self.c_dim], name='real_images') self.y = tf.placeholder(tf.float32, [self.batch_size, self.y_dim], name='y') self.image_dims = image_dims inputs = self.inputs if self.crop: inputs = tf.image.resize_image_with_crop_or_pad(inputs, target_height=self.output_height, target_width=self.output_width) self.z = tf.placeholder(tf.float32, [self.batch_size, self.z_dim], name='z') self.z_sum = histogram_summary("z", self.z) self.G = self.generator(self.z, self.y) if 'slt' in self.dataset_name or 'cifar' in self.dataset_name: self.G_sum = image_summary("G", self.G, max_outputs=10) self.updated_img = tf.placeholder(tf.float32, [self.batch_size] + image_dims, name='updated_img') self.g_loss = tf.reduce_sum(tf.square(self.updated_img - self.G)) self.g_loss_sum = scalar_summary("g_loss", self.g_loss) self.teachers_list = [] for i in range(self.batch_teachers): with tf.variable_scope("teacher%d" % i) as scope: D, D_logits = self.discriminator(inputs, self.y) scope.reuse_variables() D_, D_logits_ = self.discriminator(self.G, self.y) if self.wgan: # Use WassersteinGAN loss with gradient penalty. Reference: https://github.com/jiamings/wgan/blob/master/wgan_v2.py # Calculate interpolation of real and fake image if 'mnist' in self.dataset_name: alpha = tf.random_uniform([self.batch_size, 1, 1, 1], 0.0, 1.0) alpha = tf.tile(alpha, tf.constant([1, self.input_height, self.input_width, self.c_dim])) else: alpha = tf.random_uniform([self.batch_size, 1], 0.0, 1.0) alpha = tf.tile(alpha, tf.constant([1, self.input_size])) x_hat = tf.math.multiply(alpha, inputs) + tf.math.multiply((1 - alpha), self.G) _, d_hat = self.discriminator(x_hat, self.y) # Calculate gradient penalty for wgan ddx = tf.gradients(d_hat, x_hat)[0] ddx = tf.sqrt(tf.reduce_sum(tf.square(ddx), axis=1)) ddx = tf.reduce_mean(tf.square(ddx - 1.0) ** 2 * self.wgan_scale) if self.wgan: teacher = { 'd_loss': tf.reduce_mean(D_logits_) - tf.reduce_mean(D_logits) + ddx, 'g_loss': -tf.reduce_mean(D_logits_), } else: teacher = { 'd_loss': tf.reduce_mean(sigmoid_cross_entropy_with_logits(D_logits, tf.ones_like(D))) + \ tf.reduce_mean(sigmoid_cross_entropy_with_logits(D_logits_, tf.zeros_like(D_))), 'g_loss': tf.reduce_mean(sigmoid_cross_entropy_with_logits(D_logits_, tf.ones_like(D_))), } teacher.update({ 'd_loss_sum': scalar_summary("d_loss_%d" % i, teacher['d_loss']), 'g_loss_sum': scalar_summary("g_loss_%d" % i, teacher['g_loss']), }) # calculate the change in the images that would minimize generator loss teacher['img_grads'] = -tf.gradients(teacher['g_loss'], self.G)[0] if 'slt' in self.dataset_name: teacher['img_grads_sum'] = image_summary("img_grads", teacher['img_grads'], max_outputs=10) self.teachers_list.append(teacher) t_vars = tf.trainable_variables() g_list = tf.global_variables() add_save = [g for g in g_list if "moving_mean" in g.name] add_save += [g for g in g_list if "moving_variance" in g.name] self.save_vars = t_vars + add_save self.d_vars = [] for i in range(self.batch_teachers): self.d_vars.append([var for var in t_vars if 'teacher%d' % i in var.name]) self.g_vars = [var for var in t_vars if 'g_' in var.name] self.g_save_vars = [var for var in t_vars if 'g_' in var.name] self.d_save_vars = [var for var in t_vars if 'd_' in var.name] # print(self.d_save_vars) print(self.save_vars) # self.d_save_vars = {'k': v for k, v in zip(self.d_save_vars, self.d_save_vars)} self.saver = tf.train.Saver(max_to_keep=5, var_list=self.save_vars) self.saver_g = tf.train.Saver(max_to_keep=5, var_list=self.g_save_vars) self.saver_d = tf.train.Saver(max_to_keep=self.teachers_batch, var_list=self.d_save_vars) def get_random_labels(self, batch_size): # print(self.y_dim) y_vec = np.zeros((batch_size, self.y_dim), dtype=np.float) y = np.random.randint(0, self.y_dim, batch_size) for i, label in enumerate(y): y_vec[i, y[i]] = 1.0 return y_vec def train_together(self, config): print("Training teacher models and student model together...") if not config.non_private: assert len(self.train_data_list) == self.overall_teachers else: print(str(len(self.train_data_list))) configs = { 'sigma': config.sigma, 'sigma_thresh': config.sigma_thresh, 'pca': self.pca, 'pca_sigma': config.pca_sigma, 'step_size': config.step_size, 'batch_teachers': self.batch_teachers, 'g_step': config.g_step, 'pca_dim': self.pca_dim, } if not os.path.exists(self.checkpoint_dir): os.makedirs(self.checkpoint_dir) if not os.path.exists(self.teacher_dir): os.makedirs(self.teacher_dir) with open(os.path.join(self.checkpoint_dir, 'configs.json'), 'w') as fp: json.dump(configs, fp) if self.pca: data = self.data_X.reshape([self.data_X.shape[0], -1]) self.pca_components, rdp_budget = ComputeDPPrincipalProjection( data, self.pca_dim, self.orders, config.pca_sigma, ) self.rdp_counter += rdp_budget d_optim_list = [] for i in range(self.batch_teachers): d_optim_list.append(tf.train.AdamOptimizer(config.learning_rate, beta1=config.beta1).minimize( self.teachers_list[i]['d_loss'], var_list=self.d_vars[i])) g_optim = tf.train.AdamOptimizer(config.learning_rate, beta1=config.beta1).minimize(self.g_loss, var_list=self.g_vars) if not config.pretrain: try: tf.global_variables_initializer().run() except: tf.initialize_all_variables().run() else: try: tf.global_variables_initializer().run() except: tf.initialize_all_variables().run() self.load_pretrain(config.checkpoint_dir) # data = self.gen_data(5000) # output_dir = os.path.join(self.checkpoint_dir, self.sample_dir) # if not os.path.exists(output_dir): # os.makedirs(output_dir) # filename = 'private.data_epoch_' + str(-1) + '.pkl' # outfile = os.path.join(output_dir, filename) # mkdir(output_dir) # with open(outfile, 'wb') as f: # pickle.dump(data, f) # current_scope = tf.contrib.framework.get_name_scope() # with tf.variable_scope(current_scope, reuse=True): # biases = tf.get_variable("teacher0/d_h0_conv/biases") # biases = tf.Print(biases, [biases]) # self.sess.run(biases) if 'slt' in self.dataset_name: self.g_sum = merge_summary([self.z_sum, self.G_sum, self.g_loss_sum]) else: self.g_sum = merge_summary([self.z_sum, self.g_loss_sum]) self.d_sum_list = [] for i in range(self.batch_teachers): teacher = self.teachers_list[i] if 'slt' in self.dataset_name: self.d_sum_list.append( merge_summary([teacher['d_loss_sum'], teacher['g_loss_sum'], teacher['img_grads_sum']])) else: self.d_sum_list.append(merge_summary([teacher['d_loss_sum'], teacher['g_loss_sum']])) self.writer = SummaryWriter(os.path.join(self.checkpoint_dir, "logs"), self.sess.graph) sample_z = np.random.uniform(-1, 1, size=(self.sample_num, self.z_dim)) counter = 0 start_time = time.time() self.save_d(self.teacher_dir, 0, -1) for epoch in xrange(config.epoch): print("----------------epoch: %d --------------------" % epoch) print("-------------------train-teachers----------------") batch_idxs = int(min(self.train_size, config.train_size) // self.batch_size) # The idex of each batch print("Train %d idxs" % batch_idxs) for idx in xrange(0, batch_idxs): batch_z = np.random.uniform(-1, 1, [self.batch_size, self.z_dim]).astype(np.float32) errD = 0 # train teacher models in batches, teachers_batch: how many batches of teacher for batch_num in range(self.teachers_batch): could_load, checkpoint_counter = self.load_d(self.teacher_dir, epoch=epoch, batch_num=batch_num) if could_load: counter = checkpoint_counter print("load sucess_this_epoch") else: print('fail_1') could_load, checkpoint_counter = self.load_d(self.teacher_dir, epoch=epoch - 1, batch_num=batch_num) if could_load: counter = checkpoint_counter print("load sucess_previous_epoch") else: print('fail_2') could_load, checkpoint_counter = self.load_d(self.teacher_dir, epoch=0, batch_num=-1) # train each teacher in this batch, batch_teachers: how many teacher in a batch for teacher_id in range(self.batch_teachers): #print("Training teacher model %d" % teacher_id) # data_X = self.data_X if config.non_private else self.train_data_list[teacher_id+batch_num*self.batch_teachers] data_X = self.train_data_list[teacher_id+batch_num*self.batch_teachers] batch_idx = range(idx * self.batch_size, (idx + 1) * self.batch_size) batch_images = data_X[batch_idx] for k in range(config.d_step): if self.y is not None: # data_y = self.data_y if config.non_private else self.train_label_list[teacher_id+batch_num*self.batch_teachers] data_y = self.train_label_list[teacher_id+batch_num*self.batch_teachers] #print(data_y.shape) batch_labels = data_y[batch_idx] _, summary_str = self.sess.run([d_optim_list[teacher_id], self.d_sum_list[teacher_id]], feed_dict={ self.inputs: batch_images, self.z: batch_z, self.y: batch_labels, }) self.writer.add_summary(summary_str, epoch) err = self.teachers_list[teacher_id]['d_loss'].eval({ self.z: batch_z, self.inputs: batch_images, self.y: batch_labels, }) # print(str(batch_num*self.batch_teachers + teacher_id) + "loss:"+str(err)) errD += err else: _, summary_str = self.sess.run([d_optim_list[teacher_id], self.d_sum_list[teacher_id]], feed_dict={ self.inputs: batch_images, self.z: batch_z, }) self.writer.add_summary(summary_str, epoch) err = self.teachers_list[teacher_id]['d_loss'].eval({ self.z: batch_z, self.inputs: batch_images, }) # print(str(batch_num * self.batch_teachers + teacher_id) + "d_loss:" + str(err)) errD += err self.save_d(self.teacher_dir, epoch, batch_num) # print("------------------train-generator-------------------") for k in range(config.g_step): errG = 0 img_grads_list = [] if self.y is not None: batch_labels = self.get_random_labels(self.batch_size) for batch_num in range(self.teachers_batch): could_load, checkpoint_counter = self.load_d(self.teacher_dir, epoch=epoch, batch_num=batch_num) if could_load: counter = checkpoint_counter print("load sucess") else: print('fail') for teacher_id in range(self.batch_teachers): img_grads = self.sess.run(self.teachers_list[teacher_id]['img_grads'], feed_dict={ self.z: batch_z, self.y: batch_labels, }) img_grads_list.append(img_grads) old_img = self.sess.run(self.G, feed_dict={self.z: batch_z, self.y: batch_labels}) else: for batch_num in range(self.teachers_batch): could_load, checkpoint_counter = self.load_d(self.teacher_dir, epoch=epoch, batch_num=batch_num) if could_load: counter = checkpoint_counter print("load sucess") else: print('fail') for teacher_id in range(self.batch_teachers): img_grads = self.sess.run(self.teachers_list[teacher_id]['img_grads'], feed_dict={ self.z: batch_z, }) img_grads_list.append(img_grads) old_img = self.sess.run(self.G, feed_dict={self.z: batch_z}) img_grads_agg_list = [] for j in range(self.batch_size): thresh = self.thresh if config.non_private: img_grads_agg_tmp = self.non_private_aggregation([grads[j] for grads in img_grads_list], config) rdp_budget = 0 elif config.increasing_dim: img_grads_agg_tmp, rdp_budget = self.aggregate_results( [grads[j] for grads in img_grads_list], config, thresh=thresh, epoch=epoch) else: img_grads_agg_tmp, rdp_budget = self.aggregate_results( [grads[j] for grads in img_grads_list], config, thresh=thresh) img_grads_agg_list.append(img_grads_agg_tmp) self.rdp_counter += rdp_budget img_grads_agg = np.asarray(img_grads_agg_list) updated_img = old_img + img_grads_agg if config.non_private: eps = 0 order = 0 else: # calculate privacy budget and break if exceeds threshold eps, order = compute_eps_from_delta(self.orders, self.rdp_counter, self.dp_delta) if eps > config.max_eps: print("New budget (eps = %.2f) exceeds threshold of %.2f. Early break (eps = %.2f)." % ( eps, config.max_eps, self.dp_eps_list[-1])) # save privacy budget self.save(config.checkpoint_dir, counter) np.savetxt(self.checkpoint_dir + "/dp_eps.txt", np.asarray(self.dp_eps_list), delimiter=",") np.savetxt(self.checkpoint_dir + "/rdp_eps.txt", np.asarray(self.rdp_eps_list), delimiter=",") np.savetxt(self.checkpoint_dir + "/rdp_order.txt", np.asarray(self.rdp_order_list), delimiter=",") gen_batch = 100000 // self.batch_size + 1 data = self.gen_data(gen_batch) data = data[:100000] import joblib joblib.dump(data, self.checkpoint_dir + '/eps-%.2f.data' % self.dp_eps_list[-1]) sys.exit() self.dp_eps_list.append(eps) self.rdp_order_list.append(order) self.rdp_eps_list.append(self.rdp_counter) # Update G network if self.y is not None: _, summary_str, errG2 = self.sess.run([g_optim, self.g_sum, self.g_loss], feed_dict={ self.z: batch_z, self.updated_img: updated_img, self.y: batch_labels, }) self.writer.add_summary(summary_str, epoch) errG = self.g_loss.eval({ self.z: batch_z, self.updated_img: updated_img, self.y: batch_labels, }) else: _, summary_str = self.sess.run([g_optim, self.g_sum], feed_dict={ self.z: batch_z, self.updated_img: updated_img, }) self.writer.add_summary(summary_str, epoch) errG = self.g_loss.eval({ self.z: batch_z, self.updated_img: updated_img, }) counter += 1 print("Epoch: [%2d/%2d] [%4d/%4d] time: %4.4f, d_loss: %.8f, g_loss: %.8f, g_loss_before: %.8f, dp_eps: %.8f, rdp_order: %d" \ % (epoch, config.epoch, idx, batch_idxs, time.time() - start_time, errD, errG, errG2, eps, order)) # filename = 'epoch'+str(epoch)+'_errD'+str(errD)+'_errG'+str(errG)+'_teachers'+str(self.batch_teachers)+'f.csv' # if epoch % 4 == 0: print('----------------------generate sample----------------------') # data = self.gen_data(500) # output_dir = os.path.join(self.checkpoint_dir, self.sample_dir) # if not os.path.exists(output_dir): # os.makedirs(output_dir) # filename = 'private.data_epoch_' + str(epoch) + '.pkl' # outfile = os.path.join(output_dir, filename) # mkdir(output_dir) # with open(outfile,'wb') as f: # pickle.dump(data, f) filename = 'epoch' + str(epoch) + '_errD' + str(errD) + '_errG' + str(errG) + '_teachers' + str( self.batch_teachers) + 'f.csv' # save each epoch self.save(config.checkpoint_dir, counter) np.savetxt(self.checkpoint_dir + "/dp_eps.txt", np.asarray(self.dp_eps_list), delimiter=",") np.savetxt(self.checkpoint_dir + "/rdp_order.txt", np.asarray(self.rdp_order_list), delimiter=",") np.savetxt(self.checkpoint_dir + "/rdp_eps.txt", np.asarray(self.rdp_eps_list), delimiter=",") if config.save_epoch: floor_eps = math.floor(eps * 10) / 10.0 if not self.save_dict[floor_eps]: # get a checkpoint of low eps self.save_dict[floor_eps] = True from shutil import copytree src_dir = os.path.join(config.checkpoint_dir, self.model_dir) dst_dir = os.path.join(config.checkpoint_dir, str(floor_eps)) copytree(src_dir, dst_dir) # # save after training self.save(config.checkpoint_dir, counter) np.savetxt(self.checkpoint_dir + "/dp_eps.txt", np.asarray(self.dp_eps_list), delimiter=",") np.savetxt(self.checkpoint_dir + "/rdp_eps.txt", np.asarray(self.rdp_eps_list), delimiter=",") np.savetxt(self.checkpoint_dir + "/rdp_order.txt", np.asarray(self.rdp_order_list), delimiter=",") return self.dp_eps_list[-1], self.dp_delta def discriminator(self, image, y): yb = tf.reshape(y, [self.batch_size, 1, 1, self.y_dim]) x = conv_cond_concat(image, yb) h0 = lrelu(conv2d(x, self.c_dim + self.y_dim, name='d_h0_conv')) h0 = conv_cond_concat(h0, yb) if self.wgan: h1 = lrelu(conv2d(h0, self.df_dim + self.y_dim, name='d_h1_conv')) else: h1 = lrelu(self.d_bn1(conv2d(h0, self.df_dim + self.y_dim, name='d_h1_conv'))) h1 = tf.reshape(h1, [self.batch_size, -1]) h1 = concat([h1, y], 1) if self.wgan: h2 = lrelu(linear(h1, self.dfc_dim, 'd_h2_lin')) else: h2 = lrelu(self.d_bn2(linear(h1, self.dfc_dim, 'd_h2_lin'))) h2 = concat([h2, y], 1) h3 = linear(h2, 1, 'd_h3_lin') return tf.nn.sigmoid(h3), h3 def generator(self, z, y): with tf.variable_scope("generator") as scope: s_h, s_w = self.output_height, self.output_width s_h2, s_h4 = int(s_h / 2), int(s_h / 4) s_w2, s_w4 = int(s_w / 2), int(s_w / 4) # yb = tf.expand_dims(tf.expand_dims(y, 1),2) yb = tf.reshape(y, [self.batch_size, 1, 1, self.y_dim]) z = concat([z, y], 1) if self.wgan: h0 = tf.nn.relu(linear(z, self.gfc_dim, 'g_h0_lin')) else: h0 = tf.nn.relu(self.g_bn0(linear(z, self.gfc_dim, 'g_h0_lin'))) h0 = concat([h0, y], 1) if self.wgan: h1 = tf.nn.relu(linear(h0, self.gf_dim * 2 * s_h4 * s_w4, 'g_h1_lin')) else: h1 = tf.nn.relu(self.g_bn1(linear(h0, self.gf_dim * 2 * s_h4 * s_w4, 'g_h1_lin'))) h1 = tf.reshape(h1, [self.batch_size, s_h4, s_w4, self.gf_dim * 2]) h1 = conv_cond_concat(h1, yb) if self.wgan: h2 = tf.nn.relu(deconv2d(h1, [self.batch_size, s_h2, s_w2, self.gf_dim * 2], name='g_h2')) else: h2 = tf.nn.relu( self.g_bn2(deconv2d(h1, [self.batch_size, s_h2, s_w2, self.gf_dim * 2], name='g_h2'))) h2 = conv_cond_concat(h2, yb) if self.config.tanh: return (1 + tf.nn.tanh(deconv2d(h2, [self.batch_size, s_h, s_w, self.c_dim], name='g_h3'))) / 2. else: return tf.nn.sigmoid(deconv2d(h2, [self.batch_size, s_h, s_w, self.c_dim], name='g_h3')) def gen_data(self, n_batch, label=None): output_list = [] for i in range(n_batch): batch_z = np.random.uniform(-1, 1, [self.batch_size, self.z_dim]).astype(np.float32) if self.y is not None: if label is None: batch_labels = self.get_random_labels(self.batch_size) else: batch_labels = np.zeros((self.batch_size, self.y_dim), dtype=np.float) batch_labels[:, label] = 1.0 outputs = self.sess.run(self.G, feed_dict={ self.z: batch_z, self.y: batch_labels, }) outputsX = outputs.reshape([self.batch_size, -1]) outputs = np.hstack([outputsX, batch_labels[:, 0:10]]) else: outputs = self.sess.run(self.G, feed_dict={ self.z: batch_z, }) outputsX = outputs.reshape([self.batch_size, -1]) outputs = outputsX output_list.append(outputs) output_arr = np.vstack(output_list) return output_arr @property def model_dir(self): return "{}_{}_{}_{}".format( self.dataset_name, self.batch_size, self.output_height, self.output_width) def print_tensors_in_checkpoint(self, checkpoint_dir, ckpt_name): from tensorflow.python.tools.inspect_checkpoint import print_tensors_in_checkpoint_file import os checkpoint_path = os.path.join(checkpoint_dir, ckpt_name) # List ALL tensors example output: v0/Adam (DT_FLOAT) [3,3,1,80] print_tensors_in_checkpoint_file(file_name=checkpoint_path, tensor_name='', all_tensors=True) def load_pretrain(self, checkpoint_dir): print(" [*] Reading checkpoints...") print(checkpoint_dir) save_vars_dict = {x.name[:-2]: x for x in self.save_vars if x.name.startswith('generator')} pretrain_saver = tf.train.Saver(max_to_keep=5, var_list=save_vars_dict) print(self.dataset_name) if 'cifar' in self.dataset_name or 'cinic' in self.dataset_name: ckpt_name = 'DCGAN.model-100' elif 'mnist' in self.dataset_name: ckpt_name = 'CIFAR.model-250' elif 'celebA' in self.dataset_name: ckpt_name = 'CIFAR.model-99' pretrain_saver.restore(self.sess, os.path.join(checkpoint_dir, ckpt_name)) import re if self.config.load_d: for i in range(self.batch_teachers): print('loading teacher {}'.format(i)) save_vars_dict = {re.sub(r'teacher[0-9]+', 'teacher0', x.name[:-2]): x for x in self.save_vars if x.name.startswith('teacher{}/'.format(i))} pretrain_saver = tf.train.Saver(max_to_keep=5, var_list=save_vars_dict) pretrain_saver.restore(self.sess, os.path.join(checkpoint_dir, ckpt_name)) # save_vars_dict = {x.name: x for x in self.save_vars} # print(save_vars_dict.keys()) counter = int(next(re.finditer("(\d+)(?!.*\d)", ckpt_name)).group(0)) print(" [*] Success to read {}".format(ckpt_name)) # current_scope = tf.contrib.framework.get_name_scope() # with tf.variable_scope(current_scope, reuse=True): # biases = tf.get_variable("teacher0/d_h0_conv/biases") # biases2 = tf.get_variable("teacher12/d_h0_conv/biases") # biases3 = tf.get_variable("generator/g_h0_lin/Matrix") # biases = tf.Print(biases, [biases, biases2, biases3]) # self.sess.run(biases) return True, counter def load(self, checkpoint_dir, ckpt_name): import re print(" [*] Reading checkpoints...") print(checkpoint_dir) print(ckpt_name) self.saver.restore(self.sess, os.path.join(checkpoint_dir, ckpt_name)) counter = int(next(re.finditer("(\d+)(?!.*\d)", ckpt_name)).group(0)) print(" [*] Success to read {}".format(ckpt_name)) return True, counter # def load(self, checkpoint_dir): # import re # print(" [*] Reading checkpoints...") # checkpoint_dir = os.path.join(checkpoint_dir, self.model_dir) # print(checkpoint_dir) # ckpt = tf.train.get_checkpoint_state(checkpoint_dir) # print(ckpt) # print(ckpt.model_checkpoint_path) # if ckpt and ckpt.model_checkpoint_path: # ckpt_name = os.path.basename(ckpt.model_checkpoint_path) # print(ckpt_name) # self.saver.restore(self.sess, os.path.join(checkpoint_dir, ckpt_name)) # counter = int(next(re.finditer("(\d+)(?!.*\d)", ckpt_name)).group(0)) # print(" [*] Success to read {}".format(ckpt_name)) # return True, counter # else: # print(" [*] Failed to find a checkpoint") # return False, 0 def load_d(self, checkpoint_dir, batch_num, epoch): import re print(" [*] Reading checkpoints...") checkpoint_dir = os.path.join(checkpoint_dir, self.model_dir) model_name = "DCGAN_batch_" + str(batch_num) + "_epoch-" + str(epoch) ckpt = os.path.join(checkpoint_dir, model_name) print(ckpt + ".meta") if os.path.isfile(ckpt + ".meta"): # model_name = "DCGAN_batch_" + str(batch_num) + "_epoch_" + str(epoch) # print(model_name) self.saver_d.restore(self.sess, ckpt) counter = int(next(re.finditer("(\d+)(?!.*\d)", model_name)).group(0)) print(" [*] Success to read {}".format(model_name)) return True, counter else: print(" [*] Failed to find a checkpoint") return False, 0 def save(self, checkpoint_dir, step): model_name = "CIFAR.model" checkpoint_dir = os.path.join(checkpoint_dir, self.model_dir) if not os.path.exists(checkpoint_dir): os.makedirs(checkpoint_dir) self.saver.save(self.sess, os.path.join(checkpoint_dir, model_name), global_step=step) def save_d(self, checkpoint_dir, step, teacher_batch): model_name = "DCGAN_batch_" + str(teacher_batch) + "_epoch" checkpoint_dir = os.path.join(checkpoint_dir, self.model_dir) if not os.path.exists(checkpoint_dir): os.makedirs(checkpoint_dir) self.saver_d.save(self.sess, os.path.join(checkpoint_dir, model_name), global_step=step) print("-------------save-dis----------------------") def save_g(self, checkpoint_dir, step): model_name = "DCGAN.model" checkpoint_dir = os.path.join(checkpoint_dir, self.model_dir) if not os.path.exists(checkpoint_dir): os.makedirs(checkpoint_dir) self.saver_g.save(self.sess, os.path.join(checkpoint_dir, model_name), global_step=step)
57,590
43.575077
156
py
G-PATE
G-PATE-master/evaluation/train-classifier-celebA.py
#!/usr/bin/env python # coding: utf-8 import numpy as np import argparse import joblib import tensorflow as tf config = tf.ConfigProto() config.gpu_options.allow_growth = True # config.gpu_options.per_process_gpu_memory_fraction = 0.3 tf.keras.backend.set_session(tf.Session(config=config)); def load_celeb(): celebA_directory = '../../data/celebA/' tst_x = joblib.load(celebA_directory + 'celebA-tst-x.pkl') tst_y = joblib.load(celebA_directory + 'celebA-tst-gender.pkl') print(tst_y.sum(), len(tst_y)) from keras.utils import np_utils tst_y = np_utils.to_categorical(tst_y, 2) return tst_x, tst_y def load_celeb_train(): celebA_directory = '../../data/celebA/' tst_x = joblib.load(celebA_directory + 'celebA-trn-x-lg-ups.pkl') tst_y = joblib.load(celebA_directory + 'celebA-trn-gender-lg-ups.pkl') print(tst_y.sum(), len(tst_y)) from keras.utils import np_utils tst_y = np_utils.to_categorical(tst_y, 2) return tst_x, tst_y x_test, y_test = load_celeb() def pipeline(): parser = argparse.ArgumentParser(description='Train classifier and evaluate their accuracy') parser.add_argument('--data', type=str, help='datafile name') args = parser.parse_args() data = joblib.load(args.data) print(args.data) print(data.shape) x, label = np.hsplit(data, [-2]) nb_classes = 2 label = label.reshape((label.shape[0], nb_classes),order='F') x = x.reshape(x.shape[0], 64, 64, 3) from keras.models import Sequential from keras.layers.core import Dense, Dropout, Activation, Flatten from keras.layers.pooling import MaxPooling2D from keras.layers.convolutional import Convolution2D, Conv2D from keras.optimizers import Adam from keras import optimizers model = Sequential() model.add(Conv2D(32, kernel_size=3, activation='relu', input_shape=(64, 64, 3), name='Conv2D-1')) model.add(MaxPooling2D(pool_size=2, name='MaxPool')) model.add(Dropout(0.2, name='Dropout-1')) model.add(Conv2D(64, kernel_size=3, activation='relu', name='Conv2D-2')) model.add(Dropout(0.25, name='Dropout-2')) model.add(Flatten(name='flatten')) model.add(Dense(64, activation='relu', name='Dense')) model.add(Dense(nb_classes, activation='softmax', name='Output')) sgd = optimizers.sgd(lr=1e-4) model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy']) print(x.shape) print(label.shape) print(x_test.shape) print(y_test.shape) evals = model.fit(x, label, batch_size=256, epochs=250, validation_data=(x_test, y_test), shuffle=True) return evals.history train_accs, eval_accs = pipeline() print("Max eval acc:", max(eval_accs)) print("Max train acc:", max(train_accs))
2,797
30.438202
107
py
G-PATE
G-PATE-master/evaluation/train-classifier-fmnist.py
#!/usr/bin/env python # coding: utf-8 import numpy as np import argparse import tensorflow as tf config = tf.ConfigProto() config.gpu_options.allow_growth = True # config.gpu_options.per_process_gpu_memory_fraction = 0.3 tf.keras.backend.set_session(tf.Session(config=config)); def pipeline(): parser = argparse.ArgumentParser(description='Train classifier and evaluate their accuracy') parser.add_argument('--data', type=str, help='datafile name') args = parser.parse_args() import joblib data = joblib.load(args.data) print(args.data) x, label = np.hsplit(data, [-10]) nb_classes = 10 label = label.reshape((label.shape[0], nb_classes), order='F') x = x.reshape(x.shape[0], 28, 28, 1) from keras.datasets import fashion_mnist (x_train, y_train), (x_test, y_test) = fashion_mnist.load_data() from keras.utils import np_utils y_train = np_utils.to_categorical(y_train, 10) y_test = np_utils.to_categorical(y_test, 10) x_train = x_train.reshape(x_train.shape[0], 28, 28, 1) x_train = x_train.astype('float32') / 255. x_test = x_test.reshape(x_test.shape[0], 28, 28, 1) x_test = x_test.astype('float32') / 255. from keras.models import Sequential from keras.layers.core import Dense, Dropout, Activation, Flatten from keras.layers.pooling import MaxPooling2D from keras.layers.convolutional import Convolution2D, Conv2D from keras.optimizers import Adam from keras import optimizers model = Sequential() model.add(Conv2D(32, kernel_size=3, activation='relu', input_shape=(28, 28, 1), name='Conv2D-1')) model.add(MaxPooling2D(pool_size=2, name='MaxPool')) model.add(Dropout(0.2, name='Dropout-1')) model.add(Conv2D(64, kernel_size=3, activation='relu', name='Conv2D-2')) model.add(Dropout(0.25, name='Dropout-2')) model.add(Flatten(name='flatten')) model.add(Dense(64, activation='relu', name='Dense')) model.add(Dense(nb_classes, activation='softmax', name='Output')) sgd = optimizers.sgd(lr=2e-3) model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy']) print(x.shape) print(label.shape) print(x_test.shape) print(y_test.shape) train_accs = [] eval_accs = [] history = model.fit(x, label, batch_size=512, epochs=600, validation_data=(x_test, y_test), shuffle=True) if 'acc' in history.history: train_accs = history.history['acc'] else: train_accs = history.history['accuracy'] if 'val_acc' in history.history: eval_accs = history.history['val_acc'] else: eval_accs = history.history['val_accuracy'] return train_accs, eval_accs train_accs, eval_accs = pipeline() print("Max eval acc:", max(eval_accs)) print("Max train acc:", max(train_accs))
2,846
32.892857
109
py
G-PATE
G-PATE-master/evaluation/train-classifier-mnist.py
#!/usr/bin/env python # coding: utf-8 import numpy as np import argparse import tensorflow as tf config = tf.ConfigProto() config.gpu_options.allow_growth = True # config.gpu_options.per_process_gpu_memory_fraction = 0.3 tf.keras.backend.set_session(tf.Session(config=config)); def pipeline(): parser = argparse.ArgumentParser(description='Train classifier and evaluate their accuracy') parser.add_argument('--data', type=str, help='datafile name') args = parser.parse_args() import joblib data = joblib.load(args.data) print(args.data) x, label = np.hsplit(data, [-10]) nb_classes = 10 label = label.reshape((label.shape[0], nb_classes), order='F') x = x.reshape(x.shape[0], 28, 28, 1) from keras.datasets import mnist (x_train, y_train), (x_test, y_test) = mnist.load_data() from keras.utils import np_utils y_train = np_utils.to_categorical(y_train, 10) y_test = np_utils.to_categorical(y_test, 10) x_train = x_train.reshape(x_train.shape[0], 28, 28, 1) x_train = x_train.astype('float32') / 255. x_test = x_test.reshape(x_test.shape[0], 28, 28, 1) x_test = x_test.astype('float32') / 255. from keras.models import Sequential from keras.layers.core import Dense, Dropout, Activation, Flatten from keras.layers.pooling import MaxPooling2D from keras.layers.convolutional import Convolution2D, Conv2D from keras.optimizers import Adam from keras import optimizers model = Sequential() model.add(Conv2D(32, kernel_size=3, activation='relu', input_shape=(28, 28, 1), name='Conv2D-1')) model.add(MaxPooling2D(pool_size=2, name='MaxPool')) model.add(Dropout(0.2, name='Dropout-1')) model.add(Conv2D(64, kernel_size=3, activation='relu', name='Conv2D-2')) model.add(Dropout(0.25, name='Dropout-2')) model.add(Flatten(name='flatten')) model.add(Dense(64, activation='relu', name='Dense')) model.add(Dense(nb_classes, activation='softmax', name='Output')) sgd = optimizers.sgd(lr=1e-3) model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy']) print(x.shape) print(label.shape) print(x_test.shape) print(y_test.shape) train_accs = [] eval_accs = [] # for i in range(70): history = model.fit(x, label, batch_size=512, epochs=600, validation_data=(x_test, y_test), shuffle=True) train_accs = history.history['acc'] eval_accs = history.history['val_acc'] return train_accs, eval_accs train_accs, eval_accs = pipeline() print("Max eval acc:", max(eval_accs)) print("Max train acc:", max(train_accs))
2,661
32.696203
109
py
G-PATE
G-PATE-master/evaluation/train-classifier-hair.py
#!/usr/bin/env python # coding: utf-8 # In[13]: import numpy as np import argparse import joblib import tensorflow as tf config = tf.ConfigProto() config.gpu_options.allow_growth = True # config.gpu_options.per_process_gpu_memory_fraction = 0.3 tf.keras.backend.set_session(tf.Session(config=config)); def load_celeb(): celebA_directory = '../../data/celebA/' tst_x = joblib.load(celebA_directory + 'celeb-tst-ups-hair-x.pkl') tst_y = joblib.load(celebA_directory + 'celeb-tst-ups-hair-y.pkl') print(tst_y.sum(), len(tst_y)) from keras.utils import np_utils tst_y = np_utils.to_categorical(tst_y, 3) return tst_x, tst_y def load_celeb_train(): celebA_directory = '../../data/celebA/' tst_x = joblib.load(celebA_directory + 'celeb-trn-ups-hair-x.pkl') tst_y = joblib.load(celebA_directory + 'celeb-trn-ups-hair-y.pkl') print(tst_y.sum(), len(tst_y)) from keras.utils import np_utils tst_y = np_utils.to_categorical(tst_y, 3) return tst_x, tst_y x_test, y_test = load_celeb() def pipeline(): parser = argparse.ArgumentParser(description='Train classifier and evaluate their accuracy') parser.add_argument('--data', type=str, help='datafile name') args = parser.parse_args() data = joblib.load(args.data) print(args.data) print(data.shape) x, label = np.hsplit(data, [-3]) nb_classes = 3 label = label.reshape((label.shape[0], nb_classes),order='F') x = x.reshape(x.shape[0], 64, 64, 3) from keras.models import Sequential from keras.layers.core import Dense, Dropout, Activation, Flatten from keras.layers.pooling import MaxPooling2D from keras.layers.convolutional import Convolution2D, Conv2D from keras.optimizers import Adam from keras import optimizers model = Sequential() model.add(Conv2D(32, kernel_size=3, activation='relu', input_shape=(64, 64, 3), name='Conv2D-1')) model.add(MaxPooling2D(pool_size=2, name='MaxPool')) model.add(Dropout(0.2, name='Dropout-1')) model.add(Conv2D(64, kernel_size=3, activation='relu', name='Conv2D-2')) model.add(Dropout(0.25, name='Dropout-2')) model.add(Flatten(name='flatten')) model.add(Dense(64, activation='relu', name='Dense')) model.add(Dense(nb_classes, activation='softmax', name='Output')) sgd = optimizers.sgd(lr=1e-4) model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy']) print(x.shape) print(label.shape) print(x_test.shape) print(y_test.shape) evals = model.fit(x, label, batch_size=256, epochs=250, validation_data=(x_test, y_test), shuffle=True) return evals.history train_accs, eval_accs = pipeline() print("Max eval acc:", max(eval_accs)) print("Max train acc:", max(train_accs))
2,820
29.010638
107
py
G-PATE
G-PATE-master/evaluation/train-classifier-small-celebA.py
#!/usr/bin/env python # coding: utf-8 import numpy as np import argparse parser = argparse.ArgumentParser(description='Train classifier and evaluate their accuracy') parser.add_argument('--data', type=str, help='datafile name') args = parser.parse_args() import joblib data = joblib.load(args.data) print(args.data) import tensorflow as tf config = tf.ConfigProto() config.gpu_options.allow_growth = True # config.gpu_options.per_process_gpu_memory_fraction = 0.3 tf.keras.backend.set_session(tf.Session(config=config)); def load_celeb(): celebA_directory = '../../data/celebA/' tst_x = joblib.load(celebA_directory + 'celebA-tst-x-small.pkl') tst_y = joblib.load(celebA_directory + 'celebA-tst-gender.pkl') print(tst_y.sum(), len(tst_y)) from keras.utils import np_utils tst_y = np_utils.to_categorical(tst_y, 2) return tst_x, tst_y x_test, y_test = load_celeb() def pipeline(data): print(data.shape) x, label = np.hsplit(data, [-2]) nb_classes = 2 label = label.reshape((label.shape[0], nb_classes),order='F') x = x.reshape(x.shape[0], 32, 32, 3) from keras.models import Sequential from keras.layers.core import Dense, Dropout, Activation, Flatten from keras.layers.pooling import MaxPooling2D from keras.layers.convolutional import Convolution2D, Conv2D from keras.optimizers import Adam from keras import optimizers model = Sequential() model.add(Conv2D(32, kernel_size=3, activation='relu', input_shape=(32, 32, 3), name='Conv2D-1')) model.add(MaxPooling2D(pool_size=2, name='MaxPool')) model.add(Dropout(0.2, name='Dropout-1')) model.add(Conv2D(64, kernel_size=3, activation='relu', name='Conv2D-2')) model.add(Dropout(0.25, name='Dropout-2')) model.add(Flatten(name='flatten')) model.add(Dense(64, activation='relu', name='Dense')) model.add(Dense(nb_classes, activation='softmax', name='Output')) sgd = optimizers.sgd(lr=1e-4) model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy']) print(x.shape) print(label.shape) print(x_test.shape) print(y_test.shape) evals = model.fit(x, label, batch_size=256, epochs=250, validation_data=(x_test, y_test), shuffle=True) return evals.history hist = pipeline(data) print("Max acc:", max(hist['val_accuracy']))
2,390
27.129412
107
py
kraken
kraken-main/kraken/align.py
# # Copyright 2021 Teklia # Copyright 2021 Benjamin Kiessling # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """ align ~~~~~ A character alignment module using a network output lattice and ground truth to accuractely determine grapheme locations in input data. """ import torch import logging import numpy as np from PIL import Image from bidi.algorithm import get_display from dataclasses import dataclass from typing import List, Dict, Any, Optional, Literal from kraken import rpred from kraken.lib.codec import PytorchCodec from kraken.lib.models import TorchSeqRecognizer from kraken.lib.exceptions import KrakenInputException, KrakenEncodeException from kraken.lib.segmentation import compute_polygon_section logger = logging.getLogger('kraken') def forced_align(doc: Dict[str, Any], model: TorchSeqRecognizer, base_dir: Optional[Literal['L', 'R']] = None) -> List[rpred.ocr_record]: """ Performs a forced character alignment of text with recognition model output activations. Argument: doc: Parsed document. model: Recognition model to use for alignment. Returns: A list of kraken.rpred.ocr_record. """ im = Image.open(doc['image']) predictor = rpred.rpred(model, im, doc) if 'type' in predictor.bounds and predictor.bounds['type'] == 'baselines': rec_class = rpred.BaselineOCRRecord records = [] # enable training mode in last layer to get log_softmax output model.nn.nn[-1].training = True for idx, line in enumerate(doc['lines']): # convert text to display order do_text = get_display(line['text'], base_dir=base_dir) # encode into labels, ignoring unencodable sequences labels = model.codec.encode(do_text).long() next(predictor) if model.outputs.shape[2] < 2*len(labels): logger.warning(f'Could not align line {idx}. Output sequence length {model.outputs.shape[2]} < ' f'{2*len(labels)} (length of "{line["text"]}" after encoding).') records.append(rpred.BaselineOCRRecord('', [], [], line)) continue emission = torch.tensor(model.outputs).squeeze().T trellis = get_trellis(emission, labels) path = backtrack(trellis, emission, labels) path = merge_repeats(path, do_text) pred = [] pos = [] conf = [] for seg in path: pred.append(seg.label) pos.append((predictor._scale_val(seg.start, 0, predictor.box.size[0]), predictor._scale_val(seg.end, 0, predictor.box.size[0]))) conf.append(seg.score) records.append(rpred.BaselineOCRRecord(pred, pos, conf, line, display_order=True)) return records """ Copied from the forced alignment with Wav2Vec2 tutorial of pytorch available at: https://github.com/pytorch/audio/blob/main/examples/tutorials/forced_alignment_tutorial.py """ @dataclass class Point: token_index: int time_index: int score: float # Merge the labels @dataclass class Segment: label: str start: int end: int score: float def __repr__(self): return f"{self.label}\t({self.score:4.2f}): [{self.start:5d}, {self.end:5d})" @property def length(self): return self.end - self.start def get_trellis(emission, tokens): # width x labels in log domain num_frame = emission.size(0) num_tokens = len(tokens) # Trellis has extra dimensions for both time axis and tokens. # The extra dim for tokens represents <SoS> (start-of-sentence) # The extra dim for time axis is for simplification of the code. trellis = torch.empty((num_frame + 1, num_tokens + 1)) trellis[0, 0] = 0 trellis[1:, 0] = torch.cumsum(emission[:, 0], 0) trellis[0, -num_tokens:] = -float("inf") trellis[-num_tokens:, 0] = float("inf") for t in range(num_frame): trellis[t + 1, 1:] = torch.maximum( # Score for staying at the same token trellis[t, 1:] + emission[t, 0], # Score for changing to the next token trellis[t, :-1] + emission[t, tokens], ) return trellis def backtrack(trellis, emission, tokens): # Note: # j and t are indices for trellis, which has extra dimensions # for time and tokens at the beginning. # When referring to time frame index `T` in trellis, # the corresponding index in emission is `T-1`. # Similarly, when referring to token index `J` in trellis, # the corresponding index in transcript is `J-1`. j = trellis.size(1) - 1 t_start = torch.argmax(trellis[:, j]).item() path = [] for t in range(t_start, 0, -1): # 1. Figure out if the current position was stay or change # Note (again): # `emission[J-1]` is the emission at time frame `J` of trellis dimension. # Score for token staying the same from time frame J-1 to T. stayed = trellis[t - 1, j] + emission[t - 1, 0] # Score for token changing from C-1 at T-1 to J at T. changed = trellis[t - 1, j - 1] + emission[t - 1, tokens[j - 1]] # 2. Store the path with frame-wise probability. prob = emission[t - 1, tokens[j - 1] if changed > stayed else 0].exp().item() # Return token index and time index in non-trellis coordinate. path.append(Point(j - 1, t - 1, prob)) # 3. Update the token if changed > stayed: j -= 1 if j == 0: break else: raise ValueError("Failed to align") return path[::-1] def merge_repeats(path, ground_truth): i1, i2 = 0, 0 segments = [] while i1 < len(path): while i2 < len(path) and path[i1].token_index == path[i2].token_index: i2 += 1 score = sum(path[k].score for k in range(i1, i2)) / (i2 - i1) segments.append( Segment( ground_truth[path[i1].token_index], path[i1].time_index, path[i2 - 1].time_index + 1, score, ) ) i1 = i2 return segments
6,614
32.75
137
py
kraken
kraken-main/kraken/blla.py
# # Copyright 2019 Benjamin Kiessling # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express # or implied. See the License for the specific language governing # permissions and limitations under the License. """ kraken.blla ~~~~~~~~~~~ Trainable layout analysis tools for kraken for line and region detection. The line recognizer uses the baseline paradigm. """ import PIL import torch import logging import numpy as np import pkg_resources import shapely.geometry as geom import torch.nn.functional as F import torchvision.transforms as tf from typing import Optional, Dict, Callable, Union, List, Any, Tuple from scipy.ndimage import gaussian_filter from skimage.filters import sobel from kraken.lib import vgsl, dataset from kraken.lib.util import is_bitonal, get_im_str from kraken.lib.exceptions import KrakenInputException, KrakenInvalidModelException from kraken.lib.segmentation import (polygonal_reading_order, vectorize_lines, vectorize_regions, scale_polygonal_lines, calculate_polygonal_environment, scale_regions) __all__ = ['segment'] logger = logging.getLogger(__name__) def compute_segmentation_map(im: PIL.Image.Image, mask: Optional[np.ndarray] = None, model: vgsl.TorchVGSLModel = None, device: str = 'cpu', autocast: bool = False) -> Dict[str, Any]: """ Args: im: Input image mask: A bi-level mask array of the same size as `im` where 0-valued regions are ignored for segmentation purposes. Disables column detection. model: A TorchVGSLModel containing a segmentation model. device: The target device to run the neural network on. autocast: Runs the model with automatic mixed precision Returns: A dictionary containing the heatmaps ('heatmap', torch.Tensor), class map ('cls_map', Dict[str, Dict[str, int]]), the bounding regions for polygonization purposes ('bounding_regions', List[str]), the scale between the input image and the network output ('scale', float), and the scaled input image to the network ('scal_im', PIL.Image.Image). Raises: KrakenInputException: When given an invalid mask. """ im_str = get_im_str(im) logger.info(f'Segmenting {im_str}') if model.input[1] == 1 and model.one_channel_mode == '1' and not is_bitonal(im): logger.warning('Running binary model on non-binary input image ' '(mode {}). This will result in severely degraded ' 'performance'.format(im.mode)) model.eval() model.to(device) batch, channels, height, width = model.input padding = model.user_metadata['hyper_params']['padding'] if 'padding' in model.user_metadata['hyper_params'] else (0, 0) # expand padding to 4-tuple (left, right, top, bottom) if isinstance(padding, int): padding = (padding,) * 4 elif len(padding) == 2: padding = (padding[0], padding[0], padding[1], padding[1]) transforms = dataset.ImageInputTransforms(batch, height, width, channels, padding, valid_norm=False) tf_idx, _ = next(filter(lambda x: isinstance(x[1], tf.ToTensor), enumerate(transforms.transforms))) res_tf = tf.Compose(transforms.transforms[:tf_idx]) scal_im = np.array(res_tf(im).convert('L')) tensor_im = transforms(im) if mask: if mask.mode != '1' and not is_bitonal(mask): logger.error('Mask is not bitonal') raise KrakenInputException('Mask is not bitonal') mask = mask.convert('1') if mask.size != im.size: logger.error('Mask size {mask.size} doesn\'t match image size {im.size}') raise KrakenInputException('Mask size {mask.size} doesn\'t match image size {im.size}') logger.info('Masking enabled in segmenter.') tensor_im[~transforms(mask).bool()] = 0 with torch.autocast(device_type=device.split(":")[0], enabled=autocast): with torch.no_grad(): logger.debug('Running network forward pass') o, _ = model.nn(tensor_im.unsqueeze(0).to(device)) logger.debug('Upsampling network output') o = F.interpolate(o, size=scal_im.shape) # remove padding padding = [pad if pad else None for pad in padding] padding[1] = -padding[1] if padding[1] else None padding[3] = -padding[3] if padding[3] else None o = o[:, :, padding[2]:padding[3], padding[0]:padding[1]] scal_im = scal_im[padding[2]:padding[3], padding[0]:padding[1]] o = o.squeeze().cpu().float().numpy() scale = np.divide(im.size, o.shape[:0:-1]) bounding_regions = model.user_metadata['bounding_regions'] if 'bounding_regions' in model.user_metadata else None return {'heatmap': o, 'cls_map': model.user_metadata['class_mapping'], 'bounding_regions': bounding_regions, 'scale': scale, 'scal_im': scal_im} def vec_regions(heatmap: torch.Tensor, cls_map: Dict, scale: float, **kwargs) -> Dict[str, List[List[Tuple[int, int]]]]: """ Computes regions from a stack of heatmaps, a class mapping, and scaling factor. Args: heatmap: A stack of heatmaps of shape `NxHxW` output from the network. cls_map: Dictionary mapping string identifiers to indices on the stack of heatmaps. scale: Scaling factor between heatmap and unscaled input image. Returns: A dictionary containing a key for each region type with a list of regions inside. """ logger.info('Vectorizing regions') regions = {} for region_type, idx in cls_map['regions'].items(): logger.debug(f'Vectorizing regions of type {region_type}') regions[region_type] = vectorize_regions(heatmap[idx]) for reg_id, regs in regions.items(): regions[reg_id] = scale_regions(regs, scale) return regions def vec_lines(heatmap: torch.Tensor, cls_map: Dict[str, Dict[str, int]], scale: float, text_direction: str = 'horizontal-lr', reading_order_fn: Callable = polygonal_reading_order, regions: List[np.ndarray] = None, scal_im: np.ndarray = None, suppl_obj: List[np.ndarray] = None, topline: Optional[bool] = False, raise_on_error: bool = False, **kwargs) -> List[Dict[str, Any]]: r""" Computes lines from a stack of heatmaps, a class mapping, and scaling factor. Args: heatmap: A stack of heatmaps of shape `NxHxW` output from the network. cls_map: Dictionary mapping string identifiers to indices on the stack of heatmaps. scale: Scaling factor between heatmap and unscaled input image. text_direction: Text directions used as hints in the reading order algorithm. reading_order_fn: Reading order calculation function. regions: Regions to be used as boundaries during polygonization and atomic blocks during reading order determination for lines contained within. scal_im: A numpy array containing the scaled input image. suppl_obj: Supplementary objects which are used as boundaries during polygonization. topline: True for a topline, False for baseline, or None for a centerline. raise_on_error: Raises error instead of logging them when they are not-blocking Returns: A list of dictionaries containing the baselines, bounding polygons, and line type in reading order: .. code-block:: :force: [{'script': '$baseline_type', baseline': [[x0, y0], [x1, y1], ..., [x_n, y_n]], 'boundary': [[x0, y0, x1, y1], ... [x_m, y_m]]}, {'script': '$baseline_type', baseline': [[x0, ...]], 'boundary': [[x0, ...]]}, {'script': '$baseline_type', baseline': [[x0, ...]], 'boundary': [[x0, ...]]}, ... ] """ st_sep = cls_map['aux']['_start_separator'] end_sep = cls_map['aux']['_end_separator'] logger.info('Vectorizing baselines') baselines = [] for bl_type, idx in cls_map['baselines'].items(): logger.debug(f'Vectorizing lines of type {bl_type}') baselines.extend([(bl_type, x) for x in vectorize_lines(heatmap[(st_sep, end_sep, idx), :, :], text_direction=text_direction[:-3])]) logger.debug('Polygonizing lines') im_feats = gaussian_filter(sobel(scal_im), 0.5) lines = [] reg_pols = [geom.Polygon(x) for x in regions] for bl_idx in range(len(baselines)): bl = baselines[bl_idx] mid_point = geom.LineString(bl[1]).interpolate(0.5, normalized=True) suppl_obj = [x[1] for x in baselines[:bl_idx] + baselines[bl_idx+1:]] for reg_idx, reg_pol in enumerate(reg_pols): if reg_pol.contains(mid_point): suppl_obj.append(regions[reg_idx]) pol = calculate_polygonal_environment( baselines=[bl[1]], im_feats=im_feats, suppl_obj=suppl_obj, topline=topline, raise_on_error=raise_on_error ) if pol[0] is not None: lines.append((bl[0], bl[1], pol[0])) logger.debug('Scaling vectorized lines') sc = scale_polygonal_lines([x[1:] for x in lines], scale) lines = list(zip([x[0] for x in lines], [x[0] for x in sc], [x[1] for x in sc])) logger.debug('Reordering baselines') lines = reading_order_fn(lines=lines, regions=regions, text_direction=text_direction[-2:]) return [{'tags': {'type': bl_type}, 'baseline': bl, 'boundary': pl} for bl_type, bl, pl in lines] def segment(im: PIL.Image.Image, text_direction: str = 'horizontal-lr', mask: Optional[np.ndarray] = None, reading_order_fn: Callable = polygonal_reading_order, model: Union[List[vgsl.TorchVGSLModel], vgsl.TorchVGSLModel] = None, device: str = 'cpu', raise_on_error: bool = False, autocast: bool = False) -> Dict[str, Any]: r""" Segments a page into text lines using the baseline segmenter. Segments a page into text lines and returns the polyline formed by each baseline and their estimated environment. Args: im: Input image. The mode can generally be anything but it is possible to supply a binarized-input-only model which requires accordingly treated images. text_direction: Passed-through value for serialization.serialize. mask: A bi-level mask image of the same size as `im` where 0-valued regions are ignored for segmentation purposes. Disables column detection. reading_order_fn: Function to determine the reading order. Has to accept a list of tuples (baselines, polygon) and a text direction (`lr` or `rl`). model: One or more TorchVGSLModel containing a segmentation model. If none is given a default model will be loaded. device: The target device to run the neural network on. raise_on_error: Raises error instead of logging them when they are not-blocking autocast: Runs the model with automatic mixed precision Returns: A dictionary containing the text direction and under the key 'lines' a list of reading order sorted baselines (polylines) and their respective polygonal boundaries. The last and first point of each boundary polygon are connected. .. code-block:: :force: {'text_direction': '$dir', 'type': 'baseline', 'lines': [ {'baseline': [[x0, y0], [x1, y1], ..., [x_n, y_n]], 'boundary': [[x0, y0, x1, y1], ... [x_m, y_m]]}, {'baseline': [[x0, ...]], 'boundary': [[x0, ...]]} ] 'regions': [ {'region': [[x0, y0], [x1, y1], ..., [x_n, y_n]], 'type': 'image'}, {'region': [[x0, ...]], 'type': 'text'} ] } Raises: KrakenInvalidModelException: if the given model is not a valid segmentation model. KrakenInputException: if the mask is not bitonal or does not match the image size. """ if model is None: logger.info('No segmentation model given. Loading default model.') model = vgsl.TorchVGSLModel.load_model(pkg_resources.resource_filename(__name__, 'blla.mlmodel')) if isinstance(model, vgsl.TorchVGSLModel): model = [model] for nn in model: if nn.model_type != 'segmentation': raise KrakenInvalidModelException(f'Invalid model type {nn.model_type} for {nn}') if 'class_mapping' not in nn.user_metadata: raise KrakenInvalidModelException(f'Segmentation model {nn} does not contain valid class mapping') im_str = get_im_str(im) logger.info(f'Segmenting {im_str}') for net in model: if 'topline' in net.user_metadata: loc = {None: 'center', True: 'top', False: 'bottom'}[net.user_metadata['topline']] logger.debug(f'Baseline location: {loc}') rets = compute_segmentation_map(im, mask, net, device, autocast=autocast) regions = vec_regions(**rets) # flatten regions for line ordering/fetch bounding regions line_regs = [] suppl_obj = [] for cls, regs in regions.items(): line_regs.extend(regs) if rets['bounding_regions'] is not None and cls in rets['bounding_regions']: suppl_obj.extend(regs) # convert back to net scale suppl_obj = scale_regions(suppl_obj, 1/rets['scale']) line_regs = scale_regions(line_regs, 1/rets['scale']) lines = vec_lines(**rets, regions=line_regs, reading_order_fn=reading_order_fn, text_direction=text_direction, suppl_obj=suppl_obj, topline=net.user_metadata['topline'] if 'topline' in net.user_metadata else False, raise_on_error=raise_on_error) if len(rets['cls_map']['baselines']) > 1: script_detection = True else: script_detection = False return {'text_direction': text_direction, 'type': 'baselines', 'lines': lines, 'regions': regions, 'script_detection': script_detection}
15,295
41.371191
140
py
kraken
kraken-main/kraken/kraken.py
# # Copyright 2015 Benjamin Kiessling # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express # or implied. See the License for the specific language governing # permissions and limitations under the License. """ kraken.kraken ~~~~~~~~~~~~~ Command line drivers for recognition functionality. """ import os import warnings import logging import pkg_resources from typing import Dict, Union, List, cast, Any, IO, Callable from pathlib import Path from rich.traceback import install from functools import partial from PIL import Image import click from kraken.lib import log from kraken.lib.progress import KrakenProgressBar, KrakenDownloadProgressBar warnings.simplefilter('ignore', UserWarning) logging.captureWarnings(True) logger = logging.getLogger('kraken') # install rich traceback handler install(suppress=[click]) APP_NAME = 'kraken' SEGMENTATION_DEFAULT_MODEL = pkg_resources.resource_filename(__name__, 'blla.mlmodel') DEFAULT_MODEL = ['en_best.mlmodel'] LEGACY_MODEL_DIR = '/usr/local/share/ocropus' # raise default max image size to 20k * 20k pixels Image.MAX_IMAGE_PIXELS = 20000 ** 2 def message(msg: str, **styles) -> None: if logger.getEffectiveLevel() >= 30: click.secho(msg, **styles) def get_input_parser(type_str: str) -> Callable[[str], Dict[str, Any]]: if type_str == 'alto': from kraken.lib.xml import parse_alto return parse_alto elif type_str == 'page': from kraken.lib.xml import parse_page return parse_page elif type_str == 'xml': from kraken.lib.xml import parse_xml return parse_xml elif type_str == 'image': return Image.open # chainable functions of functional components (binarization/segmentation/recognition) def binarizer(threshold, zoom, escale, border, perc, range, low, high, input, output) -> None: from kraken import binarization ctx = click.get_current_context() if ctx.meta['first_process']: if ctx.meta['input_format_type'] != 'image': input = get_input_parser(ctx.meta['input_format_type'])(input)['image'] ctx.meta['first_process'] = False else: raise click.UsageError('Binarization has to be the initial process.') try: im = Image.open(input) except IOError as e: raise click.BadParameter(str(e)) message('Binarizing\t', nl=False) try: res = binarization.nlbin(im, threshold, zoom, escale, border, perc, range, low, high) if ctx.meta['last_process'] and ctx.meta['output_mode'] != 'native': with click.open_file(output, 'w', encoding='utf-8') as fp: fp = cast(IO[Any], fp) logger.info('Serializing as {} into {}'.format(ctx.meta['output_mode'], output)) res.save(f'{output}.png') from kraken import serialization fp.write(serialization.serialize([], image_name=f'{output}.png', image_size=res.size, template=ctx.meta['output_template'], template_source='custom' if ctx.meta['output_mode'] == 'template' else 'native', processing_steps=ctx.meta['steps'])) else: form = None ext = os.path.splitext(output)[1] if ext in ['.jpg', '.jpeg', '.JPG', '.JPEG', '']: form = 'png' if ext: logger.warning('jpeg does not support 1bpp images. Forcing to png.') res.save(output, format=form) ctx.meta['base_image'] = output except Exception: if ctx.meta['raise_failed']: raise message('\u2717', fg='red') ctx.exit(1) message('\u2713', fg='green') def segmenter(legacy, model, text_direction, scale, maxcolseps, black_colseps, remove_hlines, pad, mask, device, input, output) -> None: import json from kraken import pageseg from kraken import blla ctx = click.get_current_context() if ctx.meta['first_process']: if ctx.meta['input_format_type'] != 'image': input = get_input_parser(ctx.meta['input_format_type'])(input)['image'] ctx.meta['first_process'] = False if 'base_image' not in ctx.meta: ctx.meta['base_image'] = input try: im = Image.open(input) except IOError as e: raise click.BadParameter(str(e)) if mask: try: mask = Image.open(mask) except IOError as e: raise click.BadParameter(str(e)) message('Segmenting\t', nl=False) try: if legacy: res = pageseg.segment(im, text_direction, scale, maxcolseps, black_colseps, no_hlines=remove_hlines, pad=pad, mask=mask) else: res = blla.segment(im, text_direction, mask=mask, model=model, device=device, raise_on_error=ctx.meta['raise_failed'], autocast=ctx.meta["autocast"]) except Exception: if ctx.meta['raise_failed']: raise message('\u2717', fg='red') ctx.exit(1) if ctx.meta['last_process'] and ctx.meta['output_mode'] != 'native': with click.open_file(output, 'w', encoding='utf-8') as fp: fp = cast(IO[Any], fp) logger.info('Serializing as {} into {}'.format(ctx.meta['output_mode'], output)) from kraken import serialization fp.write(serialization.serialize_segmentation(res, image_name=ctx.meta['base_image'], image_size=im.size, template=ctx.meta['output_template'], template_source='custom' if ctx.meta['output_mode'] == 'template' else 'native', processing_steps=ctx.meta['steps'])) else: with click.open_file(output, 'w') as fp: fp = cast(IO[Any], fp) json.dump(res, fp) message('\u2713', fg='green') def recognizer(model, pad, no_segmentation, bidi_reordering, tags_ignore, input, output) -> None: import json from kraken import rpred ctx = click.get_current_context() bounds = None if 'base_image' not in ctx.meta: ctx.meta['base_image'] = input if ctx.meta['first_process']: if ctx.meta['input_format_type'] != 'image': doc = get_input_parser(ctx.meta['input_format_type'])(input) ctx.meta['base_image'] = doc['image'] doc['text_direction'] = 'horizontal-lr' if doc['base_dir'] and bidi_reordering is True: message(f'Setting base text direction for BiDi reordering to {doc["base_dir"]} (from XML input file)') bidi_reordering = doc['base_dir'] bounds = doc try: im = Image.open(ctx.meta['base_image']) except IOError as e: raise click.BadParameter(str(e)) if not bounds and ctx.meta['base_image'] != input: with click.open_file(input, 'r') as fp: try: fp = cast(IO[Any], fp) bounds = json.load(fp) except ValueError as e: raise click.UsageError(f'{input} invalid segmentation: {str(e)}') elif not bounds: if no_segmentation: bounds = {'script_detection': False, 'text_direction': 'horizontal-lr', 'boxes': [(0, 0) + im.size]} else: raise click.UsageError('No line segmentation given. Add one with the input or run `segment` first.') elif no_segmentation: logger.warning('no_segmentation mode enabled but segmentation defined. Ignoring --no-segmentation option.') tags = set() # script detection if 'script_detection' in bounds and bounds['script_detection']: it = rpred.mm_rpred(model, im, bounds, pad, bidi_reordering=bidi_reordering, tags_ignore=tags_ignore) else: it = rpred.rpred(model['default'], im, bounds, pad, bidi_reordering=bidi_reordering) preds = [] with KrakenProgressBar() as progress: pred_task = progress.add_task('Processing', total=len(it), visible=True if not ctx.meta['verbose'] else False) for pred in it: preds.append(pred) progress.update(pred_task, advance=1) ctx = click.get_current_context() with click.open_file(output, 'w', encoding='utf-8') as fp: fp = cast(IO[Any], fp) message(f'Writing recognition results for {ctx.meta["orig_file"]}\t', nl=False) logger.info('Serializing as {} into {}'.format(ctx.meta['output_mode'], output)) if ctx.meta['output_mode'] != 'native': from kraken import serialization fp.write(serialization.serialize(records=preds, image_name=ctx.meta['base_image'], image_size=Image.open(ctx.meta['base_image']).size, writing_mode=ctx.meta['text_direction'], scripts=tags, regions=bounds['regions'] if 'regions' in bounds else None, template=ctx.meta['output_template'], template_source='custom' if ctx.meta['output_mode'] == 'template' else 'native', processing_steps=ctx.meta['steps'])) else: fp.write('\n'.join(s.prediction for s in preds)) message('\u2713', fg='green') @click.group(chain=True) @click.version_option() @click.option('-i', '--input', type=(click.Path(exists=True, dir_okay=False, path_type=Path), # type: ignore click.Path(writable=True, dir_okay=False, path_type=Path)), multiple=True, help='Input-output file pairs. Each input file (first argument) is mapped to one ' 'output file (second argument), e.g. `-i input.png output.txt`') @click.option('-I', '--batch-input', multiple=True, help='Glob expression to add multiple files at once.') @click.option('-o', '--suffix', default='', show_default=True, help='Suffix for output files from batch and PDF inputs.') @click.option('-v', '--verbose', default=0, count=True, show_default=True) @click.option('-f', '--format-type', type=click.Choice(['image', 'alto', 'page', 'pdf', 'xml']), default='image', help='Sets the default input type. In image mode inputs are image ' 'files, alto/page expects XML files in the respective format, pdf ' 'expects PDF files with numbered suffixes added to output file ' 'names as needed.') @click.option('-p', '--pdf-format', default='{src}_{idx:06d}', show_default=True, help='Format for output of PDF files. valid fields ' 'are `src` (source file), `idx` (page number), and `uuid` (v4 uuid). ' '`-o` suffixes are appended to this format string.') @click.option('-h', '--hocr', 'serializer', help='Switch between hOCR, ALTO, abbyyXML, PageXML or "native" ' 'output. Native are plain image files for image, JSON for ' 'segmentation, and text for transcription output.', flag_value='hocr') @click.option('-a', '--alto', 'serializer', flag_value='alto') @click.option('-y', '--abbyy', 'serializer', flag_value='abbyyxml') @click.option('-x', '--pagexml', 'serializer', flag_value='pagexml') @click.option('-n', '--native', 'serializer', flag_value='native', default=True, show_default=True) @click.option('-t', '--template', type=click.Path(exists=True, dir_okay=False), help='Explicitly set jinja template for output serialization. Overrides -h/-a/-y/-x/-n.') @click.option('-d', '--device', default='cpu', show_default=True, help='Select device to use (cpu, cuda:0, cuda:1, ...)') @click.option('-r', '--raise-on-error/--no-raise-on-error', default=False, show_default=True, help='Raises the exception that caused processing to fail in the case of an error') @click.option('-2', '--autocast', default=False, show_default=True, flag_value=True, help='On compatible devices, uses autocast for `segment` which lower the memory usage.') def cli(input, batch_input, suffix, verbose, format_type, pdf_format, serializer, template, device, raise_on_error, autocast): """ Base command for recognition functionality. Inputs are defined as one or more pairs `-i input_file output_file` followed by one or more chainable processing commands. Likewise, verbosity is set on all subcommands with the `-v` switch. """ ctx = click.get_current_context() if device != 'cpu': import torch try: torch.ones(1, device=device) except AssertionError as e: if raise_on_error: raise logger.error(f'Device {device} not available: {e.args[0]}.') ctx.exit(1) ctx.meta['device'] = device ctx.meta['input_format_type'] = format_type if format_type != 'pdf' else 'image' ctx.meta['raise_failed'] = raise_on_error if not template: ctx.meta['output_mode'] = serializer ctx.meta['output_template'] = serializer else: ctx.meta['output_mode'] = 'template' ctx.meta['output_template'] = template ctx.meta['verbose'] = verbose ctx.meta['steps'] = [] ctx.meta["autocast"] = autocast log.set_logger(logger, level=30 - min(10 * verbose, 20)) @cli.result_callback() def process_pipeline(subcommands, input, batch_input, suffix, verbose, format_type, pdf_format, **args): """ Helper function calling the partials returned by each subcommand and placing their respective outputs in temporary files. """ import glob import uuid import tempfile ctx = click.get_current_context() input = list(input) # expand batch inputs if batch_input and suffix: for batch_expr in batch_input: for in_file in glob.glob(batch_expr, recursive=True): input.append((in_file, '{}{}'.format(os.path.splitext(in_file)[0], suffix))) # parse pdfs if format_type == 'pdf': import pyvips if not batch_input: logger.warning('PDF inputs not added with batch option. Manual output filename will be ignored and `-o` utilized.') new_input = [] num_pages = 0 for (fpath, _) in input: doc = pyvips.Image.new_from_file(fpath, dpi=300, n=-1, access="sequential") if 'n-pages' in doc.get_fields(): num_pages += doc.get('n-pages') with KrakenProgressBar() as progress: pdf_parse_task = progress.add_task('Extracting PDF pages', total=num_pages, visible=True if not ctx.meta['verbose'] else False) for (fpath, _) in input: try: doc = pyvips.Image.new_from_file(fpath, dpi=300, n=-1, access="sequential") if 'n-pages' not in doc.get_fields(): logger.warning('{fpath} does not contain pages. Skipping.') continue n_pages = doc.get('n-pages') dest_dict = {'idx': -1, 'src': fpath, 'uuid': None} for i in range(0, n_pages): dest_dict['idx'] += 1 dest_dict['uuid'] = str(uuid.uuid4()) fd, filename = tempfile.mkstemp(suffix='.png') os.close(fd) doc = pyvips.Image.new_from_file(fpath, dpi=300, page=i, access="sequential") logger.info(f'Saving temporary image {fpath}:{dest_dict["idx"]} to {filename}') doc.write_to_file(filename) new_input.append((filename, pdf_format.format(**dest_dict) + suffix)) progress.update(pdf_parse_task, advance=1) except pyvips.error.Error: num_pages -= n_pages progress.update(pdf_parse_task, total=num_pages) logger.warning(f'{fpath} is not a PDF file. Skipping.') input = new_input ctx.meta['steps'].insert(0, {'category': 'preprocessing', 'description': 'PDF image extraction', 'settings': {}}) for io_pair in input: ctx.meta['first_process'] = True ctx.meta['last_process'] = False ctx.meta['orig_file'] = io_pair[0] if 'base_image' in ctx.meta: del ctx.meta['base_image'] try: tmps = [tempfile.mkstemp() for _ in subcommands[1:]] for tmp in tmps: os.close(tmp[0]) fc = [io_pair[0]] + [tmp[1] for tmp in tmps] + [io_pair[1]] for idx, (task, input, output) in enumerate(zip(subcommands, fc, fc[1:])): if len(fc) - 2 == idx: ctx.meta['last_process'] = True task(input=input, output=output) except Exception as e: logger.error(f'Failed processing {io_pair[0]}: {str(e)}') if ctx.meta['raise_failed']: raise finally: for f in fc[1:-1]: os.unlink(f) # clean up temporary PDF image files if format_type == 'pdf': logger.debug(f'unlinking {fc[0]}') os.unlink(fc[0]) @cli.command('binarize') @click.pass_context @click.option('--threshold', show_default=True, default=0.5, type=click.FLOAT) @click.option('--zoom', show_default=True, default=0.5, type=click.FLOAT) @click.option('--escale', show_default=True, default=1.0, type=click.FLOAT) @click.option('--border', show_default=True, default=0.1, type=click.FLOAT) @click.option('--perc', show_default=True, default=80, type=click.IntRange(1, 100)) @click.option('--range', show_default=True, default=20, type=click.INT) @click.option('--low', show_default=True, default=5, type=click.IntRange(1, 100)) @click.option('--high', show_default=True, default=90, type=click.IntRange(1, 100)) def binarize(ctx, threshold, zoom, escale, border, perc, range, low, high): """ Binarizes page images. """ ctx.meta['steps'].append({'category': 'preprocessing', 'description': 'Image binarization', 'settings': {'threshold': threshold, 'zoom': zoom, 'escale': escale, 'border': border, 'perc': perc, 'range': range, 'low': low, 'high': high}}) return partial(binarizer, threshold, zoom, escale, border, perc, range, low, high) @cli.command('segment') @click.pass_context @click.option('-i', '--model', default=None, show_default=True, help='Baseline detection model to use') @click.option('-x/-bl', '--boxes/--baseline', default=True, show_default=True, help='Switch between legacy box segmenter and neural baseline segmenter') @click.option('-d', '--text-direction', default='horizontal-lr', show_default=True, type=click.Choice(['horizontal-lr', 'horizontal-rl', 'vertical-lr', 'vertical-rl']), help='Sets principal text direction') @click.option('--scale', show_default=True, default=None, type=click.FLOAT) @click.option('-m', '--maxcolseps', show_default=True, default=2, type=click.INT) @click.option('-b/-w', '--black-colseps/--white_colseps', show_default=True, default=False) @click.option('-r/-l', '--remove_hlines/--hlines', show_default=True, default=True) @click.option('-p', '--pad', show_default=True, type=(int, int), default=(0, 0), help='Left and right padding around lines') @click.option('-m', '--mask', show_default=True, default=None, type=click.File(mode='rb', lazy=True), help='Segmentation mask ' 'suppressing page areas for line detection. 0-valued image ' 'regions are ignored for segmentation purposes. Disables column ' 'detection.') def segment(ctx, model, boxes, text_direction, scale, maxcolseps, black_colseps, remove_hlines, pad, mask): """ Segments page images into text lines. """ if model and boxes: logger.warning(f'Baseline model ({model}) given but legacy segmenter selected. Forcing to -bl.') boxes = False if boxes is False: if not model: model = SEGMENTATION_DEFAULT_MODEL ctx.meta['steps'].append({'category': 'processing', 'description': 'Baseline and region segmentation', 'settings': {'model': os.path.basename(model), 'text_direction': text_direction}}) # first try to find the segmentation model by its given name, # then look in the kraken config folder location = None search = [model, os.path.join(click.get_app_dir(APP_NAME), model)] for loc in search: if os.path.isfile(loc): location = loc break if not location: raise click.BadParameter(f'No model for {model} found') from kraken.lib.vgsl import TorchVGSLModel message(f'Loading ANN {model}\t', nl=False) try: model = TorchVGSLModel.load_model(location) model.to(ctx.meta['device']) except Exception: if ctx.meta['raise_failed']: raise message('\u2717', fg='red') ctx.exit(1) message('\u2713', fg='green') else: ctx.meta['steps'].append({'category': 'processing', 'description': 'bounding box segmentation', 'settings': {'text_direction': text_direction, 'scale': scale, 'maxcolseps': maxcolseps, 'black_colseps': black_colseps, 'remove_hlines': remove_hlines, 'pad': pad}}) return partial(segmenter, boxes, model, text_direction, scale, maxcolseps, black_colseps, remove_hlines, pad, mask, ctx.meta['device']) def _validate_mm(ctx, param, value): """ Maps model mappings to a dictionary. """ model_dict = {'ignore': []} # type: Dict[str, Union[str, List[str]]] if len(value) == 1 and len(value[0].split(':')) == 1: model_dict['default'] = value[0] return model_dict try: for m in value: k, v = m.split(':') if v == 'ignore': model_dict['ignore'].append(k) # type: ignore else: model_dict[k] = os.path.expanduser(v) except Exception: raise click.BadParameter('Mappings must be in format tag:model') return model_dict @cli.command('ocr') @click.pass_context @click.option('-m', '--model', default=DEFAULT_MODEL, multiple=True, show_default=True, callback=_validate_mm, help='Path to an recognition model or mapping of the form ' '$tag1:$model1. Add multiple mappings to run multi-model ' 'recognition based on detected tags. Use the default keyword ' 'for adding a catch-all model. Recognition on tags can be ' 'ignored with the model value ignore.') @click.option('-p', '--pad', show_default=True, type=click.INT, default=16, help='Left and right ' 'padding around lines') @click.option('-n', '--reorder/--no-reorder', show_default=True, default=True, help='Reorder code points to logical order') @click.option('--base-dir', show_default=True, default='auto', type=click.Choice(['L', 'R', 'auto']), help='Set base text ' 'direction. This should be set to the direction used during the ' 'creation of the training data. If set to `auto` it will be ' 'overridden by any explicit value given in the input files.') @click.option('-s', '--no-segmentation', default=False, show_default=True, is_flag=True, help='Enables non-segmentation mode treating each input image as a whole line.') @click.option('-d', '--text-direction', default='horizontal-tb', show_default=True, type=click.Choice(['horizontal-tb', 'vertical-lr', 'vertical-rl']), help='Sets principal text direction in serialization output') @click.option('--threads', default=1, show_default=True, type=click.IntRange(1), help='Number of threads to use for OpenMP parallelization.') def ocr(ctx, model, pad, reorder, base_dir, no_segmentation, text_direction, threads): """ Recognizes text in line images. """ from kraken.lib import models if ctx.meta['input_format_type'] != 'image' and no_segmentation: raise click.BadParameter('no_segmentation mode is incompatible with page/alto inputs') if reorder and base_dir != 'auto': reorder = base_dir # first try to find the OCR model by its given name, then # in the kraken config folder, then in LEGACY_MODEL_DIR nm = {} # type: Dict[str, models.TorchSeqRecognizer] ign_tags = model.pop('ignore') for k, v in model.items(): search = [v, os.path.join(click.get_app_dir(APP_NAME), v), os.path.join(LEGACY_MODEL_DIR, v)] location = None for loc in search: if os.path.isfile(loc): location = loc break if not location: raise click.BadParameter(f'No model for {v} found') message(f'Loading ANN {v}\t', nl=False) try: rnn = models.load_any(location, device=ctx.meta['device']) nm[k] = rnn except Exception: if ctx.meta['raise_failed']: raise message('\u2717', fg='red') ctx.exit(1) message('\u2713', fg='green') if 'default' in nm: from collections import defaultdict nn = defaultdict(lambda: nm['default']) # type: Dict[str, models.TorchSeqRecognizer] nn.update(nm) nm = nn # thread count is global so setting it once is sufficient nm[k].nn.set_num_threads(threads) ctx.meta['steps'].append({'category': 'processing', 'description': 'Text line recognition', 'settings': {'text_direction': text_direction, 'models': ' '.join(os.path.basename(v) for v in model.values()), 'pad': pad, 'bidi_reordering': reorder}}) # set output mode ctx.meta['text_direction'] = text_direction return partial(recognizer, model=nm, pad=pad, no_segmentation=no_segmentation, bidi_reordering=reorder, tags_ignore=ign_tags) @cli.command('show') @click.pass_context @click.argument('model_id') def show(ctx, model_id): """ Retrieves model metadata from the repository. """ from kraken import repo from kraken.lib.util import make_printable, is_printable desc = repo.get_description(model_id) chars = [] combining = [] for char in sorted(desc['graphemes']): if not is_printable(char): combining.append(make_printable(char)) else: chars.append(char) message( 'name: {}\n\n{}\n\n{}\nscripts: {}\nalphabet: {} {}\naccuracy: {:.2f}%\nlicense: {}\nauthor(s): {}\ndate: {}'.format( model_id, desc['summary'], desc['description'], ' '.join( desc['script']), ''.join(chars), ', '.join(combining), desc['accuracy'], desc['license']['id'], '; '.join( x['name'] for x in desc['creators']), desc['publication_date'])) ctx.exit(0) @cli.command('list') @click.pass_context def list_models(ctx): """ Lists models in the repository. """ from kraken import repo with KrakenProgressBar() as progress: download_task = progress.add_task('Retrieving model list', total=0, visible=True if not ctx.meta['verbose'] else False) model_list = repo.get_listing(lambda total, advance: progress.update(download_task, total=total, advance=advance)) for id, metadata in model_list.items(): message('{} ({}) - {}'.format(id, ', '.join(metadata['type']), metadata['summary'])) ctx.exit(0) @cli.command('get') @click.pass_context @click.argument('model_id') def get(ctx, model_id): """ Retrieves a model from the repository. """ from kraken import repo try: os.makedirs(click.get_app_dir(APP_NAME)) except OSError: pass with KrakenDownloadProgressBar() as progress: download_task = progress.add_task('Processing', total=0, visible=True if not ctx.meta['verbose'] else False) filename = repo.get_model(model_id, click.get_app_dir(APP_NAME), lambda total, advance: progress.update(download_task, total=total, advance=advance)) message(f'Model name: {filename}') ctx.exit(0) if __name__ == '__main__': cli()
30,865
42.351124
139
py
kraken
kraken-main/kraken/repo.py
# # Copyright 2015 Benjamin Kiessling # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express # or implied. See the License for the specific language governing # permissions and limitations under the License. """ Accessors to the model repository on zenodo. """ import os import json import urllib import logging import requests from os import PathLike from pathlib import Path from contextlib import closing from typing import Callable, Any from kraken.lib.exceptions import KrakenRepoException __all__ = ['get_model', 'get_description', 'get_listing', 'publish_model'] logger = logging.getLogger(__name__) MODEL_REPO = 'https://zenodo.org/api/' SUPPORTED_MODELS = set(['kraken_pytorch']) def publish_model(model_file: [str, PathLike] = None, metadata: dict = None, access_token: str = None, callback: Callable[[int, int], Any] = lambda: None, private: bool = False) -> str: """ Publishes a model to the repository. Args: model_file: Path to read model from. metadata: Metadata dictionary access_token: Zenodo API access token callback: Function called with octet-wise progress. private: Whether to generate a community inclusion request that makes the model recoverable by the public. """ model_file = Path(model_file) fp = open(model_file, 'rb') _metadata = json.dumps(metadata) total = model_file.stat().st_size + len(_metadata) + 3 headers = {"Content-Type": "application/json"} r = requests.post(f'{MODEL_REPO}deposit/depositions', params={'access_token': access_token}, json={}, headers=headers) r.raise_for_status() callback(total, 1) deposition_id = r.json()['id'] data = {'filename': 'metadata.json'} files = {'file': ('metadata.json', _metadata)} r = requests.post(f'{MODEL_REPO}deposit/depositions/{deposition_id}/files', params={'access_token': access_token}, data=data, files=files) r.raise_for_status() callback(total, len(_metadata)) data = {'filename': metadata['name']} files = {'file': fp} r = requests.post(f'{MODEL_REPO}deposit/depositions/{deposition_id}/files', params={'access_token': access_token}, data=data, files=files) r.raise_for_status() callback(total, model_file.stat().st_size) # fill zenodo metadata data = {'metadata': { 'title': metadata['summary'], 'upload_type': 'publication', 'publication_type': 'other', 'description': metadata['description'], 'creators': metadata['authors'], 'access_right': 'open', 'keywords': ['kraken_pytorch'], 'license': metadata['license'] } } if not private: data['metadata']['communities'] = [{'identifier': 'ocr_models'}] # add link to training data to metadata if 'source' in metadata: data['metadata']['related_identifiers'] = [{'relation': 'isSupplementTo', 'identifier': metadata['source']}] r = requests.put(f'{MODEL_REPO}deposit/depositions/{deposition_id}', params={'access_token': access_token}, data=json.dumps(data), headers=headers) r.raise_for_status() callback(total, 1) r = requests.post(f'{MODEL_REPO}deposit/depositions/{deposition_id}/actions/publish', params={'access_token': access_token}) r.raise_for_status() callback(total, 1) return r.json()['doi'] def get_model(model_id: str, path: str, callback: Callable[[int, int], Any] = lambda total, advance: None) -> str: """ Retrieves a model and saves it to a path. Args: model_id (str): DOI of the model path (str): Destination to write model to. callback (func): Function called for every 1024 octet chunk received. Returns: The identifier the model can be called through on the command line. Will usually be the file name of the model. """ logger.info(f'Saving model {model_id} to {path}') r = requests.get(f'{MODEL_REPO}records', params={'q': f'doi:"{model_id}"'}) r.raise_for_status() callback(0, 0) resp = r.json() if resp['hits']['total'] != 1: logger.error(f'Found {resp["hits"]["total"]} models when querying for id \'{model_id}\'') raise KrakenRepoException(f'Found {resp["hits"]["total"]} models when querying for id \'{model_id}\'') metadata = resp['hits']['hits'][0] model_url = [x['links']['self'] for x in metadata['files'] if x['type'] == 'mlmodel'][0] # callable model identifier nat_id = os.path.basename(urllib.parse.urlparse(model_url).path) spath = os.path.join(path, nat_id) logger.debug(f'downloading model file {model_url} to {spath}') with closing(requests.get(model_url, stream=True)) as r: file_size = int(r.headers['Content-length']) with open(spath, 'wb') as f: for chunk in r.iter_content(chunk_size=1024): callback(file_size, len(chunk)) f.write(chunk) return nat_id def get_description(model_id: str, callback: Callable[..., Any] = lambda: None) -> dict: """ Fetches the metadata for a single model from the zenodo repository. Args: model_id (str): DOI of the model. callback (callable): Optional function called once per HTTP request. Returns: Dict """ logger.info(f'Retrieving metadata for {model_id}') r = requests.get(f'{MODEL_REPO}records', params={'q': f'doi:"{model_id}"'}) r.raise_for_status() callback() resp = r.json() if resp['hits']['total'] != 1: logger.error(f'Found {resp["hits"]["total"]} models when querying for id \'{model_id}\'') raise KrakenRepoException(f'Found {resp["hits"]["total"]} models when querying for id \'{model_id}\'') record = resp['hits']['hits'][0] metadata = record['metadata'] if 'keywords' not in metadata: logger.error('No keywords included on deposit') raise KrakenRepoException('No keywords included on deposit.') model_type = SUPPORTED_MODELS.intersection(metadata['keywords']) if not model_type: msg = 'Unsupported model type(s): {}'.format(', '.join(metadata['keywords'])) logger.error(msg) raise KrakenRepoException(msg) meta_json = None for file in record['files']: if file['key'] == 'metadata.json': callback() r = requests.get(file['links']['self']) r.raise_for_status() callback() try: meta_json = r.json() except Exception: msg = f'Metadata for \'{record["metadata"]["title"]}\' ({record["metadata"]["doi"]}) not in JSON format' logger.error(msg) raise KrakenRepoException(msg) if not meta_json: msg = 'Mo metadata.jsn found for \'{}\' ({})'.format(record['metadata']['title'], record['metadata']['doi']) logger.error(msg) raise KrakenRepoException(msg) # merge metadata.json into DataCite metadata.update({'graphemes': meta_json['graphemes'], 'summary': meta_json['summary'], 'script': meta_json['script'], 'link': record['links']['latest'], 'type': [x.split('_')[1] for x in model_type], 'accuracy': meta_json['accuracy']}) return metadata def get_listing(callback: Callable[[int, int], Any] = lambda total, advance: None) -> dict: """ Fetches a listing of all kraken models from the zenodo repository. Args: callback (Callable): Function called after each HTTP request. Returns: Dict of models with each model. """ logger.info('Retrieving model list') records = [] r = requests.get('{}{}'.format(MODEL_REPO, 'records'), params={'communities': 'ocr_models'}) r.raise_for_status() callback(1, 1) resp = r.json() if not resp['hits']['total']: logger.error('No models found in community \'ocr_models\'') raise KrakenRepoException('No models found in repository \'ocr_models\'') logger.debug('Total of {} records in repository'.format(resp['hits']['total'])) total = resp['hits']['total'] callback(total, 0) records.extend(resp['hits']['hits']) while 'next' in resp['links']: logger.debug('Fetching next page') r = requests.get(resp['links']['next']) r.raise_for_status() resp = r.json() logger.debug('Found {} new records'.format(len(resp['hits']['hits']))) records.extend(resp['hits']['hits']) logger.debug('Retrieving model metadata') models = {} # fetch metadata.jsn for each model for record in records: if 'keywords' not in record['metadata']: continue model_type = SUPPORTED_MODELS.intersection(record['metadata']['keywords']) if not model_type: continue for file in record['files']: if file['key'] == 'metadata.json': callback(total, 1) r = requests.get(file['links']['self']) r.raise_for_status() try: metadata = r.json() except Exception: msg = f'Metadata for \'{record["metadata"]["title"]}\' ({record["metadata"]["doi"]}) not in JSON format' logger.error(msg) raise KrakenRepoException(msg) # merge metadata.jsn into DataCite key = record['metadata']['doi'] models[key] = record['metadata'] models[key].update({'graphemes': metadata['graphemes'], 'summary': metadata['summary'], 'script': metadata['script'], 'link': record['links']['latest'], 'type': [x.split('_')[1] for x in model_type]}) return models
10,633
39.280303
124
py
kraken
kraken-main/kraken/ketos/segmentation.py
# # Copyright 2022 Benjamin Kiessling # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express # or implied. See the License for the specific language governing # permissions and limitations under the License. """ kraken.ketos.segmentation ~~~~~~~~~~~~~~~~~~~~~~~~~ Command line driver for segmentation training and evaluation. """ import click import pathlib import logging from PIL import Image from kraken.lib.progress import KrakenProgressBar from kraken.lib.exceptions import KrakenInputException from kraken.lib.default_specs import SEGMENTATION_HYPER_PARAMS, SEGMENTATION_SPEC from kraken.ketos.util import _validate_manifests, _expand_gt, message, to_ptl_device logging.captureWarnings(True) logger = logging.getLogger('kraken') # raise default max image size to 20k * 20k pixels Image.MAX_IMAGE_PIXELS = 20000 ** 2 def _validate_merging(ctx, param, value): """ Maps baseline/region merging to a dict of merge structures. """ if not value: return None merge_dict = {} # type: Dict[str, str] try: for m in value: k, v = m.split(':') merge_dict[v] = k # type: ignore except Exception: raise click.BadParameter('Mappings must be in format target:src') return merge_dict @click.command('segtrain') @click.pass_context @click.option('-o', '--output', show_default=True, type=click.Path(), default='model', help='Output model file') @click.option('-s', '--spec', show_default=True, default=SEGMENTATION_SPEC, help='VGSL spec of the baseline labeling network') @click.option('--line-width', show_default=True, default=SEGMENTATION_HYPER_PARAMS['line_width'], help='The height of each baseline in the target after scaling') @click.option('--pad', show_default=True, type=(int, int), default=(0, 0), help='Padding (left/right, top/bottom) around the page image') @click.option('-i', '--load', show_default=True, type=click.Path(exists=True, readable=True), help='Load existing file to continue training') @click.option('-F', '--freq', show_default=True, default=SEGMENTATION_HYPER_PARAMS['freq'], type=click.FLOAT, help='Model saving and report generation frequency in epochs ' 'during training. If frequency is >1 it must be an integer, ' 'i.e. running validation every n-th epoch.') @click.option('-q', '--quit', show_default=True, default=SEGMENTATION_HYPER_PARAMS['quit'], type=click.Choice(['early', 'dumb']), help='Stop condition for training. Set to `early` for early stopping or `dumb` for fixed number of epochs') @click.option('-N', '--epochs', show_default=True, default=SEGMENTATION_HYPER_PARAMS['epochs'], help='Number of epochs to train for') @click.option('--min-epochs', show_default=True, default=SEGMENTATION_HYPER_PARAMS['min_epochs'], help='Minimal number of epochs to train for when using early stopping.') @click.option('--lag', show_default=True, default=SEGMENTATION_HYPER_PARAMS['lag'], help='Number of evaluations (--report frequence) to wait before stopping training without improvement') @click.option('--min-delta', show_default=True, default=SEGMENTATION_HYPER_PARAMS['min_delta'], type=click.FLOAT, help='Minimum improvement between epochs to reset early stopping. By default it scales the delta by the best loss') @click.option('-d', '--device', show_default=True, default='cpu', help='Select device to use (cpu, cuda:0, cuda:1, ...)') @click.option('--precision', show_default=True, default='32', type=click.Choice(['64', '32', 'bf16', '16']), help='Numerical precision to use for training. Default is 32-bit single-point precision.') @click.option('--optimizer', show_default=True, default=SEGMENTATION_HYPER_PARAMS['optimizer'], type=click.Choice(['Adam', 'SGD', 'RMSprop', 'Lamb']), help='Select optimizer') @click.option('-r', '--lrate', show_default=True, default=SEGMENTATION_HYPER_PARAMS['lrate'], help='Learning rate') @click.option('-m', '--momentum', show_default=True, default=SEGMENTATION_HYPER_PARAMS['momentum'], help='Momentum') @click.option('-w', '--weight-decay', show_default=True, default=SEGMENTATION_HYPER_PARAMS['weight_decay'], help='Weight decay') @click.option('--warmup', show_default=True, type=float, default=SEGMENTATION_HYPER_PARAMS['warmup'], help='Number of steps to ramp up to `lrate` initial learning rate.') @click.option('--schedule', show_default=True, type=click.Choice(['constant', '1cycle', 'exponential', 'cosine', 'step', 'reduceonplateau']), default=SEGMENTATION_HYPER_PARAMS['schedule'], help='Set learning rate scheduler. For 1cycle, cycle length is determined by the `--step-size` option.') @click.option('-g', '--gamma', show_default=True, default=SEGMENTATION_HYPER_PARAMS['gamma'], help='Decay factor for exponential, step, and reduceonplateau learning rate schedules') @click.option('-ss', '--step-size', show_default=True, default=SEGMENTATION_HYPER_PARAMS['step_size'], help='Number of validation runs between learning rate decay for exponential and step LR schedules') @click.option('--sched-patience', show_default=True, default=SEGMENTATION_HYPER_PARAMS['rop_patience'], help='Minimal number of validation runs between LR reduction for reduceonplateau LR schedule.') @click.option('--cos-max', show_default=True, default=SEGMENTATION_HYPER_PARAMS['cos_t_max'], help='Epoch of minimal learning rate for cosine LR scheduler.') @click.option('-p', '--partition', show_default=True, default=0.9, help='Ground truth data partition ratio between train/validation set') @click.option('-t', '--training-files', show_default=True, default=None, multiple=True, callback=_validate_manifests, type=click.File(mode='r', lazy=True), help='File(s) with additional paths to training data') @click.option('-e', '--evaluation-files', show_default=True, default=None, multiple=True, callback=_validate_manifests, type=click.File(mode='r', lazy=True), help='File(s) with paths to evaluation data. Overrides the `-p` parameter') @click.option('--workers', show_default=True, default=1, help='Number of OpenMP threads and workers when running on CPU.') @click.option('--load-hyper-parameters/--no-load-hyper-parameters', show_default=True, default=False, help='When loading an existing model, retrieve hyper-parameters from the model') @click.option('--force-binarization/--no-binarization', show_default=True, default=False, help='Forces input images to be binary, otherwise ' 'the appropriate color format will be auto-determined through the ' 'network specification. Will be ignored in `path` mode.') @click.option('-f', '--format-type', type=click.Choice(['path', 'xml', 'alto', 'page']), default='xml', help='Sets the training data format. In ALTO and PageXML mode all ' 'data is extracted from xml files containing both baselines and a ' 'link to source images. In `path` mode arguments are image files ' 'sharing a prefix up to the last extension with JSON `.path` files ' 'containing the baseline information.') @click.option('--suppress-regions/--no-suppress-regions', show_default=True, default=False, help='Disables region segmentation training.') @click.option('--suppress-baselines/--no-suppress-baselines', show_default=True, default=False, help='Disables baseline segmentation training.') @click.option('-vr', '--valid-regions', show_default=True, default=None, multiple=True, help='Valid region types in training data. May be used multiple times.') @click.option('-vb', '--valid-baselines', show_default=True, default=None, multiple=True, help='Valid baseline types in training data. May be used multiple times.') @click.option('-mr', '--merge-regions', show_default=True, default=None, help='Region merge mapping. One or more mappings of the form `$target:$src` where $src is merged into $target.', multiple=True, callback=_validate_merging) @click.option('-mb', '--merge-baselines', show_default=True, default=None, help='Baseline type merge mapping. Same syntax as `--merge-regions`', multiple=True, callback=_validate_merging) @click.option('-br', '--bounding-regions', show_default=True, default=None, multiple=True, help='Regions treated as boundaries for polygonization purposes. May be used multiple times.') @click.option('--augment/--no-augment', show_default=True, default=SEGMENTATION_HYPER_PARAMS['augment'], help='Enable image augmentation') @click.option('--resize', show_default=True, default='fail', type=click.Choice([ 'add', 'union', # Deprecation: `add` is deprecated, `union` is the new value 'both', 'new', # Deprecation: `both` is deprecated, `new` is the new value 'fail' ]), help='Output layer resizing option. If set to `add` new classes will be ' 'added, `both` will set the layer to match exactly ' 'the training data classes, `fail` will abort if training data and model ' 'classes do not match.') @click.option('-tl', '--topline', 'topline', show_default=True, flag_value='topline', help='Switch for the baseline location in the scripts. ' 'Set to topline if the data is annotated with a hanging baseline, as is ' 'common with Hebrew, Bengali, Devanagari, etc. Set to ' ' centerline for scripts annotated with a central line.') @click.option('-cl', '--centerline', 'topline', flag_value='centerline') @click.option('-bl', '--baseline', 'topline', flag_value='baseline', default='baseline') @click.option('--logger', 'pl_logger', show_default=True, type=click.Choice(['tensorboard']), default=None, help='Logger used by PyTorch Lightning to track metrics such as loss and accuracy.') @click.option('--log-dir', show_default=True, type=click.Path(exists=True, dir_okay=True, writable=True), help='Path to directory where the logger will store the logs. If not set, a directory will be created in the current working directory.') @click.argument('ground_truth', nargs=-1, callback=_expand_gt, type=click.Path(exists=False, dir_okay=False)) def segtrain(ctx, output, spec, line_width, pad, load, freq, quit, epochs, min_epochs, lag, min_delta, device, precision, optimizer, lrate, momentum, weight_decay, warmup, schedule, gamma, step_size, sched_patience, cos_max, partition, training_files, evaluation_files, workers, load_hyper_parameters, force_binarization, format_type, suppress_regions, suppress_baselines, valid_regions, valid_baselines, merge_regions, merge_baselines, bounding_regions, augment, resize, topline, pl_logger, log_dir, ground_truth): """ Trains a baseline labeling model for layout analysis """ import shutil from kraken.lib.train import SegmentationModel, KrakenTrainer if resize != 'fail' and not load: raise click.BadOptionUsage('resize', 'resize option requires loading an existing model') if not (0 <= freq <= 1) and freq % 1.0 != 0: raise click.BadOptionUsage('freq', 'freq needs to be either in the interval [0,1.0] or a positive integer.') if augment: try: import albumentations # NOQA except ImportError: raise click.BadOptionUsage('augment', 'augmentation needs the `albumentations` package installed.') if pl_logger == 'tensorboard': try: import tensorboard # NOQA except ImportError: raise click.BadOptionUsage('logger', 'tensorboard logger needs the `tensorboard` package installed.') if log_dir is None: log_dir = pathlib.Path.cwd() logger.info('Building ground truth set from {} document images'.format(len(ground_truth) + len(training_files))) # populate hyperparameters from command line args hyper_params = SEGMENTATION_HYPER_PARAMS.copy() hyper_params.update({'line_width': line_width, 'padding': pad, 'freq': freq, 'quit': quit, 'epochs': epochs, 'min_epochs': min_epochs, 'lag': lag, 'min_delta': min_delta, 'optimizer': optimizer, 'lrate': lrate, 'momentum': momentum, 'weight_decay': weight_decay, 'warmup': warmup, 'schedule': schedule, 'augment': augment, 'gamma': gamma, 'step_size': step_size, 'rop_patience': sched_patience, 'cos_t_max': cos_max, }) # disable automatic partition when given evaluation set explicitly if evaluation_files: partition = 1 ground_truth = list(ground_truth) # merge training_files into ground_truth list if training_files: ground_truth.extend(training_files) if len(ground_truth) == 0: raise click.UsageError('No training data was provided to the train command. Use `-t` or the `ground_truth` argument.') loc = {'topline': True, 'baseline': False, 'centerline': None} topline = loc[topline] try: accelerator, device = to_ptl_device(device) except Exception as e: raise click.BadOptionUsage('device', str(e)) if hyper_params['freq'] > 1: val_check_interval = {'check_val_every_n_epoch': int(hyper_params['freq'])} else: val_check_interval = {'val_check_interval': hyper_params['freq']} model = SegmentationModel(hyper_params, output=output, spec=spec, model=load, training_data=ground_truth, evaluation_data=evaluation_files, partition=partition, num_workers=workers, load_hyper_parameters=load_hyper_parameters, force_binarization=force_binarization, format_type=format_type, suppress_regions=suppress_regions, suppress_baselines=suppress_baselines, valid_regions=valid_regions, valid_baselines=valid_baselines, merge_regions=merge_regions, merge_baselines=merge_baselines, bounding_regions=bounding_regions, resize=resize, topline=topline) message('Training line types:') for k, v in model.train_set.dataset.class_mapping['baselines'].items(): message(f' {k}\t{v}\t{model.train_set.dataset.class_stats["baselines"][k]}') message('Training region types:') for k, v in model.train_set.dataset.class_mapping['regions'].items(): message(f' {k}\t{v}\t{model.train_set.dataset.class_stats["regions"][k]}') if len(model.train_set) == 0: raise click.UsageError('No valid training data was provided to the train command. Use `-t` or the `ground_truth` argument.') trainer = KrakenTrainer(accelerator=accelerator, devices=device, precision=precision, max_epochs=hyper_params['epochs'] if hyper_params['quit'] == 'dumb' else -1, min_epochs=hyper_params['min_epochs'], enable_progress_bar=True if not ctx.meta['verbose'] else False, deterministic=ctx.meta['deterministic'], pl_logger=pl_logger, log_dir=log_dir, **val_check_interval) trainer.fit(model) if model.best_epoch == -1: logger.warning('Model did not improve during training.') ctx.exit(1) if quit == 'early': message(f'Moving best model {model.best_model} ({model.best_metric}) to {output}_best.mlmodel') logger.info(f'Moving best model {model.best_model} ({model.best_metric}) to {output}_best.mlmodel') shutil.copy(f'{model.best_model}', f'{output}_best.mlmodel') @click.command('segtest') @click.pass_context @click.option('-m', '--model', show_default=True, type=click.Path(exists=True, readable=True), multiple=False, help='Model(s) to evaluate') @click.option('-e', '--evaluation-files', show_default=True, default=None, multiple=True, callback=_validate_manifests, type=click.File(mode='r', lazy=True), help='File(s) with paths to evaluation data.') @click.option('-d', '--device', show_default=True, default='cpu', help='Select device to use (cpu, cuda:0, cuda:1, ...)') @click.option('--workers', show_default=True, default=1, help='Number of OpenMP threads when running on CPU.') @click.option('--force-binarization/--no-binarization', show_default=True, default=False, help='Forces input images to be binary, otherwise ' 'the appropriate color format will be auto-determined through the ' 'network specification. Will be ignored in `path` mode.') @click.option('-f', '--format-type', type=click.Choice(['path', 'xml', 'alto', 'page']), default='xml', help='Sets the training data format. In ALTO and PageXML mode all ' 'data is extracted from xml files containing both baselines and a ' 'link to source images. In `path` mode arguments are image files ' 'sharing a prefix up to the last extension with JSON `.path` files ' 'containing the baseline information.') @click.option('--suppress-regions/--no-suppress-regions', show_default=True, default=False, help='Disables region segmentation training.') @click.option('--suppress-baselines/--no-suppress-baselines', show_default=True, default=False, help='Disables baseline segmentation training.') @click.option('-vr', '--valid-regions', show_default=True, default=None, multiple=True, help='Valid region types in training data. May be used multiple times.') @click.option('-vb', '--valid-baselines', show_default=True, default=None, multiple=True, help='Valid baseline types in training data. May be used multiple times.') @click.option('-mr', '--merge-regions', show_default=True, default=None, help='Region merge mapping. One or more mappings of the form `$target:$src` where $src is merged into $target.', multiple=True, callback=_validate_merging) @click.option('-mb', '--merge-baselines', show_default=True, default=None, help='Baseline type merge mapping. Same syntax as `--merge-regions`', multiple=True, callback=_validate_merging) @click.option('-br', '--bounding-regions', show_default=True, default=None, multiple=True, help='Regions treated as boundaries for polygonization purposes. May be used multiple times.') @click.option("--threshold", type=click.FloatRange(.01, .99), default=.3, show_default=True, help="Threshold for heatmap binarization. Training threshold is .3, prediction is .5") @click.argument('test_set', nargs=-1, callback=_expand_gt, type=click.Path(exists=False, dir_okay=False)) def segtest(ctx, model, evaluation_files, device, workers, threshold, force_binarization, format_type, test_set, suppress_regions, suppress_baselines, valid_regions, valid_baselines, merge_regions, merge_baselines, bounding_regions): """ Evaluate on a test set. """ if not model: raise click.UsageError('No model to evaluate given.') from torch.utils.data import DataLoader import torch import torch.nn.functional as F from kraken.lib.train import BaselineSet, ImageInputTransforms from kraken.lib.vgsl import TorchVGSLModel logger.info('Building test set from {} documents'.format(len(test_set) + len(evaluation_files))) message('Loading model {}\t'.format(model), nl=False) nn = TorchVGSLModel.load_model(model) message('\u2713', fg='green') test_set = list(test_set) if evaluation_files: test_set.extend(evaluation_files) if len(test_set) == 0: raise click.UsageError('No evaluation data was provided to the test command. Use `-e` or the `test_set` argument.') _batch, _channels, _height, _width = nn.input transforms = ImageInputTransforms( _batch, _height, _width, _channels, 0, valid_norm=False, force_binarization=force_binarization ) if 'file_system' in torch.multiprocessing.get_all_sharing_strategies(): logger.debug('Setting multiprocessing tensor sharing strategy to file_system') torch.multiprocessing.set_sharing_strategy('file_system') if not valid_regions: valid_regions = None if not valid_baselines: valid_baselines = None if suppress_regions: valid_regions = [] merge_regions = None if suppress_baselines: valid_baselines = [] merge_baselines = None test_set = BaselineSet(test_set, line_width=nn.user_metadata["hyper_params"]["line_width"], im_transforms=transforms, mode=format_type, augmentation=False, valid_baselines=valid_baselines, merge_baselines=merge_baselines, valid_regions=valid_regions, merge_regions=merge_regions) test_set.class_mapping = nn.user_metadata["class_mapping"] test_set.num_classes = sum([len(classDict) for classDict in test_set.class_mapping.values()]) baselines_diff = set(test_set.class_stats["baselines"].keys()).difference(test_set.class_mapping["baselines"].keys()) regions_diff = set(test_set.class_stats["regions"].keys()).difference(test_set.class_mapping["regions"].keys()) if baselines_diff: message(f'Model baseline types missing in test set: {", ".join(sorted(list(baselines_diff)))}') if regions_diff: message(f'Model region types missing in the test set: {", ".join(sorted(list(regions_diff)))}') try: accelerator, device = to_ptl_device(device) if device: device = f'{accelerator}:{device}' else: device = accelerator except Exception as e: raise click.BadOptionUsage('device', str(e)) if len(test_set) == 0: raise click.UsageError('No evaluation data was provided to the test command. Use `-e` or the `test_set` argument.') ds_loader = DataLoader(test_set, batch_size=1, num_workers=workers, pin_memory=True) nn.to(device) nn.eval() nn.set_num_threads(1) pages = [] lines_idx = list(test_set.class_mapping["baselines"].values()) regions_idx = list(test_set.class_mapping["regions"].values()) with KrakenProgressBar() as progress: batches = len(ds_loader) pred_task = progress.add_task('Evaluating', total=batches, visible=True if not ctx.meta['verbose'] else False) for batch in ds_loader: x, y = batch['image'], batch['target'] try: pred, _ = nn.nn(x) # scale target to output size y = F.interpolate(y, size=(pred.size(2), pred.size(3))).squeeze(0).bool() pred = pred.squeeze() > threshold pred = pred.view(pred.size(0), -1) y = y.view(y.size(0), -1) pages.append({ 'intersections': (y & pred).sum(dim=1, dtype=torch.double), 'unions': (y | pred).sum(dim=1, dtype=torch.double), 'corrects': torch.eq(y, pred).sum(dim=1, dtype=torch.double), 'cls_cnt': y.sum(dim=1, dtype=torch.double), 'all_n': torch.tensor(y.size(1), dtype=torch.double, device=device) }) if lines_idx: y_baselines = y[lines_idx].sum(dim=0, dtype=torch.bool) pred_baselines = pred[lines_idx].sum(dim=0, dtype=torch.bool) pages[-1]["baselines"] = { 'intersections': (y_baselines & pred_baselines).sum(dim=0, dtype=torch.double), 'unions': (y_baselines | pred_baselines).sum(dim=0, dtype=torch.double), } if regions_idx: y_regions_idx = y[regions_idx].sum(dim=0, dtype=torch.bool) pred_regions_idx = pred[regions_idx].sum(dim=0, dtype=torch.bool) pages[-1]["regions"] = { 'intersections': (y_regions_idx & pred_regions_idx).sum(dim=0, dtype=torch.double), 'unions': (y_regions_idx | pred_regions_idx).sum(dim=0, dtype=torch.double), } except FileNotFoundError as e: batches -= 1 progress.update(pred_task, total=batches) logger.warning('{} {}. Skipping.'.format(e.strerror, e.filename)) except KrakenInputException as e: batches -= 1 progress.update(pred_task, total=batches) logger.warning(str(e)) progress.update(pred_task, advance=1) # Accuracy / pixel corrects = torch.stack([x['corrects'] for x in pages], -1).sum(dim=-1) all_n = torch.stack([x['all_n'] for x in pages]).sum() # Number of pixel for all pages class_pixel_accuracy = corrects / all_n mean_accuracy = torch.mean(class_pixel_accuracy) intersections = torch.stack([x['intersections'] for x in pages], -1).sum(dim=-1) unions = torch.stack([x['unions'] for x in pages], -1).sum(dim=-1) smooth = torch.finfo(torch.float).eps class_iu = (intersections + smooth) / (unions + smooth) mean_iu = torch.mean(class_iu) cls_cnt = torch.stack([x['cls_cnt'] for x in pages]).sum() freq_iu = torch.sum(cls_cnt / cls_cnt.sum() * class_iu.sum()) message(f"Mean Accuracy: {mean_accuracy.item():.3f}") message(f"Mean IOU: {mean_iu.item():.3f}") message(f"Frequency-weighted IOU: {freq_iu.item():.3f}") # Region accuracies if lines_idx: line_intersections = torch.stack([x["baselines"]['intersections'] for x in pages]).sum() line_unions = torch.stack([x["baselines"]['unions'] for x in pages]).sum() smooth = torch.finfo(torch.float).eps line_iu = (line_intersections + smooth) / (line_unions + smooth) message(f"Class-independent Baseline IOU: {line_iu.item():.3f}") # Region accuracies if regions_idx: region_intersections = torch.stack([x["regions"]['intersections'] for x in pages]).sum() region_unions = torch.stack([x["regions"]['unions'] for x in pages]).sum() smooth = torch.finfo(torch.float).eps region_iu = (region_intersections + smooth) / (region_unions + smooth) message(f"Class-independent Region IOU: {region_iu.item():.3f}") from rich.console import Console from rich.table import Table table = Table('Category', 'Class Name', 'Pixel Accuracy', 'IOU', 'Object Count') class_iu = class_iu.tolist() class_pixel_accuracy = class_pixel_accuracy.tolist() for (cat, class_name), iu, pix_acc in zip( [(cat, key) for (cat, subcategory) in test_set.class_mapping.items() for key in subcategory], class_iu, class_pixel_accuracy ): table.add_row(cat, class_name, f'{pix_acc:.3f}', f'{iu:.3f}', f'{test_set.class_stats[cat][class_name]}' if cat != "aux" else 'N/A') console = Console() console.print(table)
29,987
48.98
151
py
kraken
kraken-main/kraken/ketos/__init__.py
# # Copyright 2015 Benjamin Kiessling # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express # or implied. See the License for the specific language governing # permissions and limitations under the License. """ kraken.ketos ~~~~~~~~~~~~~ Command line drivers for training functionality. """ import click import logging from PIL import Image from rich.traceback import install from kraken.lib import log from .dataset import compile from .linegen import line_generator from .pretrain import pretrain from .recognition import train, test from .repo import publish from .segmentation import segtrain, segtest from .transcription import extract, transcription APP_NAME = 'kraken' logging.captureWarnings(True) logger = logging.getLogger('kraken') # install rich traceback handler install(suppress=[click]) # raise default max image size to 20k * 20k pixels Image.MAX_IMAGE_PIXELS = 20000 ** 2 @click.group() @click.version_option() @click.pass_context @click.option('-v', '--verbose', default=0, count=True) @click.option('-s', '--seed', default=None, type=click.INT, help='Seed for numpy\'s and torch\'s RNG. Set to a fixed value to ' 'ensure reproducible random splits of data') @click.option('-r', '--deterministic/--no-deterministic', default=False, help="Enables deterministic training. If no seed is given and enabled the seed will be set to 42.") def cli(ctx, verbose, seed, deterministic): ctx.meta['deterministic'] = False if not deterministic else 'warn' if seed: from pytorch_lightning import seed_everything seed_everything(seed, workers=True) elif deterministic: from pytorch_lightning import seed_everything seed_everything(42, workers=True) ctx.meta['verbose'] = verbose log.set_logger(logger, level=30 - min(10 * verbose, 20)) cli.add_command(compile) cli.add_command(pretrain) cli.add_command(train) cli.add_command(test) cli.add_command(segtrain) cli.add_command(segtest) cli.add_command(publish) # deprecated commands cli.add_command(line_generator) cli.add_command(extract) cli.add_command(transcription) if __name__ == '__main__': cli()
2,546
28.275862
113
py