python_code
stringlengths
0
456k
from abc import ABC, abstractmethod from typing import Any from chatgpt.experience_maker.base import Experience class ReplayBuffer(ABC): """Replay buffer base class. It stores experience. Args: sample_batch_size (int): Batch size when sampling. limit (int, optional): Limit of number of experience samples. A number <= 0 means unlimited. Defaults to 0. """ def __init__(self, sample_batch_size: int, limit: int = 0) -> None: super().__init__() self.sample_batch_size = sample_batch_size # limit <= 0 means unlimited self.limit = limit @abstractmethod def append(self, experience: Experience) -> None: pass @abstractmethod def clear(self) -> None: pass @abstractmethod def sample(self) -> Experience: pass @abstractmethod def __len__(self) -> int: pass @abstractmethod def __getitem__(self, idx: int) -> Any: pass @abstractmethod def collate_fn(self, batch: Any) -> Experience: pass
from typing import Optional import torch from transformers import BloomConfig, BloomForCausalLM, BloomModel from .actor import Actor class BLOOMActor(Actor): """ BLOOM Actor model. Args: pretrained (str): Pretrained model name or path. config (BloomConfig): Model config. checkpoint (bool): Enable gradient checkpointing. lora_rank (int): LoRA rank. lora_train_bias (str): LoRA bias training mode. """ def __init__(self, pretrained: str = None, config: Optional[BloomConfig] = None, checkpoint: bool = False, lora_rank: int = 0, lora_train_bias: str = 'none') -> None: if pretrained is not None: model = BloomForCausalLM.from_pretrained(pretrained) elif config is not None: model = BloomForCausalLM(config) else: model = BloomForCausalLM(BloomConfig()) if checkpoint: model.gradient_checkpointing_enable() super().__init__(model, lora_rank, lora_train_bias)
from typing import Optional import torch import torch.nn as nn from transformers import BloomConfig, BloomForCausalLM, BloomModel from .reward_model import RewardModel class BLOOMRM(RewardModel): """ BLOOM Reward model. Args: pretrained (str): Pretrained model name or path. config (BloomConfig): Model config. checkpoint (bool): Enable gradient checkpointing. lora_rank (int): LoRA rank. lora_train_bias (str): LoRA bias training mode. """ def __init__(self, pretrained: str = None, config: Optional[BloomConfig] = None, checkpoint: bool = False, lora_rank: int = 0, lora_train_bias: str = 'none') -> None: if pretrained is not None: model = BloomModel.from_pretrained(pretrained) elif config is not None: model = BloomModel(config) else: model = BloomModel(BloomConfig()) if checkpoint: model.gradient_checkpointing_enable() value_head = nn.Linear(model.config.hidden_size, 1) super().__init__(model, value_head, lora_rank, lora_train_bias)
from typing import Optional import torch.nn as nn from transformers.models.opt.configuration_opt import OPTConfig from transformers.models.opt.modeling_opt import OPTModel from .reward_model import RewardModel class OPTRM(RewardModel): """ OPT Reward model. Args: pretrained (str): Pretrained model name or path. config (OPTConfig): Model config. lora_rank (int): Rank of the low-rank approximation. lora_train_bias (str): LoRA bias training mode. """ def __init__(self, pretrained: Optional[str] = None, config: Optional[OPTConfig] = None, lora_rank: int = 0, lora_train_bias: str = 'none') -> None: if pretrained is not None: model = OPTModel.from_pretrained(pretrained) elif config is not None: model = OPTModel(config) else: model = OPTModel(OPTConfig()) value_head = nn.Linear(model.config.hidden_size, 1) super().__init__(model, value_head, lora_rank, lora_train_bias)
from typing import Any, Callable, Optional import torch import torch.nn as nn try: from transformers.generation_logits_process import ( LogitsProcessorList, TemperatureLogitsWarper, TopKLogitsWarper, TopPLogitsWarper, ) except ImportError: from transformers.generation import LogitsProcessorList, TemperatureLogitsWarper, TopKLogitsWarper, TopPLogitsWarper def prepare_logits_processor(top_k: Optional[int] = None, top_p: Optional[float] = None, temperature: Optional[float] = None) -> LogitsProcessorList: processor_list = LogitsProcessorList() if temperature is not None and temperature != 1.0: processor_list.append(TemperatureLogitsWarper(temperature)) if top_k is not None and top_k != 0: processor_list.append(TopKLogitsWarper(top_k)) if top_p is not None and top_p < 1.0: processor_list.append(TopPLogitsWarper(top_p)) return processor_list def sample(model: nn.Module, input_ids: torch.Tensor, max_length: int, early_stopping: bool = False, eos_token_id: Optional[int] = None, pad_token_id: Optional[int] = None, top_k: Optional[int] = None, top_p: Optional[float] = None, temperature: Optional[float] = None, prepare_inputs_fn: Optional[Callable[[torch.Tensor, Any], dict]] = None, update_model_kwargs_fn: Optional[Callable[[dict, Any], dict]] = None, **model_kwargs) -> torch.Tensor: if input_ids.size(1) >= max_length: return input_ids logits_processor = prepare_logits_processor(top_k, top_p, temperature) unfinished_sequences = input_ids.new(input_ids.shape[0]).fill_(1) for _ in range(input_ids.size(1), max_length): model_inputs = prepare_inputs_fn(input_ids, **model_kwargs) if prepare_inputs_fn is not None else { 'input_ids': input_ids } outputs = model(**model_inputs) next_token_logits = outputs['logits'][:, -1, :] # pre-process distribution next_token_logits = logits_processor(input_ids, next_token_logits) # sample probs = torch.softmax(next_token_logits, dim=-1, dtype=torch.float) next_tokens = torch.multinomial(probs, num_samples=1).squeeze(1) # finished sentences should have their next token be a padding token if eos_token_id is not None: if pad_token_id is None: raise ValueError("If `eos_token_id` is defined, make sure that `pad_token_id` is defined.") next_tokens = next_tokens * unfinished_sequences + pad_token_id * (1 - unfinished_sequences) # update generated ids, model inputs for next step input_ids = torch.cat([input_ids, next_tokens[:, None]], dim=-1) if update_model_kwargs_fn is not None: model_kwargs = update_model_kwargs_fn(outputs, **model_kwargs) # if eos_token was found in one sentence, set sentence to finished if eos_token_id is not None: unfinished_sequences = unfinished_sequences.mul((next_tokens != eos_token_id).long()) # stop when each sentence is finished if early_stopping=True if early_stopping and unfinished_sequences.max() == 0: break return input_ids def generate(model: nn.Module, input_ids: torch.Tensor, max_length: int, num_beams: int = 1, do_sample: bool = True, early_stopping: bool = False, eos_token_id: Optional[int] = None, pad_token_id: Optional[int] = None, top_k: Optional[int] = None, top_p: Optional[float] = None, temperature: Optional[float] = None, prepare_inputs_fn: Optional[Callable[[torch.Tensor, Any], dict]] = None, update_model_kwargs_fn: Optional[Callable[[dict, Any], dict]] = None, **model_kwargs) -> torch.Tensor: """Generate token sequence. The returned sequence is input_ids + generated_tokens. Args: model (nn.Module): model input_ids (torch.Tensor): input sequence max_length (int): max length of the returned sequence num_beams (int, optional): number of beams. Defaults to 1. do_sample (bool, optional): whether to do sample. Defaults to True. early_stopping (bool, optional): if True, the sequence length may be smaller than max_length due to finding eos. Defaults to False. eos_token_id (Optional[int], optional): end of sequence token id. Defaults to None. pad_token_id (Optional[int], optional): pad token id. Defaults to None. top_k (Optional[int], optional): the number of highest probability vocabulary tokens to keep for top-k-filtering. Defaults to None. top_p (Optional[float], optional): If set to float < 1, only the smallest set of most probable tokens with probabilities that add up to top_p or higher are kept for generation. Defaults to None. temperature (Optional[float], optional): The value used to module the next token probabilities. Defaults to None. prepare_inputs_fn (Optional[Callable[[torch.Tensor, Any], dict]], optional): Function to preprocess model inputs. Arguments of this function should be input_ids and model_kwargs. Defaults to None. update_model_kwargs_fn (Optional[Callable[[dict, Any], dict]], optional): Function to update model_kwargs based on outputs. Arguments of this function should be outputs and model_kwargs. Defaults to None. """ is_greedy_gen_mode = ((num_beams == 1) and do_sample is False) is_sample_gen_mode = ((num_beams == 1) and do_sample is True) is_beam_gen_mode = ((num_beams > 1) and do_sample is False) if is_greedy_gen_mode: # run greedy search raise NotImplementedError elif is_sample_gen_mode: # run sample return sample(model, input_ids, max_length, early_stopping=early_stopping, eos_token_id=eos_token_id, pad_token_id=pad_token_id, top_k=top_k, top_p=top_p, temperature=temperature, prepare_inputs_fn=prepare_inputs_fn, update_model_kwargs_fn=update_model_kwargs_fn, **model_kwargs) elif is_beam_gen_mode: raise NotImplementedError else: raise ValueError("Unsupported generation mode")
from typing import Optional import torch.nn as nn from transformers.models.gpt2.configuration_gpt2 import GPT2Config from transformers.models.gpt2.modeling_gpt2 import GPT2Model from .reward_model import RewardModel class GPTRM(RewardModel): """ GPT Reward model. Args: pretrained (str): Pretrained model name or path. config (GPT2Config): Model config. checkpoint (bool): Enable gradient checkpointing. """ def __init__(self, pretrained: Optional[str] = None, config: Optional[GPT2Config] = None, checkpoint: bool = False) -> None: if pretrained is not None: model = GPT2Model.from_pretrained(pretrained) elif config is not None: model = GPT2Model(config) else: model = GPT2Model(GPT2Config()) if checkpoint: model.gradient_checkpointing_enable() value_head = nn.Linear(model.config.n_embd, 1) super().__init__(model, value_head)
from typing import Optional import torch.nn as nn from transformers.models.opt.configuration_opt import OPTConfig from transformers.models.opt.modeling_opt import OPTModel from .critic import Critic class OPTCritic(Critic): """ OPT Critic model. Args: pretrained (str): Pretrained model name or path. config (OPTConfig): Model config. checkpoint (bool): Enable gradient checkpointing. lora_rank (int): Rank of the low-rank approximation. lora_train_bias (str): LoRA bias training mode. """ def __init__(self, pretrained: Optional[str] = None, config: Optional[OPTConfig] = None, checkpoint: bool = False, lora_rank: int = 0, lora_train_bias: str = 'none') -> None: if pretrained is not None: model = OPTModel.from_pretrained(pretrained) elif config is not None: model = OPTModel(config) else: model = OPTModel(OPTConfig()) if checkpoint: model.gradient_checkpointing_enable() value_head = nn.Linear(model.config.hidden_size, 1) super().__init__(model, value_head, lora_rank, lora_train_bias)
from typing import Optional import torch import torch.nn as nn from transformers import BloomConfig, BloomForCausalLM, BloomModel from .critic import Critic class BLOOMCritic(Critic): """ BLOOM Critic model. Args: pretrained (str): Pretrained model name or path. config (BloomConfig): Model config. checkpoint (bool): Enable gradient checkpointing. lora_rank (int): LoRA rank. lora_train_bias (str): LoRA bias training mode. """ def __init__(self, pretrained: str = None, config: Optional[BloomConfig] = None, checkpoint: bool = False, lora_rank: int = 0, lora_train_bias: str = 'none') -> None: if pretrained is not None: model = BloomModel.from_pretrained(pretrained) elif config is not None: model = BloomModel(config) else: model = BloomModel(BloomConfig()) if checkpoint: model.gradient_checkpointing_enable() value_head = nn.Linear(model.config.hidden_size, 1) super().__init__(model, value_head, lora_rank, lora_train_bias)
from .actor import Actor from .bloom_actor import BLOOMActor from .bloom_critic import BLOOMCritic from .bloom_rm import BLOOMRM from .critic import Critic from .gpt_actor import GPTActor from .gpt_critic import GPTCritic from .gpt_rm import GPTRM from .loss import PairWiseLoss, PolicyLoss, PPOPtxActorLoss, ValueLoss from .opt_actor import OPTActor from .opt_critic import OPTCritic from .opt_rm import OPTRM from .reward_model import RewardModel __all__ = [ 'Actor', 'Critic', 'RewardModel', 'PolicyLoss', 'ValueLoss', 'PPOPtxActorLoss', 'PairWiseLoss', 'GPTActor', 'GPTCritic', 'GPTRM', 'BLOOMActor', 'BLOOMCritic', 'BLOOMRM', 'OPTActor', 'OPTCritic', 'OPTRM' ]
from typing import Optional import torch def gpt_prepare_inputs_fn(input_ids: torch.Tensor, past: Optional[torch.Tensor] = None, **kwargs) -> dict: token_type_ids = kwargs.get("token_type_ids", None) # only last token for inputs_ids if past is defined in kwargs if past: input_ids = input_ids[:, -1].unsqueeze(-1) if token_type_ids is not None: token_type_ids = token_type_ids[:, -1].unsqueeze(-1) attention_mask = kwargs.get("attention_mask", None) position_ids = kwargs.get("position_ids", None) if attention_mask is not None and position_ids is None: # create position_ids on the fly for batch generation position_ids = attention_mask.long().cumsum(-1) - 1 position_ids.masked_fill_(attention_mask == 0, 1) if past: position_ids = position_ids[:, -1].unsqueeze(-1) else: position_ids = None return { "input_ids": input_ids, "past_key_values": past, "use_cache": kwargs.get("use_cache"), "position_ids": position_ids, "attention_mask": attention_mask, "token_type_ids": token_type_ids, } def update_model_kwargs_fn(outputs: dict, **model_kwargs) -> dict: if "past_key_values" in outputs: model_kwargs["past"] = outputs["past_key_values"] else: model_kwargs["past"] = None # update token_type_ids with last value if "token_type_ids" in model_kwargs: token_type_ids = model_kwargs["token_type_ids"] model_kwargs["token_type_ids"] = torch.cat([token_type_ids, token_type_ids[:, -1].unsqueeze(-1)], dim=-1) # update attention mask if "attention_mask" in model_kwargs: attention_mask = model_kwargs["attention_mask"] model_kwargs["attention_mask"] = torch.cat( [attention_mask, attention_mask.new_ones((attention_mask.shape[0], 1))], dim=-1) return model_kwargs def opt_prepare_inputs_fn(input_ids: torch.Tensor, past: Optional[torch.Tensor] = None, attention_mask: Optional[torch.Tensor] = None, use_cache: Optional[bool] = None, **kwargs) -> dict: # if model is used as a decoder in encoder-decoder model, the decoder attention mask is created on the fly if attention_mask is None: attention_mask = input_ids.new_ones(input_ids.shape) if past: input_ids = input_ids[:, -1:] # first step, decoder_cached_states are empty return { "input_ids": input_ids, # encoder_outputs is defined. input_ids not needed "attention_mask": attention_mask, "past_key_values": past, "use_cache": use_cache, } def bloom_prepare_inputs_fn(input_ids: torch.Tensor, past: Optional[torch.Tensor] = None, attention_mask: Optional[torch.Tensor] = None, use_cache: Optional[bool] = None, **kwargs) -> dict: # if model is used as a decoder in encoder-decoder model, the decoder attention mask is created on the fly if attention_mask is None: attention_mask = input_ids.new_ones(input_ids.shape) if past: input_ids = input_ids[:, -1:] # first step, decoder_cached_states are empty return { "input_ids": input_ids, # encoder_outputs is defined. input_ids not needed "attention_mask": attention_mask, "past_key_values": past, "use_cache": use_cache, }
from typing import Optional import torch import torch.nn as nn from .utils import masked_mean class GPTLMLoss(nn.Module): """ GPT Language Model Loss """ def __init__(self): super().__init__() self.loss = nn.CrossEntropyLoss() def forward(self, logits: torch.Tensor, labels: torch.Tensor) -> torch.Tensor: shift_logits = logits[..., :-1, :].contiguous() shift_labels = labels[..., 1:].contiguous() # Flatten the tokens return self.loss(shift_logits.view(-1, shift_logits.size(-1)), shift_labels.view(-1)) class PolicyLoss(nn.Module): """ Policy Loss for PPO """ def __init__(self, clip_eps: float = 0.2) -> None: super().__init__() self.clip_eps = clip_eps def forward(self, log_probs: torch.Tensor, old_log_probs: torch.Tensor, advantages: torch.Tensor, action_mask: Optional[torch.Tensor] = None) -> torch.Tensor: ratio = (log_probs - old_log_probs).exp() surr1 = ratio * advantages surr2 = ratio.clamp(1 - self.clip_eps, 1 + self.clip_eps) * advantages loss = -torch.min(surr1, surr2) if action_mask is not None: loss = masked_mean(loss, action_mask) loss = loss.mean() return loss class ValueLoss(nn.Module): """ Value Loss for PPO """ def __init__(self, clip_eps: float = 0.4) -> None: super().__init__() self.clip_eps = clip_eps def forward(self, values: torch.Tensor, old_values: torch.Tensor, reward: torch.Tensor, action_mask: Optional[torch.Tensor] = None) -> torch.Tensor: values_clipped = old_values + (values - old_values).clamp(-self.clip_eps, self.clip_eps) surr1 = (values_clipped - reward)**2 surr2 = (values - reward)**2 loss = torch.max(surr1, surr2) loss = loss.mean() return loss class PPOPtxActorLoss(nn.Module): """ To Do: PPO-ptx Actor Loss """ def __init__(self, policy_clip_eps: float = 0.2, pretrain_coef: float = 0.0, pretrain_loss_fn=GPTLMLoss()) -> None: super().__init__() self.pretrain_coef = pretrain_coef self.policy_loss_fn = PolicyLoss(clip_eps=policy_clip_eps) self.pretrain_loss_fn = pretrain_loss_fn def forward(self, log_probs: torch.Tensor, old_log_probs: torch.Tensor, advantages: torch.Tensor, lm_logits: torch.Tensor, lm_input_ids: torch.Tensor, action_mask: Optional[torch.Tensor] = None) -> torch.Tensor: policy_loss = self.policy_loss_fn(log_probs, old_log_probs, advantages, action_mask=action_mask) lm_loss = self.pretrain_loss_fn(lm_logits, lm_input_ids) return policy_loss + self.pretrain_coef * lm_loss class PairWiseLoss(nn.Module): """ Pairwise Loss for Reward Model """ def forward(self, chosen_reward: torch.Tensor, reject_reward: torch.Tensor) -> torch.Tensor: probs = torch.sigmoid(chosen_reward - reject_reward) log_probs = torch.log(probs) loss = -log_probs.mean() return loss
from typing import Optional, Union import loralib as lora import torch import torch.nn as nn import torch.nn.functional as F def compute_approx_kl(log_probs: torch.Tensor, log_probs_base: torch.Tensor, action_mask: Optional[torch.Tensor] = None) -> torch.Tensor: """ Compute the approximate KL divergence between two distributions. Schulman blog: http://joschu.net/blog/kl-approx.html Args: log_probs: Log probabilities of the new distribution. log_probs_base: Log probabilities of the base distribution. action_mask: Mask for actions. """ log_ratio = log_probs - log_probs_base approx_kl = (log_ratio.exp() - 1) - log_ratio if action_mask is not None: approx_kl = masked_mean(approx_kl, action_mask, dim=1) return approx_kl approx_kl = approx_kl.mean(dim=1) return approx_kl def compute_reward(r: Union[torch.Tensor, float], kl_coef: float, log_probs: torch.Tensor, log_probs_base: torch.Tensor, action_mask: Optional[torch.Tensor] = None) -> torch.Tensor: if kl_coef <= 0.0: return r kl = compute_approx_kl(log_probs, log_probs_base, action_mask=action_mask) reward = r - kl_coef * kl return reward def log_probs_from_logits(logits: torch.Tensor, labels: torch.Tensor) -> torch.Tensor: log_probs = F.log_softmax(logits, dim=-1) log_probs_labels = log_probs.gather(dim=-1, index=labels.unsqueeze(-1)) return log_probs_labels.squeeze(-1) def masked_mean(tensor: torch.Tensor, mask: torch.Tensor, dim: int = 1) -> torch.Tensor: tensor = tensor * mask tensor = tensor.sum(dim=dim) mask_sum = mask.sum(dim=dim) mean = tensor / (mask_sum + 1e-8) return mean def masked_normalize(tensor: torch.Tensor, mask: torch.Tensor, dim: int = 1, eps: float = 1e-8) -> torch.Tensor: tensor = tensor * mask mean = masked_mean(tensor, mask, dim=dim) mean_centered = tensor - mean var = masked_mean(mean_centered**2, mask, dim=dim) return mean_centered * var.clamp(min=eps).rsqrt() def normalize(tensor: torch.Tensor, dim: int = 0, eps: float = 1e-8) -> torch.Tensor: mean = tensor.mean(dim) mean_centered = tensor - mean var = (mean_centered**2).mean(dim) norm = mean_centered * var.clamp(min=eps).rsqrt() return norm def convert_to_lora(model: nn.Module, input_size: int, output_size: int, lora_rank: int = 16, lora_alpha: int = 1, lora_dropout: float = 0., fan_in_fan_out: bool = False, merge_weights: bool = True): if lora_rank > min(input_size, output_size): raise ValueError(f"LoRA rank {lora_rank} must be less or equal than {min(input_size, output_size)}") for name, module in model.named_modules(): if isinstance(module, nn.Linear): module._modules[name] = lora.Linear(input_size, output_size, r=lora_rank, lora_alpha=lora_alpha, lora_dropout=lora_dropout, fan_in_fan_out=fan_in_fan_out, merge_weights=merge_weights)
from typing import Optional from transformers.models.opt.configuration_opt import OPTConfig from transformers.models.opt.modeling_opt import OPTForCausalLM from .actor import Actor class OPTActor(Actor): """ OPT Actor model. Args: pretrained (str): Pretrained model name or path. config (OPTConfig): Model config. checkpoint (bool): Enable gradient checkpointing. lora_rank (int): Rank of the low-rank approximation. lora_train_bias (str): LoRA bias training mode. """ def __init__(self, pretrained: Optional[str] = None, config: Optional[OPTConfig] = None, checkpoint: bool = False, lora_rank: int = 0, lora_train_bias: str = 'none') -> None: if pretrained is not None: model = OPTForCausalLM.from_pretrained(pretrained) elif config is not None: model = OPTForCausalLM(config) else: model = OPTForCausalLM(OPTConfig()) if checkpoint: model.gradient_checkpointing_enable() super().__init__(model, lora_rank, lora_train_bias)
from typing import Optional import torch import torch.nn as nn from .lora import LoRAModule class RewardModel(LoRAModule): """ Reward model base class. Args: model (nn.Module): Reward model. value_head (nn.Module): Value head to get reward score. lora_rank (int): LoRA rank. lora_train_bias (str): LoRA bias training mode. """ def __init__(self, model: nn.Module, value_head: Optional[nn.Module] = None, lora_rank: int = 0, lora_train_bias: str = 'none') -> None: super().__init__(lora_rank=lora_rank, lora_train_bias=lora_train_bias) self.model = model if value_head is not None: if value_head.out_features != 1: raise ValueError("The value head of reward model's output dim should be 1!") self.value_head = value_head else: self.value_head = nn.Linear(model.config.n_embd, 1) self.convert_to_lora() def forward(self, sequences: torch.LongTensor, attention_mask: Optional[torch.Tensor] = None) -> torch.Tensor: outputs = self.model(sequences, attention_mask=attention_mask) last_hidden_states = outputs['last_hidden_state'] values = self.value_head(last_hidden_states)[:, :-1] value = values.mean(dim=1).squeeze(1) # ensure shape is (B) return value
import math from typing import Optional import loralib as lora import torch import torch.nn as nn import torch.nn.functional as F class LoraLinear(lora.LoRALayer, nn.Module): """Replace in-place ops to out-of-place ops to fit gemini. Convert a torch.nn.Linear to LoraLinear. """ def __init__( self, weight: nn.Parameter, bias: Optional[nn.Parameter], r: int = 0, lora_alpha: int = 1, lora_dropout: float = 0., fan_in_fan_out: bool = False, # Set this to True if the layer to replace stores weight like (fan_in, fan_out) merge_weights: bool = True, ): nn.Module.__init__(self) lora.LoRALayer.__init__(self, r=r, lora_alpha=lora_alpha, lora_dropout=lora_dropout, merge_weights=merge_weights) self.weight = weight self.bias = bias out_features, in_features = weight.shape self.in_features = in_features self.out_features = out_features self.fan_in_fan_out = fan_in_fan_out # Actual trainable parameters if r > 0: self.lora_A = nn.Parameter(self.weight.new_zeros((r, in_features))) self.lora_B = nn.Parameter(self.weight.new_zeros((out_features, r))) self.scaling = self.lora_alpha / self.r # Freezing the pre-trained weight matrix self.weight.requires_grad = False self.reset_parameters() if fan_in_fan_out: self.weight.data = self.weight.data.T def reset_parameters(self): if hasattr(self, 'lora_A'): # initialize A the same way as the default for nn.Linear and B to zero nn.init.kaiming_uniform_(self.lora_A, a=math.sqrt(5)) nn.init.zeros_(self.lora_B) def train(self, mode: bool = True): def T(w): return w.T if self.fan_in_fan_out else w nn.Module.train(self, mode) if self.merge_weights and self.merged: # Make sure that the weights are not merged if self.r > 0: self.weight.data -= T(self.lora_B @ self.lora_A) * self.scaling self.merged = False def eval(self): def T(w): return w.T if self.fan_in_fan_out else w nn.Module.eval(self) if self.merge_weights and not self.merged: # Merge the weights and mark it if self.r > 0: self.weight.data += T(self.lora_B @ self.lora_A) * self.scaling self.merged = True def forward(self, x: torch.Tensor): def T(w): return w.T if self.fan_in_fan_out else w if self.r > 0 and not self.merged: result = F.linear(x, T(self.weight), bias=self.bias) if self.r > 0: result = result + (self.lora_dropout(x) @ self.lora_A.t() @ self.lora_B.t()) * self.scaling return result else: return F.linear(x, T(self.weight), bias=self.bias) def lora_linear_wrapper(linear: nn.Linear, lora_rank: int) -> LoraLinear: assert lora_rank <= linear.in_features, f'LoRA rank ({lora_rank}) must be less than or equal to in features ({linear.in_features})' lora_linear = LoraLinear(linear.weight, linear.bias, r=lora_rank, merge_weights=False) return lora_linear def convert_to_lora_recursively(module: nn.Module, lora_rank: int) -> None: for name, child in module.named_children(): if isinstance(child, nn.Linear): setattr(module, name, lora_linear_wrapper(child, lora_rank)) else: convert_to_lora_recursively(child, lora_rank) class LoRAModule(nn.Module): """A LoRA module base class. All derived classes should call `convert_to_lora()` at the bottom of `__init__()`. This calss will convert all torch.nn.Linear layer to LoraLinear layer. Args: lora_rank (int, optional): LoRA rank. 0 means LoRA is not applied. Defaults to 0. lora_train_bias (str, optional): Whether LoRA train biases. 'none' means it doesn't train biases. 'all' means it trains all biases. 'lora_only' means it only trains biases of LoRA layers. Defaults to 'none'. """ def __init__(self, lora_rank: int = 0, lora_train_bias: str = 'none') -> None: super().__init__() self.lora_rank = lora_rank self.lora_train_bias = lora_train_bias def convert_to_lora(self) -> None: if self.lora_rank <= 0: return convert_to_lora_recursively(self, self.lora_rank) lora.mark_only_lora_as_trainable(self, self.lora_train_bias)
from typing import Optional, Tuple, Union import torch import torch.nn as nn import torch.nn.functional as F from .generation import generate from .lora import LoRAModule from .utils import log_probs_from_logits class Actor(LoRAModule): """ Actor model base class. Args: model (nn.Module): Actor Model. lora_rank (int): LoRA rank. lora_train_bias (str): LoRA bias training mode. """ def __init__(self, model: nn.Module, lora_rank: int = 0, lora_train_bias: str = 'none') -> None: super().__init__(lora_rank=lora_rank, lora_train_bias=lora_train_bias) self.model = model self.convert_to_lora() @torch.no_grad() def generate( self, input_ids: torch.Tensor, return_action_mask: bool = True, **kwargs ) -> Union[Tuple[torch.LongTensor, torch.LongTensor], Tuple[torch.LongTensor, torch.LongTensor, torch.BoolTensor]]: sequences = generate(self.model, input_ids, **kwargs) attention_mask = None pad_token_id = kwargs.get('pad_token_id', None) if pad_token_id is not None: attention_mask = sequences.not_equal(pad_token_id).to(dtype=torch.long, device=sequences.device) if not return_action_mask: return sequences, attention_mask input_len = input_ids.size(1) eos_token_id = kwargs.get('eos_token_id', None) if eos_token_id is None: action_mask = torch.ones_like(sequences, dtype=torch.bool) else: # left padding may be applied, only mask action action_mask = (sequences[:, input_len:] == eos_token_id).cumsum(dim=-1) == 0 action_mask = F.pad(action_mask, (1 + input_len, -1), value=True) # include eos token and input action_mask[:, :input_len] = False action_mask = action_mask[:, 1:] return sequences, attention_mask, action_mask[:, -(sequences.size(1) - input_len):] def forward(self, sequences: torch.LongTensor, num_actions: int, attention_mask: Optional[torch.Tensor] = None) -> torch.Tensor: """Returns action log probs """ output = self.model(sequences, attention_mask=attention_mask) logits = output['logits'] log_probs = log_probs_from_logits(logits[:, :-1, :], sequences[:, 1:]) return log_probs[:, -num_actions:]
from typing import Optional import torch import torch.nn as nn from .lora import LoRAModule from .utils import masked_mean class Critic(LoRAModule): """ Critic model base class. Args: model (nn.Module): Critic model. value_head (nn.Module): Value head to get value. lora_rank (int): LoRA rank. lora_train_bias (str): LoRA bias training mode. """ def __init__(self, model: nn.Module, value_head: nn.Module, lora_rank: int = 0, lora_train_bias: str = 'none') -> None: super().__init__(lora_rank=lora_rank, lora_train_bias=lora_train_bias) self.model = model self.value_head = value_head self.convert_to_lora() def forward(self, sequences: torch.LongTensor, action_mask: Optional[torch.Tensor] = None, attention_mask: Optional[torch.Tensor] = None) -> torch.Tensor: outputs = self.model(sequences, attention_mask=attention_mask) last_hidden_states = outputs['last_hidden_state'] values = self.value_head(last_hidden_states).squeeze(-1)[:, :-1] if action_mask is not None: num_actions = action_mask.size(1) values = values[:, -num_actions:] value = masked_mean(values, action_mask, dim=1) return value value = values.mean(dim=1).squeeze(1) return value
from typing import Optional from transformers.models.gpt2.configuration_gpt2 import GPT2Config from transformers.models.gpt2.modeling_gpt2 import GPT2LMHeadModel from .actor import Actor class GPTActor(Actor): """ GPT Actor model. Args: pretrained (str): Pretrained model name or path. config (GPT2Config): Model config. checkpoint (bool): Enable gradient checkpointing. """ def __init__(self, pretrained: Optional[str] = None, config: Optional[GPT2Config] = None, checkpoint: bool = False) -> None: if pretrained is not None: model = GPT2LMHeadModel.from_pretrained(pretrained) elif config is not None: model = GPT2LMHeadModel(config) else: model = GPT2LMHeadModel(GPT2Config()) if checkpoint: model.gradient_checkpointing_enable() super().__init__(model)
from typing import Optional import torch.nn as nn from transformers.models.gpt2.configuration_gpt2 import GPT2Config from transformers.models.gpt2.modeling_gpt2 import GPT2Model from .critic import Critic class GPTCritic(Critic): """ GPT Critic model. Args: pretrained (str): Pretrained model name or path. config (GPT2Config): Model config. checkpoint (bool): Enable gradient checkpointing. """ def __init__(self, pretrained: Optional[str] = None, config: Optional[GPT2Config] = None, checkpoint: bool = False) -> None: if pretrained is not None: model = GPT2Model.from_pretrained(pretrained) elif config is not None: model = GPT2Model(config) else: model = GPT2Model(GPT2Config()) if checkpoint: model.gradient_checkpointing_enable() value_head = nn.Linear(model.config.n_embd, 1) super().__init__(model, value_head)
from .reward_dataset import RewardDataset __all__ = ['RewardDataset']
from typing import Callable from torch.utils.data import Dataset from tqdm import tqdm class RewardDataset(Dataset): """ Dataset for reward model Args: dataset: dataset for reward model tokenizer: tokenizer for reward model max_length: max length of input """ def __init__(self, dataset, tokenizer: Callable, max_length: int) -> None: super().__init__() self.chosen = [] self.reject = [] for data in tqdm(dataset): prompt = data['prompt'] chosen = prompt + data['chosen'] + "<|endoftext|>" chosen_token = tokenizer(chosen, max_length=max_length, padding="max_length", truncation=True, return_tensors="pt") self.chosen.append({ "input_ids": chosen_token['input_ids'], "attention_mask": chosen_token['attention_mask'] }) reject = prompt + data['rejected'] + "<|endoftext|>" reject_token = tokenizer(reject, max_length=max_length, padding="max_length", truncation=True, return_tensors="pt") self.reject.append({ "input_ids": reject_token['input_ids'], "attention_mask": reject_token['attention_mask'] }) def __len__(self): length = len(self.chosen) return length def __getitem__(self, idx): return self.chosen[idx]["input_ids"], self.chosen[idx]["attention_mask"], self.reject[idx][ "input_ids"], self.reject[idx]["attention_mask"]
from .base import Experience, ExperienceMaker from .naive import NaiveExperienceMaker __all__ = ['Experience', 'ExperienceMaker', 'NaiveExperienceMaker']
import torch from chatgpt.nn.utils import compute_reward, normalize from .base import Experience, ExperienceMaker class NaiveExperienceMaker(ExperienceMaker): """ Naive experience maker. """ @torch.no_grad() def make_experience(self, input_ids: torch.Tensor, **generate_kwargs) -> Experience: self.actor.eval() self.critic.eval() self.initial_model.eval() self.reward_model.eval() sequences, attention_mask, action_mask = self.actor.generate(input_ids, return_action_mask=True, **generate_kwargs) num_actions = action_mask.size(1) action_log_probs = self.actor(sequences, num_actions, attention_mask) base_action_log_probs = self.initial_model(sequences, num_actions, attention_mask) value = self.critic(sequences, action_mask, attention_mask) r = self.reward_model(sequences, attention_mask) reward = compute_reward(r, self.kl_coef, action_log_probs, base_action_log_probs, action_mask=action_mask) advantage = reward - value # TODO(ver217): maybe normalize adv if advantage.ndim == 1: advantage = advantage.unsqueeze(-1) return Experience(sequences, action_log_probs, value, reward, advantage, attention_mask, action_mask)
from abc import ABC, abstractmethod from dataclasses import dataclass from typing import Optional import torch import torch.nn as nn from chatgpt.nn.actor import Actor @dataclass class Experience: """Experience is a batch of data. These data should have the the sequence length and number of actions. Left padding for sequences is applied. Shapes of each tensor: sequences: (B, S) action_log_probs: (B, A) values: (B) reward: (B) advatanges: (B) attention_mask: (B, S) action_mask: (B, A) "A" is the number of actions. """ sequences: torch.Tensor action_log_probs: torch.Tensor values: torch.Tensor reward: torch.Tensor advantages: torch.Tensor attention_mask: Optional[torch.LongTensor] action_mask: Optional[torch.BoolTensor] @torch.no_grad() def to_device(self, device: torch.device) -> None: self.sequences = self.sequences.to(device) self.action_log_probs = self.action_log_probs.to(device) self.values = self.values.to(device) self.reward = self.reward.to(device) self.advantages = self.advantages.to(device) if self.attention_mask is not None: self.attention_mask = self.attention_mask.to(device) if self.action_mask is not None: self.action_mask = self.action_mask.to(device) def pin_memory(self): self.sequences = self.sequences.pin_memory() self.action_log_probs = self.action_log_probs.pin_memory() self.values = self.values.pin_memory() self.reward = self.reward.pin_memory() self.advantages = self.advantages.pin_memory() if self.attention_mask is not None: self.attention_mask = self.attention_mask.pin_memory() if self.action_mask is not None: self.action_mask = self.action_mask.pin_memory() return self class ExperienceMaker(ABC): def __init__(self, actor: Actor, critic: nn.Module, reward_model: nn.Module, initial_model: Actor, kl_coef: float = 0.1) -> None: super().__init__() self.actor = actor self.critic = critic self.reward_model = reward_model self.initial_model = initial_model self.kl_coef = kl_coef @abstractmethod def make_experience(self, input_ids: torch.Tensor, **generate_kwargs) -> Experience: pass
from .base import Trainer from .ppo import PPOTrainer from .rm import RewardModelTrainer __all__ = ['Trainer', 'PPOTrainer', 'RewardModelTrainer']
from abc import ABC import loralib as lora from chatgpt.dataset import RewardDataset from chatgpt.nn import PairWiseLoss from torch.optim import Adam, Optimizer from torch.utils.data import DataLoader from tqdm import tqdm from .strategies import Strategy from .utils import is_rank_0 class RewardModelTrainer(ABC): """ Trainer to use while training reward model. Args: model (torch.nn.Module): the model to train strategy (Strategy): the strategy to use for training optim(Optimizer): the optimizer to use for training train_dataset (RewardDataset): the dataset to use for training eval_dataset (RewardDataset): the dataset to use for evaluation batch_size (int, defaults to 1): the batch size while training max_epochs (int, defaults to 2): the number of epochs to train optim_kwargs (dict, defaults to {'lr':1e-4}): the kwargs to use while initializing optimizer """ def __init__( self, model, strategy: Strategy, optim: Optimizer, train_dataset: RewardDataset, eval_dataset: RewardDataset, batch_size: int = 1, max_epochs: int = 2, ) -> None: super().__init__() self.strategy = strategy self.epochs = max_epochs self.train_dataloader = DataLoader(train_dataset, batch_size=batch_size) self.eval_dataloader = DataLoader(eval_dataset, batch_size=batch_size) self.model = strategy.setup_model(model) self.loss_fn = PairWiseLoss() self.optimizer = strategy.setup_optimizer(optim, self.model) def fit(self, use_lora): epoch_bar = tqdm(range(self.epochs), desc='Train epoch', disable=not is_rank_0()) for epoch in range(self.epochs): step_bar = tqdm(range(self.train_dataloader.__len__()), desc='Train step of epoch %d' % epoch, disable=not is_rank_0()) # train if use_lora > 0: print("Using Lora") lora.mark_only_lora_as_trainable(self.model.model) else: self.model.train() for chosen_ids, c_mask, reject_ids, r_mask in self.train_dataloader: chosen_ids = chosen_ids.squeeze(1).cuda() c_mask = c_mask.squeeze(1).cuda() reject_ids = reject_ids.squeeze(1).cuda() r_mask = r_mask.squeeze(1).cuda() chosen_reward = self.model(chosen_ids, attention_mask=c_mask) reject_reward = self.model(reject_ids, attention_mask=r_mask) loss = self.loss_fn(chosen_reward, reject_reward) self.strategy.backward(loss, self.model, self.optimizer) self.strategy.optimizer_step(self.optimizer) self.optimizer.zero_grad() step_bar.update() step_bar.set_postfix({'loss': loss.item()}) # eval self.model.eval() for chosen_ids, c_mask, reject_ids, r_mask in self.eval_dataloader: dist = 0 chosen_ids = chosen_ids.squeeze(1).cuda() c_mask = c_mask.squeeze(1).cuda() reject_ids = reject_ids.squeeze(1).cuda() r_mask = r_mask.squeeze(1).cuda() chosen_reward = self.model(chosen_ids, attention_mask=c_mask) reject_reward = self.model(reject_ids, attention_mask=r_mask) dist += (chosen_reward - reject_reward) dist_mean = dist / self.eval_dataloader.__len__() epoch_bar.update() step_bar.set_postfix({'loss': loss.item(), 'dist_mean': dist_mean.item()}) step_bar.close()
import torch.distributed as dist def is_rank_0() -> bool: return not dist.is_initialized() or dist.get_rank() == 0
from typing import Any, Callable, Dict, List, Optional import torch.nn as nn from chatgpt.experience_maker import Experience, NaiveExperienceMaker from chatgpt.nn import Actor, Critic, PolicyLoss, ValueLoss from chatgpt.nn.generation_utils import update_model_kwargs_fn from chatgpt.replay_buffer import NaiveReplayBuffer from torch.optim import Optimizer from .base import Trainer from .callbacks import Callback from .strategies import Strategy class PPOTrainer(Trainer): """ Trainer for PPO algorithm. Args: strategy (Strategy): the strategy to use for training actor (Actor): the actor model in ppo algorithm critic (Critic): the critic model in ppo algorithm reward_model (nn.Module): the reward model in rlhf algorithm to make reward of sentences initial_model (Actor): the initial model in rlhf algorithm to generate reference logits to limit the update of actor actor_optim (Optimizer): the optimizer to use for actor model critic_optim (Optimizer): the optimizer to use for critic model kl_coef (float, defaults to 0.1): the coefficient of kl divergence loss train_batch_size (int, defaults to 8): the batch size to use for training buffer_limit (int, defaults to 0): the max_size limitaiton of replay buffer buffer_cpu_offload (bool, defaults to True): whether to offload replay buffer to cpu eps_clip (float, defaults to 0.2): the clip coefficient of policy loss value_clip (float, defaults to 0.4): the clip coefficient of value loss experience_batch_size (int, defaults to 8): the batch size to use for experience generation max_epochs (int, defaults to 1): the number of epochs of training process tokenier (Callable, optional): the tokenizer to use for tokenizing the input sample_replay_buffer (bool, defaults to False): whether to sample from replay buffer dataloader_pin_memory (bool, defaults to True): whether to pin memory for data loader callbacks (List[Callback], defaults to []): the callbacks to call during training process generate_kwargs (dict, optional): the kwargs to use while model generating """ def __init__(self, strategy: Strategy, actor: Actor, critic: Critic, reward_model: nn.Module, initial_model: Actor, actor_optim: Optimizer, critic_optim: Optimizer, kl_coef: float = 0.1, train_batch_size: int = 8, buffer_limit: int = 0, buffer_cpu_offload: bool = True, eps_clip: float = 0.2, value_clip: float = 0.4, experience_batch_size: int = 8, max_epochs: int = 1, tokenizer: Optional[Callable[[Any], dict]] = None, sample_replay_buffer: bool = False, dataloader_pin_memory: bool = True, callbacks: List[Callback] = [], **generate_kwargs) -> None: self._set_default_generate_kwargs(generate_kwargs, actor) actor = Actor(strategy.setup_model(actor.model)) critic = strategy.setup_model(critic) reward_model = strategy.setup_model(reward_model) initial_model = Actor(strategy.setup_model(initial_model.model)) experience_maker = NaiveExperienceMaker(actor, critic, reward_model, initial_model, kl_coef) replay_buffer = NaiveReplayBuffer(train_batch_size, buffer_limit, buffer_cpu_offload) super().__init__(strategy, experience_maker, replay_buffer, experience_batch_size, max_epochs, tokenizer, sample_replay_buffer, dataloader_pin_memory, callbacks, **generate_kwargs) self.actor = actor self.critic = critic self.actor_loss_fn = PolicyLoss(eps_clip) self.critic_loss_fn = ValueLoss(value_clip) self.actor_optim = strategy.setup_optimizer(actor_optim, self.actor.model) self.critic_optim = strategy.setup_optimizer(critic_optim, self.critic) def training_step(self, experience: Experience) -> Dict[str, float]: self.actor.train() self.critic.train() num_actions = experience.action_mask.size(1) action_log_probs = self.actor(experience.sequences, num_actions, attention_mask=experience.attention_mask) actor_loss = self.actor_loss_fn(action_log_probs, experience.action_log_probs, experience.advantages, action_mask=experience.action_mask) self.strategy.backward(actor_loss, self.actor, self.actor_optim) self.strategy.optimizer_step(self.actor_optim) self.actor_optim.zero_grad() values = self.critic(experience.sequences, action_mask=experience.action_mask, attention_mask=experience.attention_mask) critic_loss = self.critic_loss_fn(values, experience.values, experience.reward, action_mask=experience.action_mask) self.strategy.backward(critic_loss, self.critic, self.critic_optim) self.strategy.optimizer_step(self.critic_optim) self.critic_optim.zero_grad() return {'actor_loss': actor_loss.item(), 'critic_loss': critic_loss.item()} def _set_default_generate_kwargs(self, generate_kwargs: dict, actor: Actor) -> None: # use huggingface models method directly if 'prepare_inputs_fn' not in generate_kwargs and hasattr(actor.model, 'prepare_inputs_for_generation'): generate_kwargs['prepare_inputs_fn'] = actor.model.prepare_inputs_for_generation if 'update_model_kwargs_fn' not in generate_kwargs: generate_kwargs['update_model_kwargs_fn'] = update_model_kwargs_fn
import random from abc import ABC, abstractmethod from typing import Any, Callable, Dict, List, Optional, Union import torch from chatgpt.experience_maker import Experience, ExperienceMaker from chatgpt.replay_buffer import ReplayBuffer from torch import Tensor from torch.utils.data import DistributedSampler from tqdm import tqdm from .callbacks import Callback from .strategies import Strategy from .utils import is_rank_0 class Trainer(ABC): """ Base class for rlhf trainers. Args: strategy (Strategy):the strategy to use for training experience_maker (ExperienceMaker): the experience maker to use for produce experience to fullfill replay buffer replay_buffer (ReplayBuffer): the replay buffer to use for training experience_batch_size (int, defaults to 8): the batch size to use for experience generation max_epochs (int, defaults to 1): the number of epochs of training process tokenizer (Callable, optional): the tokenizer to use for tokenizing the input sample_replay_buffer (bool, defaults to False): whether to sample from replay buffer data_loader_pin_memory (bool, defaults to True): whether to pin memory for data loader callbacks (List[Callback], defaults to []): the callbacks to call during training process generate_kwargs (dict, optional): the kwargs to use while model generating """ def __init__(self, strategy: Strategy, experience_maker: ExperienceMaker, replay_buffer: ReplayBuffer, experience_batch_size: int = 8, max_epochs: int = 1, tokenizer: Optional[Callable[[Any], dict]] = None, sample_replay_buffer: bool = False, dataloader_pin_memory: bool = True, callbacks: List[Callback] = [], **generate_kwargs) -> None: super().__init__() self.strategy = strategy self.experience_maker = experience_maker self.replay_buffer = replay_buffer self.experience_batch_size = experience_batch_size self.max_epochs = max_epochs self.tokenizer = tokenizer self.generate_kwargs = generate_kwargs self.sample_replay_buffer = sample_replay_buffer self.dataloader_pin_memory = dataloader_pin_memory self.callbacks = callbacks @abstractmethod def training_step(self, experience: Experience) -> Dict[str, Any]: pass def _make_experience(self, inputs: Union[Tensor, Dict[str, Tensor]]) -> Experience: if isinstance(inputs, Tensor): return self.experience_maker.make_experience(inputs, **self.generate_kwargs) elif isinstance(inputs, dict): return self.experience_maker.make_experience(**inputs, **self.generate_kwargs) else: raise ValueError(f'Unsupported input type "{type(inputs)}"') def _sample_prompts(self, prompts) -> list: indices = list(range(len(prompts))) sampled_indices = random.sample(indices, self.experience_batch_size) return [prompts[i] for i in sampled_indices] def _learn(self): # replay buffer may be empty at first, we should rebuild at each training if not self.sample_replay_buffer: dataloader = self.strategy.setup_dataloader(self.replay_buffer, self.dataloader_pin_memory) device = torch.cuda.current_device() if self.sample_replay_buffer: pbar = tqdm(range(self.max_epochs), desc='Train epoch', disable=not is_rank_0()) for _ in pbar: experience = self.replay_buffer.sample() metrics = self.training_step(experience) pbar.set_postfix(metrics) else: for epoch in range(self.max_epochs): self._on_learn_epoch_start(epoch) if isinstance(dataloader.sampler, DistributedSampler): dataloader.sampler.set_epoch(epoch) pbar = tqdm(dataloader, desc=f'Train epoch [{epoch+1}/{self.max_epochs}]', disable=not is_rank_0()) for experience in pbar: self._on_learn_batch_start() experience.to_device(device) metrics = self.training_step(experience) self._on_learn_batch_end(metrics, experience) pbar.set_postfix(metrics) self._on_learn_epoch_end(epoch) def fit(self, prompts, num_episodes: int = 50000, max_timesteps: int = 500, update_timesteps: int = 5000) -> None: time = 0 self._on_fit_start() for episode in range(num_episodes): self._on_episode_start(episode) for timestep in tqdm(range(max_timesteps), desc=f'Episode [{episode+1}/{num_episodes}]', disable=not is_rank_0()): time += 1 rand_prompts = self._sample_prompts(prompts) if self.tokenizer is not None: inputs = self.tokenizer(rand_prompts) else: inputs = rand_prompts self._on_make_experience_start() experience = self._make_experience(inputs) self._on_make_experience_end(experience) self.replay_buffer.append(experience) if time % update_timesteps == 0: self._learn() self.replay_buffer.clear() self._on_episode_end(episode) self._on_fit_end() # TODO(ver217): maybe simplify these code using context def _on_fit_start(self) -> None: for callback in self.callbacks: callback.on_fit_start() def _on_fit_end(self) -> None: for callback in self.callbacks: callback.on_fit_end() def _on_episode_start(self, episode: int) -> None: for callback in self.callbacks: callback.on_episode_start(episode) def _on_episode_end(self, episode: int) -> None: for callback in self.callbacks: callback.on_episode_end(episode) def _on_make_experience_start(self) -> None: for callback in self.callbacks: callback.on_make_experience_start() def _on_make_experience_end(self, experience: Experience) -> None: for callback in self.callbacks: callback.on_make_experience_end(experience) def _on_learn_epoch_start(self, epoch: int) -> None: for callback in self.callbacks: callback.on_learn_epoch_start(epoch) def _on_learn_epoch_end(self, epoch: int) -> None: for callback in self.callbacks: callback.on_learn_epoch_end(epoch) def _on_learn_batch_start(self) -> None: for callback in self.callbacks: callback.on_learn_batch_start() def _on_learn_batch_end(self, metrics: dict, experience: Experience) -> None: for callback in self.callbacks: callback.on_learn_batch_end(metrics, experience)
from .base import Callback from .performance_evaluator import PerformanceEvaluator __all__ = ['Callback', 'PerformanceEvaluator']
from time import time from typing import Optional import torch import torch.distributed as dist from chatgpt.experience_maker import Experience from .base import Callback def get_world_size() -> int: if dist.is_initialized(): return dist.get_world_size() return 1 def print_rank_0(*args, **kwargs) -> None: if not dist.is_initialized() or dist.get_rank() == 0: print(*args, **kwargs) @torch.no_grad() def all_reduce_mean(x: float, world_size: int) -> float: if world_size == 1: return x tensor = torch.tensor([x], device=torch.cuda.current_device()) dist.all_reduce(tensor) tensor = tensor / world_size return tensor.item() class PerformanceEvaluator(Callback): """ Callback for valuate the performance of the model. Args: actor_num_params: The number of parameters of the actor model. critic_num_params: The number of parameters of the critic model. initial_model_num_params: The number of parameters of the initial model. reward_model_num_params: The number of parameters of the reward model. enable_grad_checkpoint: Whether to enable gradient checkpointing. ignore_episodes: The number of episodes to ignore when calculating the performance. """ def __init__(self, actor_num_params: int, critic_num_params: int, initial_model_num_params: int, reward_model_num_params: int, enable_grad_checkpoint: bool = False, ignore_episodes: int = 0) -> None: super().__init__() self.world_size = get_world_size() self.actor_num_params = actor_num_params self.critic_num_params = critic_num_params self.initial_model_num_params = initial_model_num_params self.reward_model_num_params = reward_model_num_params self.enable_grad_checkpoint = enable_grad_checkpoint self.ignore_episodes = ignore_episodes self.disable: bool = False self.make_experience_duration: float = 0. self.make_experience_start_time: Optional[float] = None self.make_experience_num_samples: int = 0 self.make_experience_flop: int = 0 self.learn_duration: float = 0. self.learn_start_time: Optional[float] = None self.learn_num_samples: int = 0 self.learn_flop: int = 0 def on_episode_start(self, episode: int) -> None: self.disable = self.ignore_episodes > 0 and episode < self.ignore_episodes def on_make_experience_start(self) -> None: if self.disable: return self.make_experience_start_time = time() def on_make_experience_end(self, experience: Experience) -> None: if self.disable: return self.make_experience_duration += time() - self.make_experience_start_time batch_size, seq_len = experience.sequences.shape self.make_experience_num_samples += batch_size # actor generate num_actions = experience.action_mask.size(1) input_len = seq_len - num_actions total_seq_len = (input_len + seq_len - 1) * num_actions / 2 self.make_experience_flop += self.actor_num_params * batch_size * total_seq_len * 2 # actor forward self.make_experience_flop += self.actor_num_params * batch_size * seq_len * 2 # critic forward self.make_experience_flop += self.critic_num_params * batch_size * seq_len * 2 # initial model forward self.make_experience_flop += self.initial_model_num_params * batch_size * seq_len * 2 # reward model forward self.make_experience_flop += self.reward_model_num_params * batch_size * seq_len * 2 def on_learn_batch_start(self) -> None: if self.disable: return self.learn_start_time = time() def on_learn_batch_end(self, metrics: dict, experience: Experience) -> None: if self.disable: return self.learn_duration += time() - self.learn_start_time batch_size, seq_len = experience.sequences.shape self.learn_num_samples += batch_size # actor forward-backward, 3 means forward(1) + backward(2) self.learn_flop += self.actor_num_params * batch_size * seq_len * 2 * (3 + int(self.enable_grad_checkpoint)) # critic foward-backward self.learn_flop += self.critic_num_params * batch_size * seq_len * 2 * (3 + int(self.enable_grad_checkpoint)) def on_fit_end(self) -> None: avg_make_experience_duration = all_reduce_mean(self.make_experience_duration, self.world_size) avg_learn_duration = all_reduce_mean(self.learn_duration, self.world_size) avg_make_experience_throughput = self.make_experience_num_samples / (avg_make_experience_duration + 1e-12) avg_make_experience_tflops = self.make_experience_flop / 1e12 / (avg_make_experience_duration + 1e-12) avg_learn_throughput = self.learn_num_samples / (avg_learn_duration + 1e-12) avg_learn_tflops = self.learn_flop / 1e12 / (avg_learn_duration + 1e-12) print_rank_0( f'Making experience throughput: {avg_make_experience_throughput:.3f} samples/sec, TFLOPS: {avg_make_experience_tflops:.3f}' ) print_rank_0(f'Learning throughput: {avg_learn_throughput:.3f} samples/sec, TFLOPS: {avg_learn_tflops:.3f}')
from abc import ABC from chatgpt.experience_maker import Experience class Callback(ABC): """ Base callback class. It defines the interface for callbacks. """ def on_fit_start(self) -> None: pass def on_fit_end(self) -> None: pass def on_episode_start(self, episode: int) -> None: pass def on_episode_end(self, episode: int) -> None: pass def on_make_experience_start(self) -> None: pass def on_make_experience_end(self, experience: Experience) -> None: pass def on_learn_epoch_start(self, epoch: int) -> None: pass def on_learn_epoch_end(self, epoch: int) -> None: pass def on_learn_batch_start(self) -> None: pass def on_learn_batch_end(self, metrics: dict, experience: Experience) -> None: pass
import os import random import numpy as np import torch import torch.distributed as dist import torch.nn as nn from chatgpt.replay_buffer import ReplayBuffer from torch.nn.parallel import DistributedDataParallel as DDP from torch.utils.data import DataLoader, DistributedSampler from .naive import NaiveStrategy class DDPStrategy(NaiveStrategy): """ Strategy for distributed training using torch.distributed. """ def __init__(self, seed: int = 42) -> None: self.seed = seed super().__init__() def setup_distributed(self) -> None: try: rank = int(os.environ['RANK']) local_rank = int(os.environ['LOCAL_RANK']) world_size = int(os.environ['WORLD_SIZE']) host = os.environ['MASTER_ADDR'] port = int(os.environ['MASTER_PORT']) except KeyError as e: raise RuntimeError( f"Could not find {e} in the torch environment, visit https://www.colossalai.org/ for more information on launching with torch" ) dist.init_process_group('nccl', init_method=f'tcp://[{host}]:{port}', world_size=world_size, rank=rank) self.set_seed(self.seed) torch.cuda.set_device(local_rank) def set_seed(self, seed: int) -> None: random.seed(seed) np.random.seed(seed) torch.manual_seed(seed) def setup_model(self, model: nn.Module) -> nn.Module: device = torch.cuda.current_device() return DDP(model, device_ids=[device]) def setup_dataloader(self, replay_buffer: ReplayBuffer, pin_memory: bool = False) -> DataLoader: sampler = DistributedSampler(replay_buffer, num_replicas=dist.get_world_size(), rank=dist.get_rank(), shuffle=True, seed=self.seed, drop_last=True) return DataLoader(replay_buffer, batch_size=replay_buffer.sample_batch_size, sampler=sampler, pin_memory=pin_memory, collate_fn=replay_buffer.collate_fn)
from .base import Strategy from .colossalai import ColossalAIStrategy from .ddp import DDPStrategy from .naive import NaiveStrategy __all__ = ['Strategy', 'NaiveStrategy', 'DDPStrategy', 'ColossalAIStrategy']
import warnings from typing import Optional import torch import torch.distributed as dist import torch.nn as nn import torch.optim as optim import colossalai from colossalai.nn.optimizer import CPUAdam, HybridAdam from colossalai.nn.parallel import zero_model_wrapper, zero_optim_wrapper from colossalai.tensor import ProcessGroup, ShardSpec from colossalai.utils import get_current_device from colossalai.utils.model.colo_init_context import ColoInitContext from .ddp import DDPStrategy class ColossalAIStrategy(DDPStrategy): """ The strategy for training with ColossalAI. Args: stage(int): The stage to use in ZeRO. Choose in (1, 2, 3) seed(int): The seed for the random number generator. shard_init(bool): Whether to shard the model parameters during initialization. Only for ZeRO-3. This is not compativle with `from_pretrained()`. We temporarily disable this and will support it in the future. placement_policy(str): The placement policy for gemini. Choose in ('cpu', 'cuda') If it is “cpu”, parameters, gradients and optimizer states will be offloaded to CPU, If it is “cuda”, they will not be offloaded, which means max CUDA memory will be used. It is the fastest. pin_memory(bool): Whether to pin the memory for the data loader. Only for ZeRO-3. force_outputs_fp32(bool): Whether to force the outputs to be fp32. Only for ZeRO-3. search_range_mb(int): The search range in MB for the chunk size. Only for ZeRO-3. hidden_dim(optional, int): The hidden dimension for the gemini. Only for ZeRO-3. min_chunk_size_mb(float): The minimum chunk size in MB. Only for ZeRO-3. gpu_margin_mem_ratio(float): The margin memory ratio for the GPU. Only for ZeRO-3. reduce_bugket_size(int): The reduce bucket size in bytes. Only for ZeRO-1 and ZeRO-2. overlap_communication(bool): Whether to overlap communication and computation. Only for ZeRO-1 and ZeRO-2. initial_scale(float): The initial scale for the optimizer. growth_factor(float): The growth factor for the optimizer. backoff_factor(float): The backoff factor for the optimizer. growth_interval(int): The growth interval for the optimizer. hysteresis(int): The hysteresis for the optimizer. min_scale(float): The minimum scale for the optimizer. max_scale(float): The maximum scale for the optimizer. max_norm(float): The maximum norm for the optimizer. norm_type(float): The norm type for the optimizer. """ def __init__( self, stage: int = 3, seed: int = 42, shard_init: bool = False, # only for stage 3 placement_policy: str = 'cuda', pin_memory: bool = True, # only for stage 3 force_outputs_fp32: bool = False, # only for stage 3 search_range_mb: int = 32, # only for stage 3 hidden_dim: Optional[int] = None, # only for stage 3 min_chunk_size_mb: float = 32, # only for stage 3 gpu_margin_mem_ratio: float = 0.0, # only for stage 3 reduce_bucket_size: int = 12 * 1024**2, # only for stage 1&2 overlap_communication: bool = True, # only for stage 1&2 initial_scale: float = 2**16, growth_factor: float = 2, backoff_factor: float = 0.5, growth_interval: int = 1000, hysteresis: int = 2, min_scale: float = 1, max_scale: float = 2**32, max_norm: float = 0.0, norm_type: float = 2.0) -> None: super().__init__(seed) assert placement_policy in ('cpu', 'cuda'), f'Unsupported placement policy "{placement_policy}"' self.stage = stage # TODO(ver217): support shard_init when using from_pretrained() if shard_init: warnings.warn(f'Shard init is not supported yet. Ignore.') shard_init = False self.shard_init = shard_init self.gemini_config = dict(device=get_current_device(), placement_policy=placement_policy, pin_memory=pin_memory, force_outputs_fp32=force_outputs_fp32, strict_ddp_mode=shard_init, search_range_mb=search_range_mb, hidden_dim=hidden_dim, min_chunk_size_mb=min_chunk_size_mb) if stage == 3: self.zero_optim_config = dict(gpu_margin_mem_ratio=gpu_margin_mem_ratio) else: self.zero_optim_config = dict(reduce_bucket_size=reduce_bucket_size, overlap_communication=overlap_communication, cpu_offload=(placement_policy == 'cpu')) self.optim_kwargs = dict(initial_scale=initial_scale, growth_factor=growth_factor, backoff_factor=backoff_factor, growth_interval=growth_interval, hysteresis=hysteresis, min_scale=min_scale, max_scale=max_scale, max_norm=max_norm, norm_type=norm_type) def setup_distributed(self) -> None: colossalai.launch_from_torch({}, seed=self.seed) def model_init_context(self): if self.stage == 3: world_size = dist.get_world_size() shard_pg = ProcessGroup(tp_degree=world_size) if self.shard_init else None default_dist_spec = ShardSpec([-1], [world_size]) if self.shard_init else None return ColoInitContext(device=get_current_device(), dtype=torch.half, default_pg=shard_pg, default_dist_spec=default_dist_spec) return super().model_init_context() def setup_model(self, model: nn.Module) -> nn.Module: return zero_model_wrapper(model, zero_stage=self.stage, gemini_config=self.gemini_config) def setup_optimizer(self, optimizer: optim.Optimizer, model: nn.Module) -> optim.Optimizer: assert isinstance(optimizer, (CPUAdam, HybridAdam)), f'Unsupported optimizer {type(optimizer)}' return zero_optim_wrapper(model, optimizer, optim_config=self.zero_optim_config, **self.optim_kwargs) def backward(self, loss: torch.Tensor, model: nn.Module, optimizer: optim.Optimizer, **kwargs) -> None: optimizer.backward(loss) def optimizer_step(self, optimizer: optim.Optimizer, **kwargs) -> None: optimizer.step()
import torch import torch.nn as nn import torch.optim as optim from chatgpt.replay_buffer import ReplayBuffer from torch.utils.data import DataLoader from .base import Strategy class NaiveStrategy(Strategy): """ Strategy for single GPU. No parallelism is used. """ def backward(self, loss: torch.Tensor, model: nn.Module, optimizer: optim.Optimizer, **kwargs) -> None: loss.backward() def optimizer_step(self, optimizer: optim.Optimizer, **kwargs) -> None: optimizer.step() def setup_distributed(self) -> None: pass def setup_model(self, model: nn.Module) -> nn.Module: return model def setup_optimizer(self, optimizer: optim.Optimizer, model: nn.Module) -> optim.Optimizer: return optimizer def setup_dataloader(self, replay_buffer: ReplayBuffer, pin_memory: bool = False) -> DataLoader: return DataLoader(replay_buffer, batch_size=replay_buffer.sample_batch_size, shuffle=True, drop_last=True, pin_memory=pin_memory, collate_fn=replay_buffer.collate_fn)
from abc import ABC, abstractmethod from contextlib import nullcontext import torch import torch.nn as nn import torch.optim as optim from chatgpt.replay_buffer import ReplayBuffer from torch.utils.data import DataLoader class Strategy(ABC): """ Base class for training strategies. """ def __init__(self) -> None: super().__init__() self.setup_distributed() @abstractmethod def backward(self, loss: torch.Tensor, model: nn.Module, optimizer: optim.Optimizer, **kwargs) -> None: pass @abstractmethod def optimizer_step(self, optimizer: optim.Optimizer, **kwargs) -> None: pass @abstractmethod def setup_distributed(self) -> None: pass @abstractmethod def setup_model(self, model: nn.Module) -> nn.Module: pass @abstractmethod def setup_optimizer(self, optimizer: optim.Optimizer, model: nn.Module) -> optim.Optimizer: pass @abstractmethod def setup_dataloader(self, replay_buffer: ReplayBuffer, pin_memory: bool = False) -> DataLoader: pass def model_init_context(self): return nullcontext()
# # \file generator.py # # \brief Generates the CUTLASS Library's instances # import enum import os.path import shutil import functools import operator import collections from library import * ################################################################################################### # # Data structure modeling a GEMM operation # ################################################################################################### # class GemmOperation: # def __init__(self, gemm_kind, arch, tile_description, A, B, C, element_epilogue, \ epilogue_functor = EpilogueFunctor.LinearCombination, swizzling_functor = SwizzlingFunctor.Identity8): self.prefix = "3x" if gemm_kind == GemmKind.Universal3x else "" self.operation_kind = OperationKind.Gemm self.arch = arch self.tile_description = tile_description self.gemm_kind = gemm_kind self.A = A self.B = B self.C = C self.element_epilogue = element_epilogue self.epilogue_functor = epilogue_functor self.swizzling_functor = swizzling_functor # def is_complex(self): complex_operators = [ MathOperation.multiply_add_complex, MathOperation.multiply_add_complex_gaussian, MathOperation.multiply_add_complex_fast_f32 ] return self.tile_description.math_instruction.math_operation in complex_operators # def is_planar_complex(self): return self.gemm_kind in (GemmKind.PlanarComplex, GemmKind.PlanarComplexArray) # def accumulator_type(self): accum = self.tile_description.math_instruction.element_accumulator if self.is_complex(): return get_complex_from_real(accum) return accum # def short_math_name(self): if self.tile_description.math_instruction.math_operation == MathOperation.multiply_add_complex_gaussian: return "g%s" % ShortDataTypeNames[self.accumulator_type()] return ShortDataTypeNames[self.accumulator_type()] # def core_name(self): ''' The basic operation kind is prefixed with a letter indicating the accumulation type. ''' inst_shape = '' inst_operation = '' intermediate_type = '' math_operations_map = { MathOperation.xor_popc: 'xor', } if self.tile_description.math_instruction.opcode_class == OpcodeClass.TensorOp or \ self.tile_description.math_instruction.opcode_class == OpcodeClass.WmmaTensorOp: math_op = self.tile_description.math_instruction.math_operation math_op_string = math_operations_map[math_op] if math_op in math_operations_map.keys() else '' if self.gemm_kind == GemmKind.Universal3x: inst_shape = "{0}x{1}x{2}".format(*tuple(self.tile_description.math_instruction.instruction_shape)) else: inst_shape = "{0}{1}{2}".format(*tuple(self.tile_description.math_instruction.instruction_shape)) inst_shape += math_op_string if self.tile_description.math_instruction.element_a != self.A.element and \ self.tile_description.math_instruction.element_a != self.tile_description.math_instruction.element_accumulator: intermediate_type = DataTypeNames[self.tile_description.math_instruction.element_a] return "%s%s%s%s" % (self.short_math_name(), inst_shape, intermediate_type, GemmKindNames[self.gemm_kind]) # Generates a string representing the MMA instruction. def extended_name(self): ''' Append data types if they differ from compute type. ''' if self.is_complex(): extended_name = "${core_name}" else: if self.C.element != self.tile_description.math_instruction.element_accumulator and \ self.A.element != self.tile_description.math_instruction.element_accumulator: extended_name = "${element_c}_${core_name}_${element_a}" elif self.C.element == self.tile_description.math_instruction.element_accumulator and \ self.A.element != self.tile_description.math_instruction.element_accumulator: extended_name = "${core_name}_${element_a}" else: extended_name = "${core_name}" extended_name = SubstituteTemplate(extended_name, { 'element_a': DataTypeNames[self.A.element], 'element_c': DataTypeNames[self.C.element], 'core_name': self.core_name() }) return extended_name def extended_name_3x(self): '''Generates a string representing the MMA atom. Assumes accumulator type is C type.''' extended_name = "{core_name}_{element_a}_{element_b}_{element_acc}_{element_c}".format( element_a = DataTypeNames[self.A.element], element_b = DataTypeNames[self.B.element], element_acc = DataTypeNames[self.tile_description.math_instruction.element_accumulator], element_c = DataTypeNames[self.C.element], core_name = self.core_name()) return extended_name # Generates a short string representing the AB layout tags (e.g. nt or tn) def layout_name(self): if self.is_complex() or self.is_planar_complex(): return "%s%s" % ( ShortComplexLayoutNames[(self.A.layout, self.A.complex_transform)], ShortComplexLayoutNames[(self.B.layout, self.B.complex_transform)] ) return "%s%s" % (ShortLayoutTypeNames[self.A.layout], ShortLayoutTypeNames[self.B.layout]) # Generates a short string representing the ABC layout tags (e.g. ntn or tnn) def layout_name_3x(self): if self.is_complex() or self.is_planar_complex(): return "{}{}{}".format( ShortComplexLayoutNames[(self.A.layout, self.A.complex_transform)], ShortComplexLayoutNames[(self.B.layout, self.B.complex_transform)], ShortComplexLayoutNames[(self.C.layout, self.C.complex_transform)]) else: return "{}{}{}".format( ShortLayoutTypeNames[self.A.layout], ShortLayoutTypeNames[self.B.layout], ShortLayoutTypeNames[self.C.layout]) # Generates the full kernel function name def procedural_name(self): ''' The full procedural name indicates architecture, extended name, tile size, and layout. ''' opcode_class_name = OpcodeClassNames[self.tile_description.math_instruction.opcode_class] if self.arch >= 90: kernel_name_template = "cutlass{p}_sm{ar}_{op}_{ex}_{tbm}x{tbn}x{tbk}_{cm}x{cn}x{ck}_{l}_{s}_align{al}" return kernel_name_template.format( p = self.prefix, ar = self.arch, op = opcode_class_name, ex = self.extended_name_3x(), tbm = self.tile_description.threadblock_shape[0], tbn = self.tile_description.threadblock_shape[1], tbk = self.tile_description.threadblock_shape[2], cm = self.tile_description.cluster_shape[0], cn = self.tile_description.cluster_shape[1], ck = self.tile_description.cluster_shape[2], l = self.tile_description.stages, s = self.layout_name_3x(), al = str(max(self.A.alignment, self.B.alignment))) else: threadblock = self.tile_description.procedural_name() return "cutlass{p}_{op}_{ex}_{tb}_{l}_align{a}".format( p = self.prefix, op = opcode_class_name, ex = self.extended_name(), tb = threadblock, l = self.layout_name(), a = str(self.A.alignment)) # def configuration_name(self): ''' The full procedural name indicates architecture, extended name, tile size, and layout. ''' return self.procedural_name() ################################################################################################### # # Data structure modeling a grouped GEMM operation # ################################################################################################### # class GroupedGemmOperation(GemmOperation): # def __init__(self, gemm_kind, arch, tile_description, A, B, C, element_epilogue, \ epilogue_functor = EpilogueFunctor.LinearCombination, swizzling_functor = SwizzlingFunctor.Identity8, \ scheduler_mode = GroupScheduleMode.Device): super().__init__(gemm_kind, arch, tile_description, A, B, C, element_epilogue, \ epilogue_functor, swizzling_functor) self.scheduler_mode = scheduler_mode # def procedural_name(self): ''' The full procedural name indicates architecture, extended name, tile size, and layout. ''' base = super().procedural_name() return SubstituteTemplate( base + "_schedule${schedule}", { 'schedule': ShortGroupScheduleModeNames[self.scheduler_mode] }) ################################################################################################### # # Emits single instances of a CUTLASS device-wide operator # ################################################################################################### # class EmitGemmInstance: ''' Responsible for emitting a CUTLASS template definition''' def __init__(self, operation_suffix = ''): self.operation_suffix = operation_suffix self.includes = [] self.gemm_template = """ // Gemm operator ${operation_name} using Operation_${operation_name} = cutlass::gemm::device::Gemm< ${element_a}, ${layout_a}, ${element_b}, ${layout_b}, ${element_c}, ${layout_c}, ${element_accumulator}, ${opcode_class}, ${arch}, cutlass::gemm::GemmShape<${threadblock_shape_m}, ${threadblock_shape_n}, ${threadblock_shape_k}>, cutlass::gemm::GemmShape<${warp_shape_m}, ${warp_shape_n}, ${warp_shape_k}>, cutlass::gemm::GemmShape<${instruction_shape_m}, ${instruction_shape_n}, ${instruction_shape_k}>, ${epilogue_functor}< ${element_c}, ${epilogue_vector_length}, ${element_accumulator}, ${element_epilogue} >, ${swizzling_functor}, ${stages}, ${align_a}, ${align_b}, false, ${math_operation} ${residual} >; """ self.gemm_complex_template = """ // Gemm operator ${operation_name} using Operation_${operation_name} = cutlass::gemm::device::GemmComplex< ${element_a}, ${layout_a}, ${element_b}, ${layout_b}, ${element_c}, ${layout_c}, ${element_accumulator}, ${opcode_class}, ${arch}, cutlass::gemm::GemmShape<${threadblock_shape_m}, ${threadblock_shape_n}, ${threadblock_shape_k}>, cutlass::gemm::GemmShape<${warp_shape_m}, ${warp_shape_n}, ${warp_shape_k}>, cutlass::gemm::GemmShape<${instruction_shape_m}, ${instruction_shape_n}, ${instruction_shape_k}>, ${epilogue_functor}< ${element_c}, ${epilogue_vector_length}, ${element_accumulator}, ${element_epilogue} >, ${swizzling_functor}, ${stages}, ${transform_a}, ${transform_b}, ${math_operation} ${residual} >; """ # def instance_template(self): return """ ${compile_guard_start} manifest.append(new ${gemm_kind}<Operation_${operation_name}>("${operation_name}")); ${compile_guard_end} """ # def emit(self, operation): warp_shape = [operation.tile_description.threadblock_shape[idx] // operation.tile_description.warp_count[idx] for idx in range(3)] epilogue_vector_length = int(min(operation.C.alignment * DataTypeSize[operation.C.element], 128) / DataTypeSize[operation.C.element]) residual = '' values = { 'operation_name': operation.procedural_name(), 'element_a': DataTypeTag[operation.A.element], 'layout_a': LayoutTag[operation.A.layout], 'element_b': DataTypeTag[operation.B.element], 'layout_b': LayoutTag[operation.B.layout], 'element_c': DataTypeTag[operation.C.element], 'layout_c': LayoutTag[operation.C.layout], 'element_accumulator': DataTypeTag[operation.accumulator_type()], 'opcode_class': OpcodeClassTag[operation.tile_description.math_instruction.opcode_class], 'arch': "cutlass::arch::Sm%d" % operation.arch, 'threadblock_shape_m': str(operation.tile_description.threadblock_shape[0]), 'threadblock_shape_n': str(operation.tile_description.threadblock_shape[1]), 'threadblock_shape_k': str(operation.tile_description.threadblock_shape[2]), 'warp_shape_m': str(warp_shape[0]), 'warp_shape_n': str(warp_shape[1]), 'warp_shape_k': str(warp_shape[2]), 'instruction_shape_m': str(operation.tile_description.math_instruction.instruction_shape[0]), 'instruction_shape_n': str(operation.tile_description.math_instruction.instruction_shape[1]), 'instruction_shape_k': str(operation.tile_description.math_instruction.instruction_shape[2]), 'epilogue_vector_length': str(epilogue_vector_length), 'element_epilogue': str(DataTypeTag[operation.element_epilogue]), 'epilogue_functor': EpilogueFunctorTag[operation.epilogue_functor], 'swizzling_functor': SwizzlingFunctorTag[operation.swizzling_functor], 'stages': str(operation.tile_description.stages), 'align_a': str(operation.A.alignment), 'align_b': str(operation.B.alignment), 'transform_a': ComplexTransformTag[operation.A.complex_transform], 'transform_b': ComplexTransformTag[operation.B.complex_transform], 'math_operation': MathOperationTag[operation.tile_description.math_instruction.math_operation], 'residual': residual } template = self.gemm_complex_template if operation.is_complex() else self.gemm_template return SubstituteTemplate(template, values) ################################################################################################### class EmitSparseGemmInstance: ''' Responsible for emitting a CUTLASS template definition''' def __init__(self, operation_suffix = ''): self.operation_suffix = operation_suffix self.includes = [] self.gemm_template = """ // Gemm operator ${operation_name} using Operation_${operation_name} = cutlass::gemm::device::SparseGemm< ${element_a}, ${layout_a}, ${element_b}, ${layout_b}, ${element_c}, ${layout_c}, ${element_accumulator}, ${opcode_class}, ${arch}, cutlass::gemm::GemmShape<${threadblock_shape_m}, ${threadblock_shape_n}, ${threadblock_shape_k}>, cutlass::gemm::GemmShape<${warp_shape_m}, ${warp_shape_n}, ${warp_shape_k}>, cutlass::gemm::GemmShape<${instruction_shape_m}, ${instruction_shape_n}, ${instruction_shape_k}>, ${epilogue_functor}< ${element_c}, ${epilogue_vector_length}, ${element_accumulator}, ${element_epilogue} >, ${swizzling_functor}, ${stages}, ${align_a}, ${align_b}, false, ${math_operation} ${residual} >; """ # def instance_template(self): return """ ${compile_guard_start} manifest.append(new ${gemm_kind}<Operation_${operation_name}>("${operation_name}")); ${compile_guard_end} """ # def emit(self, operation): warp_shape = [operation.tile_description.threadblock_shape[idx] // operation.tile_description.warp_count[idx] for idx in range(3)] epilogue_vector_length = int(min(operation.C.alignment * DataTypeSize[operation.C.element], 128) / DataTypeSize[operation.C.element]) residual = '' values = { 'operation_name': operation.procedural_name(), 'element_a': DataTypeTag[operation.A.element], 'layout_a': LayoutTag[operation.A.layout], 'element_b': DataTypeTag[operation.B.element], 'layout_b': LayoutTag[operation.B.layout], 'element_c': DataTypeTag[operation.C.element], 'layout_c': LayoutTag[operation.C.layout], 'element_accumulator': DataTypeTag[operation.accumulator_type()], 'opcode_class': OpcodeClassTag[operation.tile_description.math_instruction.opcode_class], 'arch': "cutlass::arch::Sm%d" % operation.arch, 'threadblock_shape_m': str(operation.tile_description.threadblock_shape[0]), 'threadblock_shape_n': str(operation.tile_description.threadblock_shape[1]), 'threadblock_shape_k': str(operation.tile_description.threadblock_shape[2]), 'warp_shape_m': str(warp_shape[0]), 'warp_shape_n': str(warp_shape[1]), 'warp_shape_k': str(warp_shape[2]), 'instruction_shape_m': str(operation.tile_description.math_instruction.instruction_shape[0]), 'instruction_shape_n': str(operation.tile_description.math_instruction.instruction_shape[1]), 'instruction_shape_k': str(operation.tile_description.math_instruction.instruction_shape[2]), 'epilogue_vector_length': str(epilogue_vector_length), 'element_epilogue': str(DataTypeTag[operation.element_epilogue]), 'epilogue_functor': EpilogueFunctorTag[operation.epilogue_functor], 'swizzling_functor': SwizzlingFunctorTag[operation.swizzling_functor], 'stages': str(operation.tile_description.stages), 'align_a': str(operation.A.alignment), 'align_b': str(operation.B.alignment), 'transform_a': ComplexTransformTag[operation.A.complex_transform], 'transform_b': ComplexTransformTag[operation.B.complex_transform], 'math_operation': MathOperationTag[operation.tile_description.math_instruction.math_operation], 'residual': residual } template = self.gemm_template return SubstituteTemplate(template, values) ################################################################################################### # class EmitGemmUniversalInstance: ''' Responsible for emitting a CUTLASS template definition''' def __init__(self, operation_suffix = ''): self.operation_suffix = operation_suffix self.includes = [ "cutlass/cutlass.h", "cutlass/numeric_types.h", "cutlass/arch/arch.h", "cutlass/arch/mma.h", "cutlass/layout/matrix.h", "cutlass/gemm/device/gemm.h", "cutlass/gemm/device/gemm_universal_adapter.h", "cutlass/gemm/kernel/default_gemm_universal.h", ] self.builtin_epilogue_functor_template = """ ${epilogue_functor}< ${element_c}, ${epilogue_vector_length}, ${element_accumulator}, ${element_epilogue} > """ self.gemm_template = """ // Gemm operator ${operation_name} using ${operation_name}_base = typename cutlass::gemm::kernel::DefaultGemmUniversal< ${element_b}, ${layout_b}, ${transform_b}, ${align_b}, // transposed B operand ${element_a}, ${layout_a}, ${transform_a}, ${align_a}, // transposed A operand ${element_c}, ${layout_c}, ${element_accumulator}, ${opcode_class}, ${arch}, cutlass::gemm::GemmShape<${threadblock_shape_m}, ${threadblock_shape_n}, ${threadblock_shape_k}>, cutlass::gemm::GemmShape<${warp_shape_m}, ${warp_shape_n}, ${warp_shape_k}>, cutlass::gemm::GemmShape<${instruction_shape_m}, ${instruction_shape_n}, ${instruction_shape_k}>, ${epilogue_functor}, ${swizzling_functor}, ${stages}, ${math_operation} >::GemmKernel; // Define named type struct ${operation_name}${operation_suffix} : public ${operation_name}_base { }; """ self.gemm_template_interleaved = """ // Gemm operator ${operation_name} using ${operation_name}_base = typename cutlass::gemm::kernel::DefaultGemmUniversal< ${element_a}, ${layout_a}, ${transform_a}, ${align_a}, ${element_b}, ${layout_b}, ${transform_b}, ${align_b}, ${element_c}, ${layout_c}, ${element_accumulator}, ${opcode_class}, ${arch}, cutlass::gemm::GemmShape<${threadblock_shape_m}, ${threadblock_shape_n}, ${threadblock_shape_k}>, cutlass::gemm::GemmShape<${warp_shape_m}, ${warp_shape_n}, ${warp_shape_k}>, cutlass::gemm::GemmShape<${instruction_shape_m}, ${instruction_shape_n}, ${instruction_shape_k}>, ${epilogue_functor}, ${swizzling_functor}, ${stages}, ${math_operation} >::GemmKernel; // Define named type struct ${operation_name}${operation_suffix} : public ${operation_name}_base { }; """ # def instance_template(self): return """ ${compile_guard_start} manifest.append(new ${gemm_kind}< cutlass::gemm::device::GemmUniversalAdapter<${operation_name}> >("${operation_name}")); ${compile_guard_end} """ # def emit(self, operation): threadblock_shape = operation.tile_description.threadblock_shape warp_count = operation.tile_description.warp_count warp_shape = [threadblock_shape[idx] // warp_count[idx] for idx in range(3)] transpose_layouts = { LayoutType.ColumnMajor: LayoutType.RowMajor, LayoutType.RowMajor: LayoutType.ColumnMajor } if operation.A.layout in transpose_layouts.keys() and \ operation.B.layout in transpose_layouts.keys() and \ operation.C.layout in transpose_layouts.keys(): instance_layout_A = transpose_layouts[operation.A.layout] instance_layout_B = transpose_layouts[operation.B.layout] instance_layout_C = transpose_layouts[operation.C.layout] gemm_template = self.gemm_template else: instance_layout_A, instance_layout_B, instance_layout_C = \ (operation.A.layout, operation.B.layout, operation.C.layout) gemm_template = self.gemm_template_interleaved # # Support built-in epilogue functors or user-defined functions if isinstance(operation.epilogue_functor, enum.Enum): epilogue_vector_length = \ min(operation.C.alignment * DataTypeSize[operation.C.element], 128) // DataTypeSize[operation.C.element] values = { 'epilogue_vector_length': str(epilogue_vector_length), 'element_epilogue': str(DataTypeTag[operation.element_epilogue]), 'epilogue_functor': EpilogueFunctorTag[operation.epilogue_functor], } epilogue_functor = SubstituteTemplate(self.builtin_epilogue_functor_template, values) else: epilogue_functor = self.epilogue_functor.emit_declaration() # values = { 'operation_name': operation.procedural_name(), 'operation_suffix': self.operation_suffix, 'element_a': DataTypeTag[operation.A.element], 'layout_a': LayoutTag[instance_layout_A], 'element_b': DataTypeTag[operation.B.element], 'layout_b': LayoutTag[instance_layout_B], 'element_c': DataTypeTag[operation.C.element], 'layout_c': LayoutTag[instance_layout_C], 'element_accumulator': DataTypeTag[operation.accumulator_type()], 'opcode_class': OpcodeClassTag[operation.tile_description.math_instruction.opcode_class], 'arch': "cutlass::arch::Sm%d" % operation.arch, 'threadblock_shape_m': str(operation.tile_description.threadblock_shape[0]), 'threadblock_shape_n': str(operation.tile_description.threadblock_shape[1]), 'threadblock_shape_k': str(operation.tile_description.threadblock_shape[2]), 'warp_shape_m': str(warp_shape[0]), 'warp_shape_n': str(warp_shape[1]), 'warp_shape_k': str(warp_shape[2]), 'instruction_shape_m': str(operation.tile_description.math_instruction.instruction_shape[0]), 'instruction_shape_n': str(operation.tile_description.math_instruction.instruction_shape[1]), 'instruction_shape_k': str(operation.tile_description.math_instruction.instruction_shape[2]), 'epilogue_functor': epilogue_functor, 'swizzling_functor': SwizzlingFunctorTag[operation.swizzling_functor], 'stages': str(operation.tile_description.stages), 'align_a': str(operation.A.alignment), 'align_b': str(operation.B.alignment), 'transform_a': ComplexTransformTag[operation.A.complex_transform], 'transform_b': ComplexTransformTag[operation.B.complex_transform], 'math_operation': MathOperationTag[operation.tile_description.math_instruction.math_operation] } return SubstituteTemplate(gemm_template, values) ################################################################################################### # class EmitGemmUniversal3xInstance: ''' Responsible for emitting a CUTLASS 3.x template definition''' def __init__(self, operation_suffix = ''): self.operation_suffix = operation_suffix self.includes = [ "cutlass/cutlass.h", "cutlass/gemm/gemm.h", "cutlass/numeric_types.h", "cutlass/gemm/kernel/gemm_universal.hpp", "cutlass/gemm/collective/collective_builder.hpp", "cutlass/epilogue/collective/default_epilogue.hpp", "cutlass/epilogue/thread/linear_combination.h", ] self.builtin_epilogue_functor_template = """ ${epilogue_functor}< ${element_c}, ${epilogue_vector_length}, ${element_accumulator}, ${element_epilogue} > """ self.gemm_template = """ using ${operation_name}_mainloop = typename cutlass::gemm::collective::CollectiveBuilder< ${arch}, ${opcode_class}, ${element_a}, ${layout_a}, ${align_a}, ${element_b}, ${layout_b}, ${align_b}, ${element_accumulator}, cute::Shape<cute::_${threadblock_shape_m}, cute::_${threadblock_shape_n}, cute::_${threadblock_shape_k}>, cute::Shape<cute::_${cluster_m},cute::_${cluster_n},cute::_${cluster_k}>, cutlass::gemm::collective::StageCountAuto, cutlass::gemm::collective::KernelScheduleAuto >::CollectiveOp; using ${operation_name}_epilogue = cutlass::epilogue::collective::DefaultEpilogue< cutlass::gemm::TagToStrideC_t<${layout_c}>, cutlass::gemm::TagToStrideC_t<${layout_c}>, cutlass::epilogue::thread::LinearCombination< ${element_c}, ${epilogue_vector_length}, ${element_accumulator}, ${element_epilogue}> >; // Gemm operator ${operation_name} using ${operation_name}_base = cutlass::gemm::kernel::GemmUniversal< cute::Shape<int,int,int,int>, ${operation_name}_mainloop, ${operation_name}_epilogue>; // Define named type struct ${operation_name} : public ${operation_name}_base { }; """ # def instance_template(self): return """ ${compile_guard_start} using GemmKernel = cutlass::gemm::device::GemmUniversalAdapter<${operation_name}>; manifest.append( new ${gemm_kind}<GemmKernel>("${operation_name}")); ${compile_guard_end} """ # def emit(self, operation): threadblock_shape = operation.tile_description.threadblock_shape warp_count = operation.tile_description.warp_count # stage count set to zero indicates builder automatic stage selection if operation.tile_description.stages > 0: stage_count_string = f"cutlass::gemm::collective::StageCount<{str(operation.tile_description.stages)}>" else: stage_count_string = "cutlass::gemm::collective::StageCountAuto" warp_shape = [threadblock_shape[idx] // warp_count[idx] for idx in range(3)] instance_layout_A, instance_layout_B, instance_layout_C = \ (operation.A.layout, operation.B.layout, operation.C.layout) # 3.0 profiler integration only supports trivial epilogues for now epilogue_vector_length = 1 # Support built-in epilogue functors or user-defined functions if isinstance(operation.epilogue_functor, enum.Enum): values = { 'epilogue_vector_length': str(epilogue_vector_length), 'element_epilogue': str(DataTypeTag[operation.element_epilogue]), 'epilogue_functor': EpilogueFunctorTag[operation.epilogue_functor], } epilogue_functor = SubstituteTemplate(self.builtin_epilogue_functor_template, values) else: epilogue_functor = self.epilogue_functor.emit_declaration() # values = { 'operation_name': operation.procedural_name(), 'operation_suffix': self.operation_suffix, 'element_a': DataTypeTag[operation.A.element], 'layout_a': LayoutTag[instance_layout_A], 'element_b': DataTypeTag[operation.B.element], 'layout_b': LayoutTag[instance_layout_B], 'element_c': DataTypeTag[operation.C.element], 'layout_c': LayoutTag[instance_layout_C], 'element_accumulator': DataTypeTag[operation.accumulator_type()], 'opcode_class': OpcodeClassTag[operation.tile_description.math_instruction.opcode_class], 'arch': "cutlass::arch::Sm%d" % operation.arch, 'threadblock_shape_m': str(operation.tile_description.threadblock_shape[0]), 'threadblock_shape_n': str(operation.tile_description.threadblock_shape[1]), 'threadblock_shape_k': str(operation.tile_description.threadblock_shape[2]), 'cluster_m': str(operation.tile_description.cluster_shape[0]), 'cluster_n': str(operation.tile_description.cluster_shape[1]), 'cluster_k': str(operation.tile_description.cluster_shape[2]), 'warp_shape_m': str(warp_shape[0]), 'warp_shape_n': str(warp_shape[1]), 'warp_shape_k': str(warp_shape[2]), 'instruction_shape_m': str(operation.tile_description.math_instruction.instruction_shape[0]), 'instruction_shape_n': str(operation.tile_description.math_instruction.instruction_shape[1]), 'instruction_shape_k': str(operation.tile_description.math_instruction.instruction_shape[2]), 'epilogue_functor': epilogue_functor, 'stages': stage_count_string, 'align_a': str(operation.A.alignment), 'align_b': str(operation.B.alignment), 'transform_a': ComplexTransformTag[operation.A.complex_transform], 'transform_b': ComplexTransformTag[operation.B.complex_transform], 'math_operation': MathOperationTag[operation.tile_description.math_instruction.math_operation], 'epilogue_vector_length': str(epilogue_vector_length), 'element_epilogue': str(DataTypeTag[operation.element_epilogue]), } return SubstituteTemplate(self.gemm_template, values) ################################################################################################### # class EmitGemmPlanarComplexInstance: ''' Responsible for emitting a CUTLASS template definition''' def __init__(self, operation_suffix = ''): self.operation_suffix = operation_suffix self.includes = [] self.template = """ // Gemm operator ${operation_name} using Operation_${operation_name} = typename cutlass::gemm::kernel::DefaultGemmPlanarComplexUniversal< ${element_a}, ${layout_a}, ${transform_a}, ${alignment_a}, ${element_b}, ${layout_b}, ${transform_b}, ${alignment_b}, ${element_c}, cutlass::layout::RowMajor, ${element_accumulator}, ${opcode_class}, ${arch}, cutlass::gemm::GemmShape<${threadblock_shape_m}, ${threadblock_shape_n}, ${threadblock_shape_k}>, cutlass::gemm::GemmShape<${warp_shape_m}, ${warp_shape_n}, ${warp_shape_k}>, cutlass::gemm::GemmShape<${instruction_shape_m}, ${instruction_shape_n}, ${instruction_shape_k}>, cutlass::epilogue::thread::LinearCombinationPlanarComplex< ${element_c}, ${alignment_c}, ${element_accumulator}, ${element_epilogue} >, cutlass::gemm::threadblock::GemmIdentityThreadblockSwizzle<>, ${stages}, ${math_operator} >::GemmKernel; struct ${operation_name} : public Operation_${operation_name} { }; """ # def instance_template(self): return """ ${compile_guard_start} manifest.append(new ${gemm_kind}< cutlass::gemm::device::GemmUniversalAdapter<${operation_name}> >("${operation_name}")); ${compile_guard_end} """ # def emit(self, operation): warp_shape = [operation.tile_description.threadblock_shape[idx] // operation.tile_description.warp_count[idx] for idx in range(3)] # exchange and transpose A and B types, layouts, and complex transforms since the C layout is row-major transposed_layout_A = TransposedLayout[operation.A.layout] transposed_layout_B = TransposedLayout[operation.B.layout] values = { 'operation_name': operation.procedural_name(), 'element_a': DataTypeTag[operation.B.element], 'layout_a': LayoutTag[transposed_layout_B], 'transform_a': ComplexTransformTag[operation.B.complex_transform], 'alignment_a': str(operation.B.alignment), 'element_b': DataTypeTag[operation.A.element], 'layout_b': LayoutTag[transposed_layout_A], 'transform_b': ComplexTransformTag[operation.A.complex_transform], 'alignment_b': str(operation.A.alignment), 'element_c': DataTypeTag[operation.C.element], 'layout_c': LayoutTag[operation.C.layout], 'element_accumulator': DataTypeTag[operation.tile_description.math_instruction.element_accumulator], 'opcode_class': OpcodeClassTag[operation.tile_description.math_instruction.opcode_class], 'arch': "cutlass::arch::Sm%d" % operation.arch, 'threadblock_shape_m': str(operation.tile_description.threadblock_shape[0]), 'threadblock_shape_n': str(operation.tile_description.threadblock_shape[1]), 'threadblock_shape_k': str(operation.tile_description.threadblock_shape[2]), 'warp_shape_m': str(warp_shape[0]), 'warp_shape_n': str(warp_shape[1]), 'warp_shape_k': str(warp_shape[2]), 'instruction_shape_m': str(operation.tile_description.math_instruction.instruction_shape[0]), 'instruction_shape_n': str(operation.tile_description.math_instruction.instruction_shape[1]), 'instruction_shape_k': str(operation.tile_description.math_instruction.instruction_shape[2]), 'alignment_c': str(operation.C.alignment), 'element_epilogue': str(DataTypeTag[operation.element_epilogue]), 'stages': str(operation.tile_description.stages), 'math_operator': 'cutlass::arch::OpMultiplyAdd' } return SubstituteTemplate(self.template, values) ################################################################################################### # class EmitGemmPlanarComplexArrayInstance: ''' Responsible for emitting a CUTLASS template definition''' def __init__(self, operation_suffix = ''): self.operation_suffix = operation_suffix self.includes = [] self.template = """ // Gemm operator ${operation_name} using Operation_${operation_name} = typename cutlass::gemm::kernel::DefaultGemmPlanarComplexUniversal< ${element_a}, ${layout_a}, ${transform_a}, ${alignment_a}, ${element_b}, ${layout_b}, ${transform_b}, ${alignment_b}, ${element_c}, cutlass::layout::RowMajor, ${element_accumulator}, ${opcode_class}, ${arch}, cutlass::gemm::GemmShape<${threadblock_shape_m}, ${threadblock_shape_n}, ${threadblock_shape_k}>, cutlass::gemm::GemmShape<${warp_shape_m}, ${warp_shape_n}, ${warp_shape_k}>, cutlass::gemm::GemmShape<${instruction_shape_m}, ${instruction_shape_n}, ${instruction_shape_k}>, cutlass::epilogue::thread::LinearCombinationPlanarComplex< ${element_c}, ${alignment_c}, ${element_accumulator}, ${element_epilogue} >, cutlass::gemm::threadblock::GemmIdentityThreadblockSwizzle<>, ${stages}, ${math_operator} >::GemmArrayKernel; struct ${operation_name} : public Operation_${operation_name} { }; """ # def instance_template(self): return """ ${compile_guard_start} manifest.append(new ${gemm_kind}< cutlass::gemm::device::GemmUniversalAdapter<${operation_name}> >("${operation_name}")); ${compile_guard_end} """ # def emit(self, operation): warp_shape = [operation.tile_description.threadblock_shape[idx] // operation.tile_description.warp_count[idx] for idx in range(3)] # exchange and transpose A and B types, layouts, and complex transforms since the C layout is row-major transposed_layout_A = TransposedLayout[operation.A.layout] transposed_layout_B = TransposedLayout[operation.B.layout] values = { 'operation_name': operation.procedural_name(), 'element_a': DataTypeTag[operation.B.element], 'layout_a': LayoutTag[transposed_layout_B], 'transform_a': ComplexTransformTag[operation.B.complex_transform], 'alignment_a': str(operation.B.alignment), 'element_b': DataTypeTag[operation.A.element], 'layout_b': LayoutTag[transposed_layout_A], 'transform_b': ComplexTransformTag[operation.A.complex_transform], 'alignment_b': str(operation.A.alignment), 'element_c': DataTypeTag[operation.C.element], 'layout_c': LayoutTag[operation.C.layout], 'element_accumulator': DataTypeTag[operation.tile_description.math_instruction.element_accumulator], 'opcode_class': OpcodeClassTag[operation.tile_description.math_instruction.opcode_class], 'arch': "cutlass::arch::Sm%d" % operation.arch, 'threadblock_shape_m': str(operation.tile_description.threadblock_shape[0]), 'threadblock_shape_n': str(operation.tile_description.threadblock_shape[1]), 'threadblock_shape_k': str(operation.tile_description.threadblock_shape[2]), 'warp_shape_m': str(warp_shape[0]), 'warp_shape_n': str(warp_shape[1]), 'warp_shape_k': str(warp_shape[2]), 'instruction_shape_m': str(operation.tile_description.math_instruction.instruction_shape[0]), 'instruction_shape_n': str(operation.tile_description.math_instruction.instruction_shape[1]), 'instruction_shape_k': str(operation.tile_description.math_instruction.instruction_shape[2]), 'alignment_c': str(operation.C.alignment), 'element_epilogue': str(DataTypeTag[operation.element_epilogue]), 'stages': str(operation.tile_description.stages), 'math_operator': 'cutlass::arch::OpMultiplyAdd' } return SubstituteTemplate(self.template, values) ################################################################################################### # class EmitGemmGroupedInstance: ''' Responsible for emitting a CUTLASS template definition''' def __init__(self, operation_suffix = ''): self.operation_suffix = operation_suffix self.includes = [ "cutlass/cutlass.h", "cutlass/numeric_types.h", "cutlass/arch/arch.h", "cutlass/arch/mma.h", "cutlass/layout/matrix.h", "cutlass/gemm/device/gemm.h", "cutlass/gemm/kernel/gemm_grouped.h", "cutlass/gemm/kernel/default_gemm_grouped.h", "cutlass/gemm/device/gemm_grouped.h" ] self.builtin_epilogue_functor_template = """ ${epilogue_functor}< ${element_c}, ${epilogue_vector_length}, ${element_accumulator}, ${element_epilogue} > """ self.gemm_template = """ // Gemm operator ${operation_name} using ${operation_name}_base = typename cutlass::gemm::kernel::DefaultGemmGrouped< ${element_a}, ${layout_a}, ${transform_a}, ${align_a}, ${element_b}, ${layout_b}, ${transform_b}, ${align_b}, ${element_c}, ${layout_c}, ${element_accumulator}, ${opcode_class}, ${arch}, cutlass::gemm::GemmShape<${threadblock_shape_m}, ${threadblock_shape_n}, ${threadblock_shape_k}>, cutlass::gemm::GemmShape<${warp_shape_m}, ${warp_shape_n}, ${warp_shape_k}>, cutlass::gemm::GemmShape<${instruction_shape_m}, ${instruction_shape_n}, ${instruction_shape_k}>, ${epilogue_functor}, ${swizzling_functor}, ${stages}, ${scheduler_mode}, ${math_operation} >::GemmKernel; // Define named type struct ${operation_name}${operation_suffix} : public ${operation_name}_base { }; """ # def instance_template(self): return """ ${compile_guard_start} manifest.append(new ${gemm_kind}< cutlass::gemm::device::GemmGrouped<${operation_name}> >("${operation_name}")); ${compile_guard_end} """ # def emit(self, operation): threadblock_shape = operation.tile_description.threadblock_shape warp_count = operation.tile_description.warp_count warp_shape = [threadblock_shape[idx] // warp_count[idx] for idx in range(3)] transpose_layouts = { LayoutType.ColumnMajor: LayoutType.RowMajor, LayoutType.RowMajor: LayoutType.ColumnMajor } instance_layout_A, instance_layout_B, instance_layout_C = \ (operation.A.layout, operation.B.layout, operation.C.layout) # # Support built-in epilogue functors or user-defined functions if isinstance(operation.epilogue_functor, enum.Enum): epilogue_vector_length = \ min(operation.C.alignment * DataTypeSize[operation.C.element], 128) // DataTypeSize[operation.C.element] values = { 'epilogue_vector_length': str(epilogue_vector_length), 'element_epilogue': str(DataTypeTag[operation.element_epilogue]), 'epilogue_functor': EpilogueFunctorTag[operation.epilogue_functor], } epilogue_functor = SubstituteTemplate(self.builtin_epilogue_functor_template, values) else: epilogue_functor = self.epilogue_functor.emit_declaration() # values = { 'operation_name': operation.procedural_name(), 'operation_suffix': self.operation_suffix, 'element_a': DataTypeTag[operation.A.element], 'layout_a': LayoutTag[instance_layout_A], 'element_b': DataTypeTag[operation.B.element], 'layout_b': LayoutTag[instance_layout_B], 'element_c': DataTypeTag[operation.C.element], 'layout_c': LayoutTag[instance_layout_C], 'element_accumulator': DataTypeTag[operation.accumulator_type()], 'opcode_class': OpcodeClassTag[operation.tile_description.math_instruction.opcode_class], 'arch': "cutlass::arch::Sm%d" % operation.arch, 'threadblock_shape_m': str(operation.tile_description.threadblock_shape[0]), 'threadblock_shape_n': str(operation.tile_description.threadblock_shape[1]), 'threadblock_shape_k': str(operation.tile_description.threadblock_shape[2]), 'warp_shape_m': str(warp_shape[0]), 'warp_shape_n': str(warp_shape[1]), 'warp_shape_k': str(warp_shape[2]), 'instruction_shape_m': str(operation.tile_description.math_instruction.instruction_shape[0]), 'instruction_shape_n': str(operation.tile_description.math_instruction.instruction_shape[1]), 'instruction_shape_k': str(operation.tile_description.math_instruction.instruction_shape[2]), 'epilogue_functor': epilogue_functor, 'swizzling_functor': SwizzlingFunctorTag[operation.swizzling_functor], 'stages': str(operation.tile_description.stages), 'align_a': str(operation.A.alignment), 'align_b': str(operation.B.alignment), 'transform_a': ComplexTransformTag[operation.A.complex_transform], 'transform_b': ComplexTransformTag[operation.B.complex_transform], 'scheduler_mode': GroupScheduleModeTag[operation.scheduler_mode], 'math_operation': MathOperationTag[operation.tile_description.math_instruction.math_operation] } return SubstituteTemplate(self.gemm_template, values) ################################################################################################### # # Emitters functions for all targets # ################################################################################################### class EmitGemmConfigurationLibrary: def __init__(self, operation_path, configuration_name): self.configuration_name = configuration_name self.configuration_path = os.path.join(operation_path, "%s.cu" % configuration_name).replace('\\', '/') self.instance_emitter = { GemmKind.Gemm: EmitGemmInstance, GemmKind.Sparse: EmitSparseGemmInstance, GemmKind.Universal: EmitGemmUniversalInstance, GemmKind.Universal3x: EmitGemmUniversal3xInstance, GemmKind.PlanarComplex: EmitGemmPlanarComplexInstance, GemmKind.PlanarComplexArray: EmitGemmPlanarComplexArrayInstance, GemmKind.Grouped: EmitGemmGroupedInstance } self.gemm_kind_wrappers = { GemmKind.Gemm: 'GemmOperation', GemmKind.Sparse: 'GemmSparseOperation', GemmKind.Universal: 'GemmUniversalOperation', GemmKind.Universal3x: 'GemmUniversal3xOperation', GemmKind.PlanarComplex: 'GemmPlanarComplexOperation', GemmKind.PlanarComplexArray: 'GemmPlanarComplexArrayOperation', GemmKind.Grouped: 'GemmGroupedOperation' } self.wmma_guard_start = "#if defined(CUTLASS_ARCH_WMMA_SM${sm_number}_ENABLED)" self.separator = """ /////////////////////////////////////////////////////////////////////////////////////////////////// """ self.header_template = """ /* Generated by gemm_operation.py - Do not edit. */ """ self.initialize_function_template = """ /////////////////////////////////////////////////////////////////////////////////////////////////// namespace cutlass { namespace library { /////////////////////////////////////////////////////////////////////////////////////////////////// void initialize_${configuration_name}(Manifest &manifest) { """ self.epilogue_template = """ } /////////////////////////////////////////////////////////////////////////////////////////////////// } // namespace library } // namespace cutlass /////////////////////////////////////////////////////////////////////////////////////////////////// """ def __enter__(self): self.configuration_file = open(self.configuration_path, "w") self.configuration_file.write(self.header_template) self.configuration_file.write(self.separator) self.includes = collections.OrderedDict([ ("cutlass/cutlass.h", None), ("cutlass/library/library.h", None), ("cutlass/library/manifest.h", None), ("library_internal.h", None), ("gemm_operation.h", None), ("gemm_operation_3x.hpp", None), ("cutlass/arch/wmma.h", None), ("cutlass/numeric_types.h", None) ]) self.instance_definitions = [] self.instance_wrappers = [] self.operations = [] return self def emit(self, operation): emitter = self.instance_emitter[operation.gemm_kind]() for incl in emitter.includes: self.includes[incl] = None self.operations.append(operation) self.instance_definitions.append(emitter.emit(operation)) self.instance_wrappers.append(SubstituteTemplate(emitter.instance_template(), { 'configuration_name': self.configuration_name, 'operation_name': operation.procedural_name(), 'gemm_kind': self.gemm_kind_wrappers[operation.gemm_kind], 'compile_guard_start': SubstituteTemplate(self.wmma_guard_start, {'sm_number': str(operation.arch)}) \ if operation.tile_description.math_instruction.opcode_class == OpcodeClass.WmmaTensorOp else "", 'compile_guard_end': "#endif" \ if operation.tile_description.math_instruction.opcode_class == OpcodeClass.WmmaTensorOp else "" })) def __exit__(self, exception_type, exception_value, traceback): # Write includes for incl, _ in self.includes.items(): include_statement = "#include \"%s\"\n" % incl self.configuration_file.write(include_statement) self.configuration_file.write(self.separator) # Write instance definitions in top-level namespace for instance_definition in self.instance_definitions: self.configuration_file.write(instance_definition) # Add wrapper objects within initialize() function self.configuration_file.write(SubstituteTemplate(self.initialize_function_template, { 'configuration_name': self.configuration_name })) for instance_wrapper in self.instance_wrappers: self.configuration_file.write(instance_wrapper) self.configuration_file.write(self.epilogue_template) self.configuration_file.close() ################################################################################################### ###################################################################################################
# # \file generator.py # # \brief Generates the CUTLASS Library's instances # import re ################################################################################################### import enum # The following block implements enum.auto() for Python 3.5 variants that don't include it such # as the default 3.5.2 on Ubuntu 16.04. # # https://codereview.stackexchange.com/questions/177309/reimplementing-pythons-enum-auto-for-compatibility try: from enum import auto as enum_auto except ImportError: __cutlass_library_auto_enum = 0 def enum_auto() -> int: global __cutlass_library_auto_enum i = __cutlass_library_auto_enum __cutlass_library_auto_enum += 1 return i ################################################################################################### # class GeneratorTarget(enum.Enum): Library = enum_auto() # GeneratorTargetNames = { GeneratorTarget.Library: 'library' } # ################################################################################################### # class DataType(enum.Enum): b1 = enum_auto() u4 = enum_auto() u8 = enum_auto() u16 = enum_auto() u32 = enum_auto() u64 = enum_auto() s4 = enum_auto() s8 = enum_auto() s16 = enum_auto() s32 = enum_auto() s64 = enum_auto() e4m3 = enum_auto() e5m2 = enum_auto() f16 = enum_auto() bf16 = enum_auto() f32 = enum_auto() tf32 = enum_auto() f64 = enum_auto() cf16 = enum_auto() cbf16 = enum_auto() cf32 = enum_auto() ctf32 = enum_auto() cf64 = enum_auto() cs4 = enum_auto() cs8 = enum_auto() cs16 = enum_auto() cs32 = enum_auto() cs64 = enum_auto() cu4 = enum_auto() cu8 = enum_auto() cu16 = enum_auto() cu32 = enum_auto() cu64 = enum_auto() invalid = enum_auto() # ShortDataTypeNames = { DataType.s32: 'i', DataType.e4m3: 'e4m3', DataType.e5m2: 'e5m2', DataType.f16: 'h', DataType.f32: 's', DataType.f64: 'd', DataType.cf32: 'c', DataType.cf64: 'z', } # DataTypeNames = { DataType.b1: "b1", DataType.u4: "u4", DataType.u8: "u8", DataType.u16: "u16", DataType.u32: "u32", DataType.u64: "u64", DataType.s4: "s4", DataType.s8: "s8", DataType.s16: "s16", DataType.s32: "s32", DataType.s64: "s64", DataType.e4m3: 'e4m3', DataType.e5m2: 'e5m2', DataType.f16: "f16", DataType.bf16: "bf16", DataType.f32: "f32", DataType.tf32: "tf32", DataType.f64: "f64", DataType.cf16: "cf16", DataType.cbf16: "cbf16", DataType.cf32: "cf32", DataType.ctf32: "ctf32", DataType.cf64: "cf64", DataType.cu4: "cu4", DataType.cu8: "cu8", DataType.cu16: "cu16", DataType.cu32: "cu32", DataType.cu64: "cu64", DataType.cs4: "cs4", DataType.cs8: "cs8", DataType.cs16: "cs16", DataType.cs32: "cs32", DataType.cs64: "cs64", } DataTypeTag = { DataType.b1: "cutlass::uint1b_t", DataType.u4: "cutlass::uint4b_t", DataType.u8: "uint8_t", DataType.u16: "uint16_t", DataType.u32: "uint32_t", DataType.u64: "uint64_t", DataType.s4: "cutlass::int4b_t", DataType.s8: "int8_t", DataType.s16: "int16_t", DataType.s32: "int32_t", DataType.s64: "int64_t", DataType.e4m3: 'cutlass::float_e4m3_t', DataType.e5m2: 'cutlass::float_e5m2_t', DataType.f16: "cutlass::half_t", DataType.bf16: "cutlass::bfloat16_t", DataType.f32: "float", DataType.tf32: "cutlass::tfloat32_t", DataType.f64: "double", DataType.cf16: "cutlass::complex<cutlass::half_t>", DataType.cbf16: "cutlass::complex<cutlass::bfloat16_t>", DataType.cf32: "cutlass::complex<float>", DataType.ctf32: "cutlass::complex<cutlass::tfloat32_t>", DataType.cf64: "cutlass::complex<double>", DataType.cu4: "cutlass::complex<cutlass::uint4b_t>", DataType.cu8: "cutlass::complex<cutlass::uint8_t>", DataType.cu16: "cutlass::complex<cutlass::uint16_t>", DataType.cu32: "cutlass::complex<cutlass::uint32_t>", DataType.cu64: "cutlass::complex<cutlass::uint64_t>", DataType.cs4: "cutlass::complex<cutlass::int4b_t>", DataType.cs8: "cutlass::complex<cutlass::int8_t>", DataType.cs16: "cutlass::complex<cutlass::int16_t>", DataType.cs32: "cutlass::complex<cutlass::int32_t>", DataType.cs64: "cutlass::complex<cutlass::int64_t>", } DataTypeSize = { DataType.b1: 1, DataType.u4: 4, DataType.u8: 8, DataType.u16: 16, DataType.u32: 32, DataType.u64: 64, DataType.s4: 4, DataType.s8: 8, DataType.s16: 16, DataType.s32: 32, DataType.s64: 64, DataType.e4m3: 8, DataType.e5m2: 8, DataType.f16: 16, DataType.bf16: 16, DataType.f32: 32, DataType.tf32: 32, DataType.f64: 64, DataType.cf16: 32, DataType.cbf16: 32, DataType.cf32: 64, DataType.ctf32: 32, DataType.cf64: 128, DataType.cu4: 8, DataType.cu8: 16, DataType.cu16: 32, DataType.cu32: 64, DataType.cu64: 128, DataType.cs4: 8, DataType.cs8: 16, DataType.cs16: 32, DataType.cs32: 64, DataType.cs64: 128, } ################################################################################################### # class BlasMode(enum.Enum): symmetric = enum_auto() hermitian = enum_auto() # BlasModeTag = { BlasMode.symmetric: 'cutlass::BlasMode::kSymmetric', BlasMode.hermitian: 'cutlass::BlasMode::kHermitian', } # class ComplexTransform(enum.Enum): none = enum_auto() conj = enum_auto() # ComplexTransformTag = { ComplexTransform.none: 'cutlass::ComplexTransform::kNone', ComplexTransform.conj: 'cutlass::ComplexTransform::kConjugate', } # RealComplexBijection = [ (DataType.f16, DataType.cf16), (DataType.f32, DataType.cf32), (DataType.f64, DataType.cf64), ] # def is_complex(data_type): for r, c in RealComplexBijection: if data_type == c: return True return False # def get_complex_from_real(real_type): for r, c in RealComplexBijection: if real_type == r: return c return DataType.invalid # def get_real_from_complex(complex_type): for r, c in RealComplexBijection: if complex_type == c: return r return DataType.invalid # class ComplexMultiplyOp(enum.Enum): multiply_add = enum_auto() gaussian = enum_auto() ################################################################################################### # class MathOperation(enum.Enum): multiply_add = enum_auto() multiply_add_saturate = enum_auto() xor_popc = enum_auto() multiply_add_fast_bf16 = enum_auto() multiply_add_fast_f16 = enum_auto() multiply_add_fast_f32 = enum_auto() multiply_add_complex_fast_f32 = enum_auto() multiply_add_complex = enum_auto() multiply_add_complex_gaussian = enum_auto() # MathOperationTag = { MathOperation.multiply_add: 'cutlass::arch::OpMultiplyAdd', MathOperation.multiply_add_saturate: 'cutlass::arch::OpMultiplyAddSaturate', MathOperation.xor_popc: 'cutlass::arch::OpXorPopc', MathOperation.multiply_add_fast_bf16: 'cutlass::arch::OpMultiplyAddFastBF16', MathOperation.multiply_add_fast_f16: 'cutlass::arch::OpMultiplyAddFastF16', MathOperation.multiply_add_fast_f32: 'cutlass::arch::OpMultiplyAddFastF32', MathOperation.multiply_add_complex_fast_f32: 'cutlass::arch::OpMultiplyAddComplexFastF32', MathOperation.multiply_add_complex: 'cutlass::arch::OpMultiplyAddComplex', MathOperation.multiply_add_complex_gaussian: 'cutlass::arch::OpMultiplyAddGaussianComplex', } ################################################################################################### # class LayoutType(enum.Enum): ColumnMajor = enum_auto() RowMajor = enum_auto() ColumnMajorInterleaved2 = enum_auto() RowMajorInterleaved2 = enum_auto() ColumnMajorInterleaved32 = enum_auto() RowMajorInterleaved32 = enum_auto() ColumnMajorInterleaved64 = enum_auto() RowMajorInterleaved64 = enum_auto() TensorNHWC = enum_auto() TensorNDHWC = enum_auto() TensorNCHW = enum_auto() TensorNGHWC = enum_auto() TensorNC32HW32 = enum_auto() TensorNC64HW64 = enum_auto() TensorC32RSK32 = enum_auto() TensorC64RSK64 = enum_auto() # LayoutTag = { LayoutType.ColumnMajor: 'cutlass::layout::ColumnMajor', LayoutType.RowMajor: 'cutlass::layout::RowMajor', LayoutType.ColumnMajorInterleaved2: 'cutlass::layout::ColumnMajorInterleaved<2>', LayoutType.RowMajorInterleaved2: 'cutlass::layout::RowMajorInterleaved<2>', LayoutType.ColumnMajorInterleaved32: 'cutlass::layout::ColumnMajorInterleaved<32>', LayoutType.RowMajorInterleaved32: 'cutlass::layout::RowMajorInterleaved<32>', LayoutType.ColumnMajorInterleaved64: 'cutlass::layout::ColumnMajorInterleaved<64>', LayoutType.RowMajorInterleaved64: 'cutlass::layout::RowMajorInterleaved<64>', LayoutType.TensorNHWC: 'cutlass::layout::TensorNHWC', LayoutType.TensorNDHWC: 'cutlass::layout::TensorNDHWC', LayoutType.TensorNCHW: 'cutlass::layout::TensorNCHW', LayoutType.TensorNGHWC: 'cutlass::layout::TensorNGHWC', LayoutType.TensorNC32HW32: 'cutlass::layout::TensorNCxHWx<32>', LayoutType.TensorC32RSK32: 'cutlass::layout::TensorCxRSKx<32>', LayoutType.TensorNC64HW64: 'cutlass::layout::TensorNCxHWx<64>', LayoutType.TensorC64RSK64: 'cutlass::layout::TensorCxRSKx<64>', } # TransposedLayout = { LayoutType.ColumnMajor: LayoutType.RowMajor, LayoutType.RowMajor: LayoutType.ColumnMajor, LayoutType.ColumnMajorInterleaved2: LayoutType.RowMajorInterleaved2, LayoutType.RowMajorInterleaved2: LayoutType.ColumnMajorInterleaved2, LayoutType.ColumnMajorInterleaved32: LayoutType.RowMajorInterleaved32, LayoutType.RowMajorInterleaved32: LayoutType.ColumnMajorInterleaved32, LayoutType.ColumnMajorInterleaved64: LayoutType.RowMajorInterleaved64, LayoutType.RowMajorInterleaved64: LayoutType.ColumnMajorInterleaved64, LayoutType.TensorNHWC: LayoutType.TensorNHWC } # ShortLayoutTypeNames = { LayoutType.ColumnMajor: 'n', LayoutType.ColumnMajorInterleaved2: 'n2', LayoutType.ColumnMajorInterleaved32: 'n32', LayoutType.ColumnMajorInterleaved64: 'n64', LayoutType.RowMajor: 't', LayoutType.RowMajorInterleaved2: 't2', LayoutType.RowMajorInterleaved32: 't32', LayoutType.RowMajorInterleaved64: 't64', LayoutType.TensorNHWC: 'nhwc', LayoutType.TensorNDHWC: 'ndhwc', LayoutType.TensorNCHW: 'nchw', LayoutType.TensorNGHWC: 'nghwc', LayoutType.TensorNC32HW32: 'nc32hw32', LayoutType.TensorNC64HW64: 'nc64hw64', LayoutType.TensorC32RSK32: 'c32rsk32', LayoutType.TensorC64RSK64: 'c64rsk64' } # ShortComplexLayoutNames = { (LayoutType.ColumnMajor, ComplexTransform.none): 'n', (LayoutType.ColumnMajor, ComplexTransform.conj): 'c', (LayoutType.RowMajor, ComplexTransform.none): 't', (LayoutType.RowMajor, ComplexTransform.conj): 'h' } ################################################################################################### # class SideMode(enum.Enum): Left = enum_auto() Right = enum_auto() # SideModeTag = { SideMode.Left: 'cutlass::SideMode::kLeft', SideMode.Right: 'cutlass::SideMode::kRight' } # ShortSideModeNames = { SideMode.Left: 'ls', SideMode.Right: 'rs' } ################################################################################################### # class FillMode(enum.Enum): Lower = enum_auto() Upper = enum_auto() # FillModeTag = { FillMode.Lower: 'cutlass::FillMode::kLower', FillMode.Upper: 'cutlass::FillMode::kUpper' } # ShortFillModeNames = { FillMode.Lower: 'l', FillMode.Upper: 'u' } ################################################################################################### # class DiagType(enum.Enum): NonUnit = enum_auto() Unit = enum_auto() # DiagTypeTag = { DiagType.NonUnit: 'cutlass::DiagType::kNonUnit', DiagType.Unit: 'cutlass::DiagType::kUnit' } # ShortDiagTypeNames = { DiagType.NonUnit: 'nu', DiagType.Unit: 'un' } ################################################################################################### # class OpcodeClass(enum.Enum): Simt = enum_auto() TensorOp = enum_auto() WmmaTensorOp = enum_auto() SparseTensorOp = enum_auto() OpcodeClassNames = { OpcodeClass.Simt: 'simt', OpcodeClass.TensorOp: 'tensorop', OpcodeClass.WmmaTensorOp: 'wmma_tensorop', } OpcodeClassTag = { OpcodeClass.Simt: 'cutlass::arch::OpClassSimt', OpcodeClass.TensorOp: 'cutlass::arch::OpClassTensorOp', OpcodeClass.WmmaTensorOp: 'cutlass::arch::OpClassWmmaTensorOp', } ################################################################################################### # class OperationKind(enum.Enum): Gemm = enum_auto() RankK = enum_auto() Rank2K = enum_auto() Trmm = enum_auto() Symm = enum_auto() Conv2d = enum_auto() Conv3d = enum_auto() # OperationKindNames = { OperationKind.Gemm: 'gemm' , OperationKind.RankK: 'rank_k' , OperationKind.Rank2K: 'rank_2k' , OperationKind.Trmm: 'trmm' , OperationKind.Symm: 'symm' , OperationKind.Conv2d: 'conv2d' , OperationKind.Conv3d: 'conv3d' } # class Target(enum.Enum): library = enum_auto() # ArchitectureNames = { 50: 'maxwell', 60: 'pascal', 61: 'pascal', 70: 'volta', 75: 'turing', 80: 'ampere', 89: 'ada', 90: 'hopper' } # SharedMemPerCC = { 70: 96, # 96KB of SMEM 72: 96, # 96KB of SMEM 75: 64, # 64KB of SMEM 80: 163, # 163KB of SMEM - 1KB reserved for the driver 86: 99, # 99KB of SMEM - 1KB reserved for the driver 87: 163, # 163KB of SMEM - 1KB reserved for the driver 89: 99, # 99KB of SMEM - 1KB reserved for the driver 90: 227, # 227KB of SMEM - 1KB reserved for the driver } ################################################################################################### # def SubstituteTemplate(template, values): text = template changed = True while changed: changed = False for key, value in values.items(): regex = "\\$\\{%s\\}" % key newtext = re.sub(regex, value, text) if newtext != text: changed = True text = newtext return text ################################################################################################### # class GemmKind(enum.Enum): Gemm = enum_auto() Sparse = enum_auto() Universal = enum_auto() Universal3x = enum_auto() PlanarComplex = enum_auto() PlanarComplexArray = enum_auto() Grouped = enum_auto() # GemmKindNames = { GemmKind.Gemm: "gemm", GemmKind.Sparse: "spgemm", GemmKind.Universal: "gemm", GemmKind.Universal3x: "gemm", GemmKind.PlanarComplex: "gemm_planar_complex", GemmKind.PlanarComplexArray: "gemm_planar_complex_array", GemmKind.Grouped: "gemm_grouped" } # class RankKKind(enum.Enum): Universal = enum_auto() # RankKKindNames = { RankKKind.Universal: "rank_k" } # class TrmmKind(enum.Enum): Universal = enum_auto() # TrmmKindNames = { TrmmKind.Universal: "trmm" } # class SymmKind(enum.Enum): Universal = enum_auto() # SymmKindNames = { SymmKind.Universal: "symm" } # class EpilogueFunctor(enum.Enum): LinearCombination = enum_auto() LinearCombinationClamp = enum_auto() # EpilogueFunctorTag = { EpilogueFunctor.LinearCombination: 'cutlass::epilogue::thread::LinearCombination', EpilogueFunctor.LinearCombinationClamp: 'cutlass::epilogue::thread::LinearCombinationClamp', } # class SwizzlingFunctor(enum.Enum): Identity1 = enum_auto() Identity2 = enum_auto() Identity4 = enum_auto() Identity8 = enum_auto() Horizontal = enum_auto() StridedDgradIdentity1 = enum_auto() StridedDgradIdentity4 = enum_auto() StridedDgradHorizontal = enum_auto() StreamK = enum_auto() # SwizzlingFunctorTag = { SwizzlingFunctor.Identity1: 'cutlass::gemm::threadblock::GemmIdentityThreadblockSwizzle<1>', SwizzlingFunctor.Identity2: 'cutlass::gemm::threadblock::GemmIdentityThreadblockSwizzle<2>', SwizzlingFunctor.Identity4: 'cutlass::gemm::threadblock::GemmIdentityThreadblockSwizzle<4>', SwizzlingFunctor.Identity8: 'cutlass::gemm::threadblock::GemmIdentityThreadblockSwizzle<8>', SwizzlingFunctor.Horizontal: 'cutlass::gemm::threadblock::GemmHorizontalThreadblockSwizzle', SwizzlingFunctor.StridedDgradIdentity1: 'cutlass::conv::threadblock::StridedDgradIdentityThreadblockSwizzle<1>', SwizzlingFunctor.StridedDgradIdentity4: 'cutlass::conv::threadblock::StridedDgradIdentityThreadblockSwizzle<4>', SwizzlingFunctor.StridedDgradHorizontal: 'cutlass::conv::threadblock::StridedDgradHorizontalThreadblockSwizzle', SwizzlingFunctor.StreamK: 'cutlass::gemm::threadblock::ThreadblockSwizzleStreamK', } # class GroupScheduleMode(enum.Enum): Device = enum_auto(), Host = enum_auto() # GroupScheduleModeTag = { GroupScheduleMode.Device: 'cutlass::gemm::kernel::GroupScheduleMode::kDeviceOnly', GroupScheduleMode.Host: 'cutlass::gemm::kernel::GroupScheduleMode::kHostPrecompute' } # ShortGroupScheduleModeNames = { GroupScheduleMode.Device: 'Device', GroupScheduleMode.Host: 'Host' } ################################################################################################### # class ConvKind(enum.Enum): Fprop = enum_auto() Dgrad = enum_auto() Wgrad = enum_auto() # ConvKindTag = { ConvKind.Fprop: 'cutlass::conv::Operator::kFprop', ConvKind.Dgrad: 'cutlass::conv::Operator::kDgrad', ConvKind.Wgrad: 'cutlass::conv::Operator::kWgrad' } ConvKindNames = { ConvKind.Fprop: 'fprop', ConvKind.Dgrad: 'dgrad', ConvKind.Wgrad: 'wgrad', } # class IteratorAlgorithm(enum.Enum): Analytic = enum_auto() Optimized = enum_auto() FixedChannels = enum_auto() FewChannels = enum_auto() FixedStrideDilation = enum_auto() # IteratorAlgorithmTag = { IteratorAlgorithm.Analytic: 'cutlass::conv::IteratorAlgorithm::kAnalytic', IteratorAlgorithm.Optimized: 'cutlass::conv::IteratorAlgorithm::kOptimized', IteratorAlgorithm.FixedChannels: 'cutlass::conv::IteratorAlgorithm::kFixedChannels', IteratorAlgorithm.FewChannels: 'cutlass::conv::IteratorAlgorithm::kFewChannels', IteratorAlgorithm.FixedStrideDilation: 'cutlass::conv::IteratorAlgorithm::kFixedStrideDilation' } IteratorAlgorithmNames = { IteratorAlgorithm.Analytic: 'analytic', IteratorAlgorithm.Optimized: 'optimized', IteratorAlgorithm.FixedChannels: 'fixed_channels', IteratorAlgorithm.FewChannels: 'few_channels', IteratorAlgorithm.FixedStrideDilation: 'fixed_stride_dilation' } # class StrideSupport(enum.Enum): Strided = enum_auto() Unity = enum_auto() Fixed = enum_auto() # StrideSupportTag = { StrideSupport.Strided: 'cutlass::conv::StrideSupport::kStrided', StrideSupport.Unity: 'cutlass::conv::StrideSupport::kUnity', StrideSupport.Fixed: 'cutlass::conv::StrideSupport::kFixed' } StrideSupportNames = { StrideSupport.Strided: '', StrideSupport.Unity: 'unity_stride', StrideSupport.Fixed: 'fixed_stride' } # class GroupMode(enum.Enum): NoneGroup = enum_auto() # dense conv (G=1) SingleGroup = enum_auto() # grouped convolution (single group per CTA) MultipleGroup = enum_auto() # grouped convolution ( multiple groups per CTA) Depthwise = enum_auto() # Depthwise convolution ( C=K=G ) # GroupModeTag = { GroupMode.NoneGroup: 'cutlass::conv::GroupMode::kNone', GroupMode.SingleGroup: 'cutlass::conv::GroupMode::kSingleGroup', GroupMode.MultipleGroup: 'cutlass::conv::GroupMode::kMultipleGroup', GroupMode.Depthwise: 'cutlass::conv::GroupMode::kDepthwise', } GroupModeNames = { GroupMode.NoneGroup: '', GroupMode.SingleGroup: 'single_group', GroupMode.MultipleGroup: 'multiple_group', GroupMode.Depthwise: 'depthwise', } ################################################################################################### # class MathInstruction: def __init__(self, instruction_shape, element_a, element_b, element_accumulator, opcode_class, math_operation = MathOperation.multiply_add): self.instruction_shape = instruction_shape self.element_a = element_a self.element_b = element_b self.element_accumulator = element_accumulator self.opcode_class = opcode_class self.math_operation = math_operation # class TileDescription: def __init__(self, threadblock_shape, stages, warp_count, math_instruction, min_compute, max_compute, cluster_shape = [1,1,1]): self.threadblock_shape = threadblock_shape self.stages = stages self.warp_count = warp_count self.math_instruction = math_instruction self.minimum_compute_capability = min_compute self.maximum_compute_capability = max_compute self.cluster_shape = cluster_shape def procedural_name(self): if self.minimum_compute_capability >= 90: return "{tbm}x{tbn}x{tbk}_{cm}x{cn}x{ck}_{s}".format( tbm = self.threadblock_shape[0], tbn = self.threadblock_shape[1], tbk = self.threadblock_shape[2], cm = self.cluster_shape[0], cn = self.cluster_shape[1], ck = self.cluster_shape[2], s = self.stages) else: return "%dx%d_%dx%d" % (self.threadblock_shape[0], self.threadblock_shape[1], self.threadblock_shape[2], self.stages) # class Direct2dConvFixedStrideDilationTileDescription: def __init__(self, threadblock_output_shape, filter_shape, stages, stride, dilation, warp_count, math_instruction, min_compute, max_compute): self.threadblock_shape = [threadblock_output_shape[0]*threadblock_output_shape[1]*threadblock_output_shape[2], threadblock_output_shape[3], filter_shape[0]*filter_shape[1]] self.threadblock_output_shape = threadblock_output_shape self.filter_shape = filter_shape self.stages = stages self.warp_count = warp_count self.stride = stride self.dilation = dilation self.math_instruction = math_instruction self.minimum_compute_capability = min_compute self.maximum_compute_capability = max_compute def procedural_name(self): str_name = "%dx%dx%d_%dx%dx%dx%d_%d_filter%dx%d" % (self.threadblock_shape[0], self.threadblock_shape[1], self.threadblock_shape[2], self.threadblock_output_shape[0], self.threadblock_output_shape[1], self.threadblock_output_shape[2], self.threadblock_output_shape[3], self.stages, self.filter_shape[0], self.filter_shape[1]) # Fixed Strided and dilation if self.stride != [-1, -1] and self.dilation != [-1, -1]: str_name += "_stride%dx%d_dilation%dx%d" % (self.stride[0], self.stride[1], self.dilation[0], self.dilation[1]) return str_name # class Direct2dConvFixedStrideDilationTileDescription: def __init__(self, threadblock_output_shape, filter_shape, stages, stride, dilation, warp_count, math_instruction, min_compute, max_compute): self.threadblock_shape = [threadblock_output_shape[0]*threadblock_output_shape[1]*threadblock_output_shape[2], threadblock_output_shape[3], filter_shape[0]*filter_shape[1]] self.threadblock_output_shape = threadblock_output_shape self.filter_shape = filter_shape self.stages = stages self.warp_count = warp_count self.stride = stride self.dilation = dilation self.math_instruction = math_instruction self.minimum_compute_capability = min_compute self.maximum_compute_capability = max_compute def procedural_name(self): str_name = "%dx%dx%d_%dx%dx%dx%d_%d_filter%dx%d" % (self.threadblock_shape[0], self.threadblock_shape[1], self.threadblock_shape[2], self.threadblock_output_shape[0], self.threadblock_output_shape[1], self.threadblock_output_shape[2], self.threadblock_output_shape[3], self.stages, self.filter_shape[0], self.filter_shape[1]) # Fixed Strided and dilation if self.stride != [-1, -1] and self.dilation != [-1, -1]: str_name += "_stride%dx%d_dilation%dx%d" % (self.stride[0], self.stride[1], self.dilation[0], self.dilation[1]) return str_name # class TensorDescription: def __init__(self, element, layout, alignment = 1, complex_transform = ComplexTransform.none): self.element = element self.layout = layout self.alignment = alignment self.complex_transform = complex_transform # class SymmetricTensorDescription: def __init__(self, element, layout, fill_mode, alignment = 1, complex_transform = ComplexTransform.none, side_mode = SideMode.Left): self.element = element self.layout = layout self.fill_mode = fill_mode self.alignment = alignment self.complex_transform = complex_transform self.side_mode = side_mode # class TriangularTensorDescription: def __init__(self, element, layout, side_mode, fill_mode, diag_type, alignment = 1, complex_transform = ComplexTransform.none): self.element = element self.layout = layout self.side_mode = side_mode self.fill_mode = fill_mode self.diag_type = diag_type self.alignment = alignment self.complex_transform = complex_transform ################################################################################################### # def CalculateSmemUsage(operation): cta_shape = operation.tile_description.threadblock_shape stages = operation.tile_description.stages if operation.operation_kind == OperationKind.Gemm and operation.gemm_kind == GemmKind.Sparse: # Elements represented by 8 bits of metadata (based on 4:8, 2:4 or 1:2 sparsity) if DataTypeSize[operation.A.element] == 32: elements_per_8b_md = 2 elif DataTypeSize[operation.A.element] == 4: elements_per_8b_md = 8 else: elements_per_8b_md = 4 smem_per_stage = DataTypeSize[operation.A.element] * cta_shape[0] * (cta_shape[2] // 2) // 8 + \ DataTypeSize[operation.B.element] * cta_shape[1] * cta_shape[2] // 8 + \ cta_shape[0] * (cta_shape[2] // 2) // elements_per_8b_md else: # Few BLAS3 operations only have A tensor smem_per_stage = DataTypeSize[operation.A.element] * cta_shape[0] * cta_shape[2] // 8 + \ DataTypeSize[operation.A.element] * cta_shape[1] * cta_shape[2] // 8 smem_usage = smem_per_stage * stages return (smem_usage >> 10) ###################################################################################################
# # \file generator.py # # \brief Generates the CUTLASS Library's instances # import enum import os.path import shutil from library import * from gemm_operation import * from rank_k_operation import * from rank_2k_operation import * from trmm_operation import * from symm_operation import * from conv2d_operation import * from conv3d_operation import * import logging ################################################################################################### _LOGGER = logging.getLogger(__name__) class EmitOperationKindLibrary: def __init__(self, generated_path, kind, args): self.generated_path = generated_path self.kind = kind self.args = args self.emitters = { OperationKind.Gemm: EmitGemmConfigurationLibrary , OperationKind.Conv2d: EmitConv2dConfigurationLibrary , OperationKind.Conv3d: EmitConv3dConfigurationLibrary , OperationKind.RankK: EmitRankKConfigurationLibrary , OperationKind.Rank2K: EmitRank2KConfigurationLibrary , OperationKind.Trmm: EmitTrmmConfigurationLibrary , OperationKind.Symm: EmitSymmConfigurationLibrary } self.configurations = []; self.header_template =""" /* Generated by manifest.py - Do not edit. */ #include "cutlass/cutlass.h" #include "cutlass/library/library.h" #include "cutlass/library/manifest.h" namespace cutlass { namespace library { /////////////////////////////////////////////////////////////////////////////////////////////////// """ self.entry_template = """ // // Entry point to construct operations // void initialize_all_${operation_name}_operations(Manifest &manifest) { """ self.configuration_prototype_template = "void initialize_${configuration_name}(Manifest &manifest);\n" self.configuration_template =" initialize_${configuration_name}(manifest);\n" self.epilogue_template =""" } /////////////////////////////////////////////////////////////////////////////////////////////////// } // namespace library } // namespace cutlass """ # def __enter__(self): self.operation_path = os.path.join(self.generated_path, OperationKindNames[self.kind]) os.mkdir(self.operation_path) self.top_level_path = os.path.join(self.operation_path, "all_%s_operations.cu" % OperationKindNames[self.kind]) self.top_level_file = open(self.top_level_path, "w") self.top_level_file.write(self.header_template) self.source_files = [self.top_level_path,] return self # def emit(self, configuration_name, operations): with self.emitters[self.kind](self.operation_path, configuration_name) as configuration_emitter: for operation in operations: configuration_emitter.emit(operation) self.source_files.append(configuration_emitter.configuration_path) self.configurations.append(configuration_name) self.top_level_file.write(SubstituteTemplate(self.configuration_prototype_template, {'configuration_name': configuration_name} )) # def __exit__(self, exception_type, exception_value, traceback): self.top_level_file.write(SubstituteTemplate(self.entry_template, {'operation_name': OperationKindNames[self.kind]})) for configuration_name in self.configurations: self.top_level_file.write(SubstituteTemplate(self.configuration_template, {'configuration_name': configuration_name})) self.top_level_file.write(self.epilogue_template) self.top_level_file.close() class EmitInterfaceLibrary: def __init__(self, generated_path, operation_count, args): self.generated_path = generated_path self.args = args self.prototypes = [] self.fn_calls = [] self.operation_count = str(operation_count) self.top_level_hdr_template = ''' /* Generated by manifest.py - Do not edit. */ ''' self.top_level_prologue = ''' #include "cutlass/library/library.h" #include "cutlass/library/manifest.h" namespace cutlass { \tnamespace library { ${prototypes} \t\tvoid initialize_all(Manifest &manifest) { \t\t\tmanifest.reserve(${operation_count});\n\n ${fn_calls} \t\t\t} \t} // namespace library } // namespace cutlass ''' # def __enter__(self): self.top_level_path = os.path.join(self.generated_path, 'initialize_all.cpp') self.top_level_file = open(self.top_level_path, "w") self.top_level_file.write(self.top_level_hdr_template) self.source_files = [self.top_level_path,] return self # def emit(self, operation_name): self.prototypes.append(SubstituteTemplate( "\t\tvoid initialize_all_${operation_kind}_operations(Manifest &manifest);", {'operation_kind': operation_name})) self.fn_calls.append(SubstituteTemplate( "\t\t\tinitialize_all_${operation_kind}_operations(manifest);", {'operation_kind': operation_name})) # def __exit__(self, exception_type, exception_value, traceback): self.top_level_file.write(SubstituteTemplate(self.top_level_prologue, {'prototypes':"\n".join(self.prototypes), 'fn_calls':"\n".join(self.fn_calls), 'operation_count': self.operation_count})) self.top_level_file.close() ################################################################################################### ################################################################################################### class Options: def __init__(self): pass ################################################################################################### # class Manifest: # def __init__(self, args = None): self.operations = {} self.args = args self.operation_count = 0 self.operations_by_name = {} self.kernel_filter = '' self.kernel_filter_list = [] self.kernel_names = [] self.operations_enabled = [] self.selected_kernels = [] self.ignore_kernel_names = [] self.compute_capabilities = [50,] self.curr_build_dir = '.' self.filter_by_cc = True if self.args: self.kernel_filter = self.args.kernels self.curr_build_dir = args.curr_build_dir architectures = args.architectures.split(';') if len(args.architectures) else ['50',] architectures = [x if x != '90a' else '90' for x in architectures] self.compute_capabilities = [int(x) for x in architectures] if args.filter_by_cc in ['false', 'False', '0']: self.filter_by_cc = False if args.operations == 'all': self.operations_enabled = [] else: operations_list = [ OperationKind.Gemm , OperationKind.Conv2d , OperationKind.Conv3d , OperationKind.RankK , OperationKind.Trmm , OperationKind.Symm ] self.operations_enabled = [x for x in operations_list if OperationKindNames[x] in args.operations.split(',')] if args.kernels == 'all': self.kernel_names = [] else: self.kernel_names = [x for x in args.kernels.split(',') if x != ''] self.ignore_kernel_names = [x for x in args.ignore_kernels.split(',') if x != ''] if args.kernel_filter_file is None: self.kernel_filter_list = [] else: self.kernel_filter_list = self.get_kernel_filters(args.kernel_filter_file) self.operation_count = 0 self.operations_by_name = {} self.disable_full_archs_compilation = args.disable_full_archs_compilation def get_kernel_filters (self, kernelListFile): if os.path.isfile(kernelListFile): with open(kernelListFile, 'r') as fileReader: lines = [line.rstrip() for line in fileReader if not line.startswith("#")] lines = [re.compile(line) for line in lines if line] return lines else: return [] # def filter_out_kernels(self, kernel_name, kernel_filter_list): for kernel_filter_re in kernel_filter_list: if kernel_filter_re.search(kernel_name) is not None: return True return False # def _filter_string_matches(self, filter_string, haystack): ''' Returns true if all substrings appear in the haystack in order''' substrings = filter_string.split('*') for sub in substrings: idx = haystack.find(sub) if idx < 0: return False haystack = haystack[idx + len(sub):] return True # def filter(self, operation): ''' Filtering operations based on various criteria''' # filter based on compute capability enabled = not (self.filter_by_cc) for cc in self.compute_capabilities: if cc >= operation.tile_description.minimum_compute_capability and \ cc <= operation.tile_description.maximum_compute_capability and \ (cc not in SharedMemPerCC or SharedMemPerCC[cc] >= CalculateSmemUsage(operation)): enabled = True break if not enabled: return False if len(self.operations_enabled) and not operation.operation_kind in self.operations_enabled: return False # eliminate duplicates if operation.procedural_name() in self.operations_by_name.keys(): return False # Filter based on list of valid substrings if len(self.kernel_names): name = operation.procedural_name() enabled = False # compare against the include list for name_substr in self.kernel_names: if self._filter_string_matches(name_substr, name): enabled = True break # compare against the exclude list for name_substr in self.ignore_kernel_names: if self._filter_string_matches(name_substr, name): enabled = False break if len(self.kernel_filter_list) > 0: enabled = False if self.filter_out_kernels(operation.procedural_name(), self.kernel_filter_list): enabled = True # todo: filter based on compute data type return enabled # # def append(self, operation): ''' Inserts the operation. operation_kind -> configuration_name -> [] ''' if self.filter(operation): self.selected_kernels.append(operation.procedural_name()) self.operations_by_name[operation.procedural_name()] = operation # add the configuration configuration_name = operation.configuration_name() if operation.operation_kind not in self.operations.keys(): self.operations[operation.operation_kind] = {} if configuration_name not in self.operations[operation.operation_kind].keys(): self.operations[operation.operation_kind][configuration_name] = [] self.operations[operation.operation_kind][configuration_name].append(operation) self.operation_count += 1 else: _LOGGER.debug("Culled {} from manifest".format(operation.procedural_name())) # # def emit(self, target = GeneratorTarget.Library): operation_emitters = { GeneratorTarget.Library: EmitOperationKindLibrary } interface_emitters = { GeneratorTarget.Library: EmitInterfaceLibrary } generated_path = os.path.join(self.curr_build_dir, 'generated') # create generated/ if os.path.exists(generated_path): shutil.rmtree(generated_path) os.mkdir(generated_path) source_files = [] with interface_emitters[target](generated_path, self.operation_count, self.args) as iface_emitter: for operation_kind, configurations in self.operations.items(): iface_emitter.emit(OperationKindNames[operation_kind]) source_files += iface_emitter.source_files # for each operation kind, emit initializer for all configurations for operation_kind, configurations in self.operations.items(): with operation_emitters[target](generated_path, operation_kind, self.args) as operation_kind_emitter: for configuration_name, operations in configurations.items(): operation_kind_emitter.emit(configuration_name, operations) source_files += operation_kind_emitter.source_files # write the manifest.cmake file containing paths from all targets manifest_path = os.path.join(generated_path, "manifest.cmake") with open(manifest_path, "w") as manifest_file: target_name = 'cutlass_library_objs' target_text = SubstituteTemplate("""cutlass_target_sources( ${target_name} BATCH_SOURCES ON PRIVATE """, { 'target_name': target_name}) manifest_file.write(target_text + '\n\n') for source_file in source_files: manifest_file.write(" %s\n" % str(source_file.replace('\\', '/'))) manifest_file.write(")\n") if self.disable_full_archs_compilation: def for_hopper(name): pass def for_ampere(name): return "16816" in name or \ "16832" in name or \ "16864" in name or \ ("1688" in name and "tf32" in name) def for_turing(name): return ("1688" in name and "tf32" not in name) or \ "8816" in name def for_volta(name): return "884" in name def is_cpp(name): return name.endswith(".cpp") def get_src_archs_str_given_requested_cuda_archs(archs, source_file): intersected_archs = archs & set(self.compute_capabilities) if intersected_archs == set(): raise RuntimeError( """ Empty archs set for file {} after taking the intersection of {} (global requested archs) and {} (per file requested archs) """.format(source_file, set(self.compute_capabilities), archs)) else: return " ".join(map(str, intersected_archs)) for source_file in source_files: if is_cpp(source_file): continue # skip because source is cpp elif for_ampere(source_file): archs_str = get_src_archs_str_given_requested_cuda_archs({80, 87, 90}, source_file) elif for_turing(source_file): archs_str = get_src_archs_str_given_requested_cuda_archs({75}, source_file) elif for_volta(source_file): archs_str = get_src_archs_str_given_requested_cuda_archs({70, 72}, source_file) else: raise RuntimeError("Per file archs are not set {}, as there is no rule specified for this file pattern".format(source_file)) manifest_file.write("cutlass_apply_cuda_gencode_flags({} SM_ARCHS {})\n".format(str(source_file.replace('\\', '/')), archs_str)) # ###################################################################################################
# # \file generator.py # # \brief Generates the CUTLASS Library's instances # # import enum import os.path import shutil import functools import operator from library import * ################################################################################################### # # Data structure modeling a Rank K update operation # ################################################################################################### # class RankKOperation: # def __init__(self, rank_k_kind, arch, tile_description, A, C, element_epilogue, \ epilogue_functor = EpilogueFunctor.LinearCombination, swizzling_functor = SwizzlingFunctor.Identity8, \ blas_mode = BlasMode.symmetric): self.blas_mode = blas_mode self.operation_kind = OperationKind.RankK self.arch = arch self.tile_description = tile_description self.rank_k_kind = rank_k_kind self.A = A self.C = C self.element_epilogue = element_epilogue self.epilogue_functor = epilogue_functor self.swizzling_functor = swizzling_functor # def is_complex(self): complex_operators = [ MathOperation.multiply_add_complex, MathOperation.multiply_add_complex_gaussian, MathOperation.multiply_add_complex_fast_f32 ] return self.tile_description.math_instruction.math_operation in complex_operators return False # def is_planar_complex(self): return False # def accumulator_type(self): accum = self.tile_description.math_instruction.element_accumulator if self.is_complex(): return get_complex_from_real(accum) return accum # def short_math_name(self): if self.tile_description.math_instruction.math_operation == MathOperation.multiply_add_complex_gaussian: return "g%s" % ShortDataTypeNames[self.accumulator_type()] return ShortDataTypeNames[self.accumulator_type()] # def core_name(self): ''' The basic operation kind is prefixed with a letter indicating the accumulation type. ''' inst_shape = '' inst_operation = '' intermediate_type = '' math_operations_map = { MathOperation.xor_popc: 'xor', } if self.tile_description.math_instruction.opcode_class == OpcodeClass.TensorOp or \ self.tile_description.math_instruction.opcode_class == OpcodeClass.WmmaTensorOp: math_op = self.tile_description.math_instruction.math_operation math_op_string = math_operations_map[math_op] if math_op in math_operations_map.keys() else '' inst_shape = "%d%d%d" % tuple(self.tile_description.math_instruction.instruction_shape) inst_shape += math_op_string if self.tile_description.math_instruction.element_a != self.A.element and \ self.tile_description.math_instruction.element_a != self.tile_description.math_instruction.element_accumulator: intermediate_type = DataTypeNames[self.tile_description.math_instruction.element_a] operation_name = 'syrk' if self.blas_mode == BlasMode.symmetric else 'herk' return "%s%s%s%s" % (self.short_math_name(), inst_shape, intermediate_type, operation_name) # def extended_name(self): ''' Append data types if they differ from compute type. ''' if self.is_complex(): extended_name = "${core_name}" else: if self.C.element != self.tile_description.math_instruction.element_accumulator and \ self.A.element != self.tile_description.math_instruction.element_accumulator: extended_name = "${element_c}_${core_name}_${element_a}" elif self.C.element == self.tile_description.math_instruction.element_accumulator and \ self.A.element != self.tile_description.math_instruction.element_accumulator: extended_name = "${core_name}_${element_a}" else: extended_name = "${core_name}" extended_name = SubstituteTemplate(extended_name, { 'element_a': DataTypeNames[self.A.element], 'element_c': DataTypeNames[self.C.element], 'core_name': self.core_name() }) return extended_name # def layout_name(self): if self.is_complex() or self.is_planar_complex(): return "%s" % ( ShortComplexLayoutNames[(self.A.layout, self.A.complex_transform)] ) return "%s" % (ShortLayoutTypeNames[self.A.layout]) # def fill_mode_name(self): return "%s" % (ShortFillModeNames[self.C.fill_mode]) # def procedural_name(self): ''' The full procedural name indicates architecture, extended name, tile size, and layout. ''' threadblock = self.tile_description.procedural_name() opcode_class_name = OpcodeClassNames[self.tile_description.math_instruction.opcode_class] alignment = max([self.A.alignment, self.C.alignment]) return SubstituteTemplate( "cutlass_${opcode_class}_${extended_name}_${threadblock}_${layout}_${fill_mode}_align${alignment}", { 'opcode_class': opcode_class_name, 'extended_name': self.extended_name(), 'threadblock': threadblock, 'layout': self.layout_name(), 'fill_mode': self.fill_mode_name(), 'alignment': "%d" % self.A.alignment, } ) # def configuration_name(self): ''' The full procedural name indicates architecture, extended name, tile size, and layout. ''' return self.procedural_name() ################################################################################################### # # Emits single instances of a CUTLASS device-wide operator # ################################################################################################### # class EmitRankKUniversalInstance: ''' Responsible for emitting a CUTLASS template definition''' def __init__(self): self.rank_k_template = """ // Rank K operator ${operation_name} using Operation_${operation_name} = typename cutlass::gemm::device::RankK< ${element_a}, ${layout_a}, ${element_c}, ${layout_c}, ${fill_mode}, ${element_accumulator}, ${opcode_class}, ${arch}, cutlass::gemm::GemmShape<${threadblock_shape_m}, ${threadblock_shape_n}, ${threadblock_shape_k}>, cutlass::gemm::GemmShape<${warp_shape_m}, ${warp_shape_n}, ${warp_shape_k}>, cutlass::gemm::GemmShape<${instruction_shape_m}, ${instruction_shape_n}, ${instruction_shape_k}>, ${epilogue_functor}< ${element_c}, ${epilogue_vector_length}, ${element_accumulator}, ${element_epilogue} >, ${swizzling_functor}, ${stages}, ${align_a}, ${split_k_serial}, ${math_operation} >; """ self.rank_k_complex_template = """ // Rank K operator ${operation_name} using Operation_${operation_name} = typename cutlass::gemm::device::RankK< ${element_a}, ${layout_a}, ${element_c}, ${layout_c}, ${fill_mode}, ${element_accumulator}, ${opcode_class}, ${arch}, cutlass::gemm::GemmShape<${threadblock_shape_m}, ${threadblock_shape_n}, ${threadblock_shape_k}>, cutlass::gemm::GemmShape<${warp_shape_m}, ${warp_shape_n}, ${warp_shape_k}>, cutlass::gemm::GemmShape<${instruction_shape_m}, ${instruction_shape_n}, ${instruction_shape_k}>, ${epilogue_functor}< ${element_c}, ${epilogue_vector_length}, ${element_accumulator}, ${element_epilogue} >, ${swizzling_functor}, ${stages}, ${align_a}, ${split_k_serial}, ${math_operation}, ${transform_a}, ${blas_mode} >; """ def emit(self, operation): threadblock_shape = operation.tile_description.threadblock_shape warp_count = operation.tile_description.warp_count warp_shape = [threadblock_shape[idx] // warp_count[idx] for idx in range(3)] epilogue_vector_length = int(min(operation.C.alignment * DataTypeSize[operation.C.element], 128) / DataTypeSize[operation.C.element]) values = { 'operation_name': operation.procedural_name(), 'element_a': DataTypeTag[operation.A.element], 'layout_a': LayoutTag[operation.A.layout], 'element_c': DataTypeTag[operation.C.element], 'layout_c': LayoutTag[operation.C.layout], 'fill_mode': FillModeTag[operation.C.fill_mode], 'element_accumulator': DataTypeTag[operation.accumulator_type()], 'opcode_class': OpcodeClassTag[operation.tile_description.math_instruction.opcode_class], 'arch': "cutlass::arch::Sm%d" % operation.arch, 'threadblock_shape_m': str(operation.tile_description.threadblock_shape[0]), 'threadblock_shape_n': str(operation.tile_description.threadblock_shape[1]), 'threadblock_shape_k': str(operation.tile_description.threadblock_shape[2]), 'warp_shape_m': str(warp_shape[0]), 'warp_shape_n': str(warp_shape[1]), 'warp_shape_k': str(warp_shape[2]), 'instruction_shape_m': str(operation.tile_description.math_instruction.instruction_shape[0]), 'instruction_shape_n': str(operation.tile_description.math_instruction.instruction_shape[1]), 'instruction_shape_k': str(operation.tile_description.math_instruction.instruction_shape[2]), 'epilogue_vector_length': str(epilogue_vector_length), 'element_epilogue': str(DataTypeTag[operation.element_epilogue]), 'epilogue_functor': EpilogueFunctorTag[operation.epilogue_functor], 'swizzling_functor': SwizzlingFunctorTag[operation.swizzling_functor], 'stages': str(operation.tile_description.stages), 'align_a': str(operation.A.alignment), 'split_k_serial': 'false', 'math_operation': MathOperationTag[operation.tile_description.math_instruction.math_operation], 'transform_a': ComplexTransformTag[operation.A.complex_transform], 'blas_mode': BlasModeTag[operation.blas_mode] } rank_k_template = self.rank_k_complex_template if operation.is_complex() else self.rank_k_template return SubstituteTemplate(rank_k_template, values) ################################################################################################### ################################################################################################### # # Emitters functions for all targets # ################################################################################################### class EmitRankKConfigurationLibrary: def __init__(self, operation_path, configuration_name): self.configuration_name = configuration_name self.configuration_path = os.path.join(operation_path, "%s.cu" % configuration_name).replace('\\', '/') self.instance_emitter = { RankKKind.Universal: EmitRankKUniversalInstance, } self.rank_k_kind_wrappers = { RankKKind.Universal: 'RankKOperation', } self.instance_template = { RankKKind.Universal: """ ${compile_guard_start} manifest.append(new ${rank_k_kind}< Operation_${operation_name} >("${operation_name}")); ${compile_guard_end} """ } self.header_template = """ /* Generated by rank_k_operation.py - Do not edit. */ /////////////////////////////////////////////////////////////////////////////////////////////////// #include "cutlass/cutlass.h" #include "cutlass/library/library.h" #include "cutlass/library/manifest.h" #include "library_internal.h" #include "rank_k_operation.h" /////////////////////////////////////////////////////////////////////////////////////////////////// """ self.initialize_function_template = """ /////////////////////////////////////////////////////////////////////////////////////////////////// namespace cutlass { namespace library { /////////////////////////////////////////////////////////////////////////////////////////////////// void initialize_${configuration_name}(Manifest &manifest) { """ self.epilogue_template = """ } /////////////////////////////////////////////////////////////////////////////////////////////////// } // namespace library } // namespace cutlass /////////////////////////////////////////////////////////////////////////////////////////////////// """ def __enter__(self): self.configuration_file = open(self.configuration_path, "w") self.configuration_file.write(self.header_template) self.instance_definitions = [] self.instance_wrappers = [] self.operations = [] return self def emit(self, operation): emitter = self.instance_emitter[operation.rank_k_kind]() self.operations.append(operation) self.instance_definitions.append(emitter.emit(operation)) self.instance_wrappers.append(SubstituteTemplate(self.instance_template[operation.rank_k_kind], { 'configuration_name': self.configuration_name, 'operation_name': operation.procedural_name(), 'rank_k_kind': self.rank_k_kind_wrappers[operation.rank_k_kind], 'compile_guard_start': SubstituteTemplate(self.wmma_guard_start, {'sm_number': str(operation.arch)}) \ if operation.tile_description.math_instruction.opcode_class == OpcodeClass.WmmaTensorOp else "", 'compile_guard_end': "#endif" \ if operation.tile_description.math_instruction.opcode_class == OpcodeClass.WmmaTensorOp else "" })) def __exit__(self, exception_type, exception_value, traceback): # Write instance definitions in top-level namespace for instance_definition in self.instance_definitions: self.configuration_file.write(instance_definition) # Add wrapper objects within initialize() function self.configuration_file.write(SubstituteTemplate(self.initialize_function_template, { 'configuration_name': self.configuration_name })) for instance_wrapper in self.instance_wrappers: self.configuration_file.write(instance_wrapper) self.configuration_file.write(self.epilogue_template) self.configuration_file.close() ###################################################################################################
################################################################################################# # # Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: BSD-3-Clause # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # 3. Neither the name of the copyright holder nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ################################################################################################# # System imports import struct import io import ctypes # CUDA Python import from cuda import cuda from cuda import nvrtc # CUTLASS imports from library import * from gemm_operation import EmitGemmUniversalInstance ################################################################################################# # # CUTLASS Py Runtime Components # ################################################################################################# # def MaxAlignment(fmt): align = 1 for x in fmt: align = max(align, struct.calcsize(x)) return align # def AlignedOffset(offset, align): remainder = (offset % align) if remainder: offset += (align - remainder) return offset # def PackInteger(host_workspace, offset, value): fmt = "i" padding = AlignedOffset(offset, 4) struct.pack_into(fmt, host_workspace, offset, value) return padding + struct.calcsize(fmt) # def PackDevicePointer(host_workspace, offset, value): fmt = "P" offset = AlignedOffset(offset, 8) struct.pack_into(fmt, host_workspace, offset, value) return offset + struct.calcsize(fmt) # def ceil_div(a, b): return -(a // -b) ################################################################################################# # class PitchLinearCoord: def __init__(self, contiguous, strided): self.contiguous = contiguous self.strided = strided # class GemmCoord: def __init__(self, m = 1, n = 1, k = 1): self.m = m self.n = n self.k = k self.fmt = "iii" # def ceil_div(self, rhs): return GemmCoord(ceil_div(self.m, rhs.m), ceil_div(self.n, rhs.n), ceil_div(self.k, rhs.k)) # def size(self): return struct.calcsize(self.fmt) # def alignment(self): return MaxAlignment(self.fmt) # def pack_into(self, host_workspace, offset): offset = AlignedOffset(offset, 4) struct.pack_into( self.fmt, host_workspace, offset, self.m, self.n, self.k) return offset + self.size() # class TensorRef: def __init__(self, pointer = None, layout = 0): self.pointer = pointer self.layout = layout def __str__(self): return "(%x, %d)" % (self.pointer._ptr, self.layout) ################################################################################################# # class PredicatedTileAccessIteratorDesc: ''' ''' def __init__( self, element_size_bits, advance_rank, threadblock_shape, threadmap_iterations, threadmap_delta): self.element_size_bits = element_size_bits self.advance_rank = advance_rank self.threadblock_shape = threadblock_shape self.threadmap_iterations = threadmap_iterations self.threadmap_delta = threadmap_delta # class PredicatedTileAccessIteratorParams: ''' ''' # def __init__(self, desc, label): self.desc = desc self.label = label self.fmt = "qqqq" # def size(self): return struct.calcsize(self.fmt) # def alignment(self): return MaxAlignment(self.fmt) # def initialize(self, host_workspace, offset, stride): offset = AlignedOffset(offset, self.alignment()) inc_strided = stride * \ self.desc.threadmap_delta.strided * \ self.desc.element_size_bits // 8 if self.desc.advance_rank: inc_advance = self.desc.threadblock_shape.strided * \ stride * \ self.desc.element_size_bits // 8 else: inc_advance = self.desc.threadblock_shape.contiguous * \ self.desc.element_size_bits // 8 inc_next = inc_advance - (self.desc.threadmap_iterations.strided - 1) * \ self.desc.threadmap_delta.strided * \ stride * \ self.desc.element_size_bits // 8 struct.pack_into( self.fmt, host_workspace, offset, stride, inc_strided, inc_next, inc_advance) return offset + self.size() # ################################################################################################# # class EpilogueTileDesc: ''' ''' def __init__(self, column, row, group, cluster, tile): self.column = column self.row = row self.group = group self.cluster = cluster self.tile = tile # class EpilogueThreadMap: ''' ''' def __init__(self, threads, elements_per_access, element_size_bits, shape, iterations, delta, count): self.threads = threads self.elements_per_access = elements_per_access self.element_size_bits = element_size_bits self.shape = shape self.iterations = iterations self.delta = delta self.count = count pass # class EpilogueTileIteratorParams: ''' ''' # def __init__(self, desc, label): self.desc = desc self.label = label self.fmt = "qqqqqqqq" # def size(self): return struct.calcsize(self.fmt) # def alignment(self): return MaxAlignment(self.fmt) # def initialize(self, host_workspace, offset, stride): stride = stride * self.desc.element_size_bits // 8 offset = AlignedOffset(offset, self.alignment()) increment_row = stride * self.desc.delta.row increment_group = stride * self.desc.delta.group \ - stride * self.desc.delta.row * (self.desc.iterations.row - 1) increment_cluster = stride * self.desc.delta.cluster \ - stride * self.desc.delta.group * (self.desc.iterations.group - 1) \ - stride * self.desc.delta.row * (self.desc.iterations.row - 1) advance_row = stride * self.desc.shape.row advance_group = stride * \ (self.desc.shape.group - 1) * \ self.desc.shape.row * \ self.desc.count.row advance_cluster = stride * \ self.desc.count.group * \ self.desc.shape.group * \ self.desc.count.row * \ self.desc.shape.row advance_tile = stride * \ self.desc.shape.group * \ self.desc.shape.row * \ self.desc.shape.cluster * \ self.desc.shape.tile struct.pack_into( self.fmt, \ host_workspace, \ offset, \ stride, \ increment_row, increment_group, increment_cluster, \ advance_row, advance_group, advance_cluster, advance_tile) return offset + self.size() # ################################################################################################# # # Launch configuration # ################################################################################################# class LaunchConfiguration: def __init__(self, grid = [1,1,1], block = [1,1,1], smem = 0): self.grid = grid self.block = block self.shared_memory_capacity = smem ################################################################################################# # # Functors # ################################################################################################# # class Functor: def __init__(self): self.decl = '' self.definition = '' self.fmt = '' self.identifier = '' # def emit_declaration(self): return self.decl # def emit_definition(self): return self.definition # def size(self): ''' Size of the packed Params structure ''' return struct.calcsize(self.fmt) # def alignment(self): return MaxAlignment(self.fmt) # def initialize(self, host_workspace, offset, arguments): return offset + self.size() ################################################################################################# # class LinearCombinationFunctorArguments: def __init__(self, alpha = 1.0, beta = 0.0): self.alpha = alpha self.beta = beta self.alpha_ptr = 0 self.beta_ptr = 0 # class LinearCombinationFunctor(Functor): def __init__(self): super().__init__() self.decl = """ cutlass::epilogue::thread::LinearCombination< float, 1, float, float >""" self.identifier = 'linear_combination' self.fmt = "ffPP" # def size(self): ''' Size of the packed Params structure ''' return struct.calcsize(self.fmt) # def alignment(self): return MaxAlignment(self.fmt) # def initialize(self, host_workspace, offset, arguments): offset = AlignedOffset(offset, self.alignment()) struct.pack_into( self.fmt, host_workspace, offset, arguments.alpha, arguments.beta, arguments.alpha_ptr, arguments.beta_ptr) return offset + self.size() ################################################################################################# # # Base class for an executable operation # ################################################################################################# # class ExecutableOperation: ''' ''' def __init__(self, operation): self.operation = operation self.module = None self.kernel = None # def name(self): return self.operation.procedural_name() # def emit(self): return '' # def can_implement(self, configuration, arguments): return False # def get_host_workspace_size(self, arguments): return 0 # def get_device_workspace_size(self, arguments): return 0 # def plan(self, arguments): return LaunchConfiguration() # def initialize(self, host_workspace, device_workspace, launch_config, arguments, stream = cuda.CUstream(0)): raise NotImplementedError() # def run(self, host_workspace, device_workspace, launch_config, stream = cuda.CUstream(0)): cArg = (ctypes.c_char * len(host_workspace)).from_buffer(host_workspace) packed = (ctypes.c_void_p * 1)() packed[0] = ctypes.addressof(cArg) err, = cuda.cuLaunchKernel( self.kernel, launch_config.grid[0], launch_config.grid[1], launch_config.grid[2], launch_config.block[0], launch_config.block[1], launch_config.block[2], launch_config.shared_memory_capacity, stream, packed, 0) return err ################################################################################################# # class GemmArguments: ''' ''' def __init__(self): self.problem_size = GemmCoord(0, 0, 0) self.A = TensorRef() self.B = TensorRef() self.C = TensorRef() self.D = TensorRef() self.output_op = LinearCombinationFunctorArguments() # class ThreadblockSwizzle: def __init__(self, threadblock_shape, log_threadblock_cohort = 0): self.threadblock_shape = threadblock_shape self.log_threadblock_cohort = log_threadblock_cohort def grid_tiled_shape(self, problem_size): return GemmCoord( ceil_div(problem_size.m, self.threadblock_shape.m), ceil_div(problem_size.n, self.threadblock_shape.n), 1) # class Gemm(ExecutableOperation): ''' GEMM manages the CUTLASS runtime components ''' # def __init__(self, operation): super().__init__(operation) self.emitter = EmitGemmUniversalInstance('_type') self.threadblock_swizzle = ThreadblockSwizzle(GemmCoord(128, 128, 8)) self.threads = 256 self.shared_memory_capacity = (32 << 10) self.params_A = PredicatedTileAccessIteratorParams( PredicatedTileAccessIteratorDesc( 32, 1, PitchLinearCoord(128, 8), PitchLinearCoord(1, 4), PitchLinearCoord(1, 2)), 'A') self.params_B = PredicatedTileAccessIteratorParams( PredicatedTileAccessIteratorDesc( 32, 1, PitchLinearCoord(128, 8), PitchLinearCoord(1, 4), PitchLinearCoord(1, 2)), 'B') self.params_C = EpilogueTileIteratorParams( EpilogueThreadMap( 256, 1, 32, EpilogueTileDesc(128, 1, 4, 4, 1), EpilogueTileDesc(4, 1, 2, 1, 1), EpilogueTileDesc(32, 1, 8, 1, 1), EpilogueTileDesc(1, 4, 2, 1, 8)), 'C') self.params_D = EpilogueTileIteratorParams( EpilogueThreadMap( 256, 1, 32, EpilogueTileDesc(128, 1, 4, 4, 1), EpilogueTileDesc(4, 1, 2, 1, 1), EpilogueTileDesc(32, 1, 8, 1, 1), EpilogueTileDesc(1, 4, 2, 1, 8)), 'D') self.output_op = LinearCombinationFunctor() # def emit(self): return self.emitter.emit(self.operation) # def can_implement(self, configuration, arguments): pass # def get_host_workspace_size(self, arguments): return 336 # def get_device_workspace_size(self, arguments): return 0 # def plan(self, arguments): grid = self.threadblock_swizzle.grid_tiled_shape(arguments.problem_size) return LaunchConfiguration([grid.m, grid.n, grid.k], [self.threads, 1, 1], self.shared_memory_capacity) # def initialize(self, host_workspace, device_workspace, launch_config, arguments, stream = cuda.CUstream(0)): offset = 0 # Compute intermediate results swizzle_log_tile = 0 gemm_mode = 0 batch_count = 1 gemm_k_size = arguments.problem_size.k # Pack into the host workspace buffer offset = arguments.problem_size.pack_into(host_workspace, offset) grid_tiled_shape = self.threadblock_swizzle.grid_tiled_shape(arguments.problem_size) offset = grid_tiled_shape.pack_into(host_workspace, offset) offset = PackInteger(host_workspace, offset, swizzle_log_tile) offset = self.params_A.initialize(host_workspace, offset, arguments.A.layout) offset = self.params_B.initialize(host_workspace, offset, arguments.B.layout) offset = self.params_C.initialize(host_workspace, offset, arguments.C.layout) offset = self.params_D.initialize(host_workspace, offset, arguments.D.layout) offset = self.output_op.initialize(host_workspace, offset, arguments.output_op) offset = PackInteger(host_workspace, offset, gemm_mode) offset = PackInteger(host_workspace, offset, batch_count) offset = PackInteger(host_workspace, offset, gemm_k_size) offset = PackDevicePointer(host_workspace, offset, int(arguments.A.pointer)) offset = PackDevicePointer(host_workspace, offset, int(arguments.B.pointer)) offset = PackDevicePointer(host_workspace, offset, int(arguments.C.pointer)) offset = PackDevicePointer(host_workspace, offset, int(arguments.D.pointer)) return offset ################################################################################################# # # Module represents a compilation unit # ################################################################################################# # class CompilationOptions: ''' Compilation options. ''' # def __init__(self, architectures = [80], include_paths = []): self.includes = [] self.include_paths = include_paths self.flags = ['-std=c++11', '-default-device'] self.architectures = architectures # def get(self): options = [] for flag in self.flags: options.append(bytes(str.encode(flag))) for incl in self.include_paths: options.append(bytes(str.encode('--include-path=%s' % incl))) arch_list = "-arch=" for idx, arch in enumerate(self.architectures): if idx: arch_list += "," arch_list += "sm_%d" % arch options.append(bytes(str.encode(arch_list))) return options IncludeTemplate = r'''#include "${include}" ''' KernelTemplate = r''' extern "C" __global__ void ${operation_name}(${operation_name}${operation_suffix}::Params params) { // Dynamic shared memory base pointer extern __shared__ int SharedStorageBase[]; // Declare pointer to dynamic shared memory. ${operation_name}${operation_suffix}::SharedStorage *shared_storage = reinterpret_cast<${operation_name}${operation_suffix}::SharedStorage *>(SharedStorageBase); ${operation_name}${operation_suffix} op; op(params, *shared_storage); } ''' # class Module: def __init__(self, name, operations, compilation_options): self.name = name self.operations = operations self.module = None self.log = None self.cubin_image = None self.source_buffer = '' # # Emit source # self.emit_() # # Compile # self.compile_(compilation_options) # # Load module # self.load_() # Done return # Emit a source buffer def emit_(self): # 1. Includes includes = [] for operation in self.operations: for incl in operation.emitter.includes: if incl not in includes: includes.append(incl) for incl in includes: self.source_buffer += SubstituteTemplate(IncludeTemplate, { 'include': incl} ) # 2. Operations for operation in self.operations: self.source_buffer += operation.emit() values = { 'operation_name': operation.name(), 'operation_suffix': operation.emitter.operation_suffix } self.source_buffer += SubstituteTemplate(KernelTemplate, values) # Done return # Compile with NVRTC def compile_(self, compilation_options): err, program = nvrtc.nvrtcCreateProgram( str.encode(self.source_buffer), bytes(str.encode(self.name)), 0, [], []) if err != nvrtc.nvrtcResult.NVRTC_SUCCESS: raise RuntimeError('NVRTC Error: {}'.format(err)) # Compile program options = compilation_options.get() err, = nvrtc.nvrtcCompileProgram(program, len(options), options) if err != nvrtc.nvrtcResult.NVRTC_SUCCESS: error_string = 'NVRTC Error: {}\n'.format(err) # Get log from compilation err, logSize = nvrtc.nvrtcGetProgramLogSize(program) if err != nvrtc.nvrtcResult.NVRTC_SUCCESS: raise RuntimeError('NVRTC Error: {}'.format(err)) self.log = b' ' * logSize err, = nvrtc.nvrtcGetProgramLog(program, self.log) if err != nvrtc.nvrtcResult.NVRTC_SUCCESS: raise RuntimeError('NVRTC Error: {}'.format(err)) raise RuntimeError(error_string + self.log.decode() + self.source_buffer) # Get data from compilation err, dataSize = nvrtc.nvrtcGetCUBINSize(program) if err != nvrtc.nvrtcResult.NVRTC_SUCCESS: raise RuntimeError('NVRTC Error: {}'.format(err)) self.cubin_image = b' ' * dataSize err, = nvrtc.nvrtcGetCUBIN(program, self.cubin_image) if err != nvrtc.nvrtcResult.NVRTC_SUCCESS: raise RuntimeError('NVRTC Error: {}'.format(err)) return # def load_(self): # Load data as module data err, self.module = cuda.cuModuleLoadData(self.cubin_image) if err != cuda.CUresult.CUDA_SUCCESS: raise RuntimeError('Cuda Error: {}'.format(err)) # Get functions for operation in self.operations: err, operation.kernel = cuda.cuModuleGetFunction( self.module, bytes(str.encode(operation.name()))) if err != cuda.CUresult.CUDA_SUCCESS: raise RuntimeError('Cuda Error: {}'.format(err)) operation.module = self return ################################################################################################# # # Manifest represents an 'owner' for modules and operations # ################################################################################################# # class Manifest: # def __init__(self): self.operations = {} self.modules = [] pass # def append_module(self, module): ''' Appends a module and takes ownership of operations used to construct it. ''' self.modules.append(module) for operation in module.operations: self.operations[operation.name()] = operation #################################################################################################
# # \file generator.py # # \brief Generates the CUTLASS Library's instances # # import enum import os.path import shutil from library import * ################################################################################################### # class Conv2dOperation: # def __init__(self, conv_kind, iterator_algorithm, arch, tile_description, A, B, C, element_epilogue, \ stride_support, epilogue_functor = EpilogueFunctor.LinearCombination, swizzling_functor = SwizzlingFunctor.Identity1, \ group_mode = GroupMode.NoneGroup): self.operation_kind = OperationKind.Conv2d self.arch = arch self.tile_description = tile_description self.conv_kind = conv_kind self.A = A self.B = B self.C = C self.element_epilogue = element_epilogue self.epilogue_functor = epilogue_functor self.iterator_algorithm = iterator_algorithm self.stride_support = stride_support self.swizzling_functor = swizzling_functor self.group_mode = group_mode # def is_complex(self): complex_operators = [ MathOperation.multiply_add_complex, MathOperation.multiply_add_complex_gaussian ] return self.tile_description.math_instruction.math_operation in complex_operators # def accumulator_type(self): accum = self.tile_description.math_instruction.element_accumulator if self.is_complex(): return get_complex_from_real(accum) return accum # def core_name(self): ''' The basic operation kind is prefixed with a letter indicating the accumulation type. ''' intermediate_type = '' if self.tile_description.math_instruction.opcode_class == OpcodeClass.TensorOp: inst_shape = "%d%d%d" % tuple(self.tile_description.math_instruction.instruction_shape) if self.tile_description.math_instruction.element_a != self.A.element and \ self.tile_description.math_instruction.element_a != self.accumulator_type(): intermediate_type = DataTypeNames[self.tile_description.math_instruction.element_a] else: inst_shape = '' return "%s%s%s%s_%s" % (ShortDataTypeNames[self.accumulator_type()], \ inst_shape, intermediate_type, ConvKindNames[self.conv_kind], IteratorAlgorithmNames[self.iterator_algorithm]) # def extended_name(self): ''' Append data types if they differ from compute type. ''' if self.C.element != self.tile_description.math_instruction.element_accumulator and \ self.A.element != self.tile_description.math_instruction.element_accumulator: extended_name = "${element_c}_${core_name}_${element_a}" elif self.C.element == self.tile_description.math_instruction.element_accumulator and \ self.A.element != self.tile_description.math_instruction.element_accumulator: extended_name = "${core_name}_${element_a}" else: extended_name = "${core_name}" extended_name = SubstituteTemplate(extended_name, { 'element_a': DataTypeNames[self.A.element], 'element_c': DataTypeNames[self.C.element], 'core_name': self.core_name() }) return extended_name # def layout_name(self): return "%s" % (ShortLayoutTypeNames[self.A.layout]) # def configuration_name(self): ''' The full procedural name indicates architecture, extended name, tile size, and layout. ''' opcode_class_name = OpcodeClassNames[self.tile_description.math_instruction.opcode_class] threadblock = self.tile_description.procedural_name() # grouped conv if self.group_mode != GroupMode.NoneGroup: group_conv_name = f"{GroupModeNames[self.group_mode]}_" else: group_conv_name = "" if self.stride_support == StrideSupport.Unity: configuration_name = "cutlass_${opcode_class}_${extended_name}_${threadblock}_${layout}_unity_stride_${group_conv_name}align${alignment}" else: configuration_name = "cutlass_${opcode_class}_${extended_name}_${threadblock}_${layout}_${group_conv_name}align${alignment}" return SubstituteTemplate( configuration_name, { 'opcode_class': opcode_class_name, 'extended_name': self.extended_name(), 'threadblock': threadblock, 'layout': self.layout_name(), 'alignment': "%d" % self.A.alignment, 'group_conv_name': group_conv_name } ) # def procedural_name(self): ''' The full procedural name indicates architecture, extended name, tile size, and layout. ''' return self.configuration_name() ################################################################################################### # # Emits single instances of a CUTLASS device-wide operator # ################################################################################################### class EmitConv2dInstance: def __init__(self): self.template = """ // Conv2d${conv_kind_name} ${iterator_algorithm_name} kernel instance "${operation_name}" using ${operation_name}_base = typename cutlass::conv::kernel::DefaultConv2d${conv_kind_name}< ${element_a}, ${layout_a}, ${element_b}, ${layout_b}, ${element_c}, ${layout_c}, ${element_accumulator}, ${opcode_class}, ${arch}, cutlass::gemm::GemmShape<${threadblock_shape_m}, ${threadblock_shape_n}, ${threadblock_shape_k}>, cutlass::gemm::GemmShape<${warp_shape_m}, ${warp_shape_n}, ${warp_shape_k} >, cutlass::gemm::GemmShape<${instruction_shape_m}, ${instruction_shape_n}, ${instruction_shape_k}>, ${epilogue_functor}< ${element_c}, ${epilogue_vector_length}, ${element_accumulator}, ${element_epilogue} >, ${swizzling_functor}, // cutlass::gemm::threadblock::GemmSplitKIdentityThreadblockSwizzle<>, ${stages}, ${math_operator}, ${iterator_algorithm}, ${stride_support}, ${align_a}, ${align_b} >::Kernel; """ self.template_group_conv = """ // Conv2d${conv_kind_name} ${iterator_algorithm_name} kernel instance "${operation_name}" using ${operation_name}_base = typename cutlass::conv::kernel::DefaultConv2dGroup${conv_kind_name}< ${element_a}, ${layout_a}, ${element_b}, ${layout_b}, ${element_c}, ${layout_c}, ${element_accumulator}, ${opcode_class}, ${arch}, cutlass::gemm::GemmShape<${threadblock_shape_m}, ${threadblock_shape_n}, ${threadblock_shape_k}>, cutlass::gemm::GemmShape<${warp_shape_m}, ${warp_shape_n}, ${warp_shape_k} >, cutlass::gemm::GemmShape<${instruction_shape_m}, ${instruction_shape_n}, ${instruction_shape_k}>, ${epilogue_functor}< ${element_c}, ${epilogue_vector_length}, ${element_accumulator}, ${element_epilogue} >, ${swizzling_functor}, // cutlass::gemm::threadblock::GemmSplitKIdentityThreadblockSwizzle<>, ${stages}, ${math_operator}, ${group_mode}, ${iterator_algorithm}, ${stride_support}, ${align_a}, ${align_b} >::Kernel; """ self.template_depthwise_direct_conv = """ // Conv2d${conv_kind_name} ${iterator_algorithm_name} kernel instance "${operation_name}" using ${operation_name}_base = typename cutlass::conv::kernel::DefaultDepthwiseDirect2dConv${conv_kind_name}< ${element_a}, ${layout_a}, ${element_b}, ${layout_b}, ${element_c}, ${layout_c}, ${element_accumulator}, ${opcode_class}, ${arch}, cutlass::gemm::GemmShape<${threadblock_shape_m}, ${threadblock_shape_n}, ${threadblock_shape_k}>, cutlass::conv::TensorNHWCShape<${threadblock_output_shape_n}, ${threadblock_output_shape_p}, ${threadblock_output_shape_q}, ${groups_per_cta}>, cutlass::MatrixShape<${filter_shape_r}, ${filter_shape_s}>, cutlass::gemm::GemmShape<${warp_shape_m}, ${warp_shape_n}, ${warp_shape_k}>, cutlass::gemm::GemmShape<${instruction_shape_m}, ${instruction_shape_n}, ${instruction_shape_k}>, ${epilogue_functor}< ${element_c}, ${epilogue_vector_length}, ${element_accumulator}, ${element_epilogue}, cutlass::epilogue::thread::ScaleType::OnlyAlphaScaling >, cutlass::conv::threadblock::DepthwiseDirect2dConvIdentityThreadblockSwizzle< 1, ${threadblock_output_shape_n}, ${threadblock_output_shape_p}, ${threadblock_output_shape_q}>, ${stages}, ${math_operator}, ${iterator_algorithm}, ${stride_support}, cutlass::MatrixShape<${stride_r}, ${stride_s}>, cutlass::MatrixShape<${dilation_r}, ${dilation_s}> >::Kernel; """ def emit(self, operation): warp_shape = [int(operation.tile_description.threadblock_shape[idx] / operation.tile_description.warp_count[idx]) for idx in range(3)] epilogue_vector_length = int(min(operation.C.alignment * DataTypeSize[operation.C.element], 128) / DataTypeSize[operation.C.element]) values = { 'operation_name': operation.procedural_name(), 'conv_kind': ConvKindTag[operation.conv_kind], 'conv_kind_name': ConvKindNames[operation.conv_kind].capitalize(), 'element_a': DataTypeTag[operation.A.element], 'layout_a': LayoutTag[operation.A.layout], 'element_b': DataTypeTag[operation.B.element], 'layout_b': LayoutTag[operation.B.layout], 'element_c': DataTypeTag[operation.C.element], 'layout_c': LayoutTag[operation.C.layout], 'element_accumulator': DataTypeTag[operation.accumulator_type()], 'opcode_class': OpcodeClassTag[operation.tile_description.math_instruction.opcode_class], 'arch': "cutlass::arch::Sm%d" % operation.arch, 'threadblock_shape_m': str(operation.tile_description.threadblock_shape[0]), 'threadblock_shape_n': str(operation.tile_description.threadblock_shape[1]), 'threadblock_shape_k': str(operation.tile_description.threadblock_shape[2]), 'warp_shape_m': str(warp_shape[0]), 'warp_shape_n': str(warp_shape[1]), 'warp_shape_k': str(warp_shape[2]), 'instruction_shape_m': str(operation.tile_description.math_instruction.instruction_shape[0]), 'instruction_shape_n': str(operation.tile_description.math_instruction.instruction_shape[1]), 'instruction_shape_k': str(operation.tile_description.math_instruction.instruction_shape[2]), 'epilogue_vector_length': str(epilogue_vector_length), 'epilogue_functor': EpilogueFunctorTag[operation.epilogue_functor], 'element_epilogue': str(DataTypeTag[operation.element_epilogue]), 'swizzling_functor': SwizzlingFunctorTag[operation.swizzling_functor], 'stages': str(operation.tile_description.stages), 'iterator_algorithm': IteratorAlgorithmTag[operation.iterator_algorithm], 'iterator_algorithm_name': IteratorAlgorithmNames[operation.iterator_algorithm].capitalize(), 'stride_support': StrideSupportTag[operation.stride_support], 'math_operator': 'cutlass::arch::OpMultiplyAddComplex' if operation.is_complex() else \ MathOperationTag[operation.tile_description.math_instruction.math_operation], 'align_a': str(operation.A.alignment), 'align_b': str(operation.B.alignment), } if operation.group_mode == GroupMode.NoneGroup: return SubstituteTemplate(self.template, values) elif operation.group_mode == GroupMode.Depthwise: values['group_mode'] = GroupModeTag[operation.group_mode] # Setup other template params values['threadblock_output_shape_n'] = str(operation.tile_description.threadblock_output_shape[0]) values['threadblock_output_shape_p'] = str(operation.tile_description.threadblock_output_shape[1]) values['threadblock_output_shape_q'] = str(operation.tile_description.threadblock_output_shape[2]) values['groups_per_cta'] = str(operation.tile_description.threadblock_output_shape[3]) values['filter_shape_r'] = str(operation.tile_description.filter_shape[0]) values['filter_shape_s'] = str(operation.tile_description.filter_shape[1]) values['stride_r'] = str(operation.tile_description.stride[0]) values['stride_s'] = str(operation.tile_description.stride[1]) values['dilation_r'] = str(operation.tile_description.dilation[0]) values['dilation_s'] = str(operation.tile_description.dilation[1]) return SubstituteTemplate(self.template_depthwise_direct_conv, values) else: values['group_mode'] = GroupModeTag[operation.group_mode] return SubstituteTemplate(self.template_group_conv, values) ################################################################################################### # # Generator functions for all layouts # ################################################################################################### # def GenerateConv2dTensorOp(manifest, tile_descriptions, min_cc, align = 128): for tile in tile_descriptions: for conv_kind in [ConvKind.Fprop, ConvKind.Dgrad, ConvKind.Wgrad]: if conv_kind == ConvKind.Fprop or (tile.math_instruction.element_accumulator in [DataType.f16, DataType.f32]): # output_types = [tile.math_instruction.element_a, tile.math_instruction.element_accumulator] \ if DataTypeSize[tile.math_instruction.element_accumulator] == 32 \ else [tile.math_instruction.element_accumulator,] for output_type in output_types: A = TensorDescription(tile.math_instruction.element_a, LayoutType.TensorNHWC, int(align / DataTypeSize[tile.math_instruction.element_a])) B = TensorDescription(tile.math_instruction.element_b, LayoutType.TensorNHWC, int(align / DataTypeSize[tile.math_instruction.element_b])) C = TensorDescription(output_type, LayoutType.TensorNHWC, max(1, int(align / DataTypeSize[output_type]))) manifest.append(Conv2dOperation(conv_kind, min_cc, tile, A, B, C, tile.math_instruction.element_accumulator)) ################################################################################################### # # Emitters functions for all targets # ################################################################################################### class EmitConv2dConfigurationLibrary: def __init__(self, operation_path, configuration_name): self.configuration_name = configuration_name self.configuration_path = os.path.join(operation_path, "%s.cu" % configuration_name) self.instance_emitter = EmitConv2dInstance() self.instance_template = """ ${operation_instance} // Derived class struct ${operation_name} : public ${operation_name}_base { }; /////////////////////////////////////////////////////////////////////////////////////////////////// """ self.header_template = """ /* Generated by conv2d_operation.py - Do not edit. */ /////////////////////////////////////////////////////////////////////////////////////////////////// #include "cutlass/cutlass.h" #include "cutlass/library/library.h" #include "cutlass/library/manifest.h" #include "library_internal.h" #include "conv2d_operation.h" /////////////////////////////////////////////////////////////////////////////////////////////////// """ self.configuration_header = """ namespace cutlass { namespace library { // Initialize all instances void initialize_${configuration_name}(Manifest &manifest) { """ self.configuration_instance = """ using Operation_${operation_name} = cutlass::conv::device::ImplicitGemmConvolution< ${operation_name}>; manifest.append(new cutlass::library::Conv2dOperation< Operation_${operation_name}>( "${operation_name}")); """ self.configuration_direct_conv_instance = """ using Operation_${operation_name} = cutlass::conv::device::DirectConvolution< ${operation_name}>; manifest.append(new cutlass::library::DirectConv2dOperation< Operation_${operation_name}>( "${operation_name}")); """ self.configuration_epilogue = """ } """ self.epilogue_template = """ /////////////////////////////////////////////////////////////////////////////////////////////////// } // namespace library } // namespace cutlass /////////////////////////////////////////////////////////////////////////////////////////////////// """ # def __enter__(self): self.configuration_file = open(self.configuration_path, "w") self.configuration_file.write(SubstituteTemplate(self.header_template, { 'configuration_name': self.configuration_name })) self.operations = [] return self # def emit(self, operation): self.operations.append(operation) self.configuration_file.write(SubstituteTemplate(self.instance_template, { 'configuration_name': self.configuration_name, 'operation_name': operation.procedural_name(), 'operation_instance': self.instance_emitter.emit(operation) })) # def __exit__(self, exception_type, exception_value, traceback): self.configuration_file.write(SubstituteTemplate(self.configuration_header, { 'configuration_name': self.configuration_name })) for operation in self.operations: if operation.group_mode == GroupMode.Depthwise: self.configuration_file.write(SubstituteTemplate(self.configuration_direct_conv_instance, { 'configuration_name': self.configuration_name, 'operation_name': operation.procedural_name() })) else: self.configuration_file.write(SubstituteTemplate(self.configuration_instance, { 'configuration_name': self.configuration_name, 'operation_name': operation.procedural_name() })) self.configuration_file.write(self.configuration_epilogue) self.configuration_file.write(self.epilogue_template) self.configuration_file.close() ################################################################################################### ###################################################################################################
# # \file generator.py # # \brief Generates the CUTLASS Library's instances # import enum import os.path import shutil import argparse import logging from library import * from manifest import * from itertools import product ################################################################################################### # def CudaToolkitVersionSatisfies(semantic_ver_string, major, minor, patch = 0): # by default, use the latest CUDA Toolkit version cuda_version = [11, 0, 132] # Update cuda_version based on parsed string if semantic_ver_string != '': for i, x in enumerate([int(x) for x in semantic_ver_string.split('.')]): if i < len(cuda_version): cuda_version[i] = x else: cuda_version.append(x) return cuda_version >= [major, minor, patch] ################################################################################################### ################################################################################################### # def EpilogueAlignment(max_alignment, tile, epilogue_steps = 8): ''' Helper to compute the maximum alignment of the epilogue ''' def product(X, identity = 1): result = identity for item in X: result *= item return result elements_per_thread = product(tile.threadblock_shape[:-1]) // product(tile.warp_count) // 32 // epilogue_steps return min(max_alignment, elements_per_thread) def DefaultSwizzlingFunctor(): return SwizzlingFunctor.Identity8; # To use StreamK decomposition for basic GEMMs, set `swizzling_functor = SwizzlingFunctor.StreamK` # def CreateGemmOperator(manifest, layouts, tile_descriptions, data_type, \ alignment_constraints, complex_transforms = None, epilogue_functor = EpilogueFunctor.LinearCombination, \ swizzling_functor = DefaultSwizzlingFunctor()): if complex_transforms is None: complex_transforms = [(ComplexTransform.none, ComplexTransform.none),] element_a, element_b, element_c, element_epilogue = data_type operations = [] # by default, only generate the largest tile and largest alignment if manifest.kernel_filter == '': tile_descriptions = [tile_descriptions[0],] alignment_constraints = [alignment_constraints[0],] for layout in layouts: for tile_description in tile_descriptions: for alignment in alignment_constraints: for complex_transform in complex_transforms: alignment_c = min(8, alignment) A = TensorDescription(element_a, layout[0], alignment, complex_transform[0]) B = TensorDescription(element_b, layout[1], alignment, complex_transform[1]) C = TensorDescription(element_c, layout[2], alignment_c) new_operation = GemmOperation(GemmKind.Universal, tile_description.minimum_compute_capability, \ tile_description, A, B, C, element_epilogue, epilogue_functor, swizzling_functor) manifest.append(new_operation) operations.append(new_operation) return operations # Generates 3.0 API based GemmUniversal API kernels. Alignment constraints are folded in with layouts def CreateGemmUniversal3xOperator( manifest, layouts, tile_descriptions, data_type, complex_transforms=None, epilogue_functor=EpilogueFunctor.LinearCombination, swizzling_functor=SwizzlingFunctor.Identity1): if complex_transforms is None: complex_transforms = [(ComplexTransform.none, ComplexTransform.none), ] element_a, element_b, element_c, element_epilogue = data_type operations = [] # by default, only generate the largest tile and largest alignment if manifest.kernel_filter == '': tile_descriptions = [tile_descriptions[0]] for layout in layouts: for tile_description in tile_descriptions: for complex_transform in complex_transforms: A = TensorDescription( element_a, layout[0][0], layout[0][1], complex_transform[0]) B = TensorDescription( element_b, layout[1][0], layout[1][1], complex_transform[1]) C = TensorDescription(element_c, layout[2][0], layout[2][1]) operation = GemmOperation( GemmKind.Universal3x, tile_description.minimum_compute_capability, tile_description, A, B, C, element_epilogue, epilogue_functor, swizzling_functor) manifest.append(operation) operations.append(operation) return operations # def CreateSparseGemmOperator(manifest, layouts, tile_descriptions, data_type, \ alignment_constraints, complex_transforms = None, epilogue_functor = EpilogueFunctor.LinearCombination, \ swizzling_functor = SwizzlingFunctor.Identity8): if complex_transforms is None: complex_transforms = [(ComplexTransform.none, ComplexTransform.none),] element_a, element_b, element_c, element_epilogue = data_type gemm_kinds = [GemmKind.Sparse] operations = [] # by default, only generate the largest tile and largest alignment if manifest.kernel_filter == '': tile_descriptions = [tile_descriptions[0],] alignment_constraints = [alignment_constraints[0],] for layout in layouts: for tile_description in tile_descriptions: for alignment in alignment_constraints: for complex_transform in complex_transforms: alignment_c = min(8, alignment) A = TensorDescription(element_a, layout[0], alignment, complex_transform[0]) B = TensorDescription(element_b, layout[1], alignment, complex_transform[1]) C = TensorDescription(element_c, layout[2], alignment_c) new_operation = GemmOperation(GemmKind.Sparse, tile_description.minimum_compute_capability, \ tile_description, A, B, C, element_epilogue, epilogue_functor, swizzling_functor) manifest.append(new_operation) operations.append(new_operation) return operations # def CreateGemmPlanarComplexOperator(manifest, layouts, tile_descriptions, data_type, \ alignment_constraints, complex_transforms): if complex_transforms is None: complex_transforms = [(ComplexTransform.none, ComplexTransform.none),] element_a, element_b, element_c, element_epilogue = data_type gemm_kinds = [GemmKind.PlanarComplex, GemmKind.PlanarComplexArray] # by default, only generate the largest tile and largest alignment if manifest.kernel_filter == '': tile_descriptions = [tile_descriptions[0],] alignment_constraints = [alignment_constraints[0],] for gemm_kind in gemm_kinds: for layout in layouts: for tile_description in tile_descriptions: for alignment in alignment_constraints: for complex_transform in complex_transforms: alignment_c = min(8, alignment) A = TensorDescription(element_a, layout[0], alignment, complex_transform[0]) B = TensorDescription(element_b, layout[1], alignment, complex_transform[1]) C = TensorDescription(element_c, layout[2], alignment_c) manifest.append(GemmOperation(gemm_kind, \ tile_description.minimum_compute_capability, \ tile_description, A, B, C, element_epilogue)) return # def CreateGemmGroupedOperator(manifest, layouts, tile_descriptions, data_type, \ alignment_constraints, complex_transforms = None, epilogue_functor = EpilogueFunctor.LinearCombination, \ swizzling_functor = SwizzlingFunctor.Identity8): if complex_transforms is None: complex_transforms = [(ComplexTransform.none, ComplexTransform.none),] element_a, element_b, element_c, element_epilogue = data_type operations = [] # by default, only generate the largest tile and largest alignment if manifest.kernel_filter == '': tile_descriptions = [tile_descriptions[0],] alignment_constraints = [alignment_constraints[0],] for layout in layouts: for tile_description in tile_descriptions: for alignment in alignment_constraints: for complex_transform in complex_transforms: alignment_c = min(8, alignment) A = TensorDescription(element_a, layout[0], alignment, complex_transform[0]) B = TensorDescription(element_b, layout[1], alignment, complex_transform[1]) C = TensorDescription(element_c, layout[2], alignment_c) new_operation = GroupedGemmOperation(GemmKind.Grouped, tile_description.minimum_compute_capability, \ tile_description, A, B, C, element_epilogue, epilogue_functor, swizzling_functor) manifest.append(new_operation) operations.append(new_operation) return operations # def CreateRankKOperator(manifest, layouts, fill_modes, tile_descriptions, data_type, \ alignment_constraints, blas_mode, epilogue_functor = EpilogueFunctor.LinearCombination, \ swizzling_functor = SwizzlingFunctor.Identity8): element_a, element_c, element_epilogue = data_type operations = [] # by default, only generate the largest tile and largest alignment if manifest.kernel_filter == '': tile_descriptions = [tile_descriptions[0],] alignment_constraints = [alignment_constraints[0],] for layout in layouts: for fill_mode in fill_modes: for tile_description in tile_descriptions: for alignment in alignment_constraints: # SERK supported layouts (RowMajor, ColumnMajor) with no conjugation complex_transform = ComplexTransform.none # HERK supported layouts (RowMajor + conj, ColumnMajor) if blas_mode == BlasMode.hermitian and layout[0] == LayoutType.RowMajor: complex_transform = ComplexTransform.conj alignment_c = 1 # Alignment only applies to A in SYRK A = TensorDescription(element_a, layout[0], alignment, complex_transform) C = SymmetricTensorDescription(element_c, layout[1], fill_mode, alignment_c) # Rank-K update new_operation = RankKOperation(RankKKind.Universal, tile_description.minimum_compute_capability, \ tile_description, A, C, element_epilogue, epilogue_functor, swizzling_functor, blas_mode) manifest.append(new_operation) operations.append(new_operation) # Rank-2K update new_operation = Rank2KOperation(RankKKind.Universal, tile_description.minimum_compute_capability, \ tile_description, A, C, element_epilogue, epilogue_functor, swizzling_functor, blas_mode) manifest.append(new_operation) operations.append(new_operation) return operations # def CreateTrmmOperator(manifest, layouts, side_modes, fill_modes, diag_types, tile_descriptions, data_type, \ alignment_constraints, complex_transforms = None, epilogue_functor = EpilogueFunctor.LinearCombination, \ swizzling_functor = SwizzlingFunctor.Identity8): if complex_transforms is None: complex_transforms = [(ComplexTransform.none),] element_a, element_b, element_c, element_epilogue = data_type operations = [] # by default, only generate the largest tile and largest alignment if manifest.kernel_filter == '': tile_descriptions = [tile_descriptions[0],] alignment_constraints = [alignment_constraints[0],] for layout in layouts: for side_mode in side_modes: for fill_mode in fill_modes: for diag_type in diag_types: for tile_description in tile_descriptions: for alignment in alignment_constraints: for complex_transform in complex_transforms: alignment_c = min(8, alignment) A = TriangularTensorDescription(element_a, layout[0], side_mode, fill_mode, diag_type, alignment, complex_transform) B = TensorDescription(element_b, layout[1], alignment) C = TensorDescription(element_c, layout[2], alignment_c) new_operation = TrmmOperation(TrmmKind.Universal, tile_description.minimum_compute_capability, \ tile_description, A, B, C, element_epilogue, epilogue_functor, swizzling_functor) manifest.append(new_operation) operations.append(new_operation) return operations # def CreateSymmOperator(manifest, layouts, side_modes, fill_modes, tile_descriptions, data_type, \ alignment_constraints, blas_mode, epilogue_functor = EpilogueFunctor.LinearCombination, \ swizzling_functor = SwizzlingFunctor.Identity8): element_a, element_b, element_c, element_epilogue = data_type operations = [] # by default, only generate the largest tile and largest alignment if manifest.kernel_filter == '': tile_descriptions = [tile_descriptions[0],] alignment_constraints = [alignment_constraints[0],] for layout in layouts: for side_mode in side_modes: for fill_mode in fill_modes: for tile_description in tile_descriptions: for alignment in alignment_constraints: # SYMM supported layouts (RowMajor, ColumnMajor) with no conjugation complex_transform = ComplexTransform.none alignment_a = 1 # No vectorized access for the triangular matrix alignment_c = min(8, alignment) A = SymmetricTensorDescription(element_a, layout[0], fill_mode, alignment_a, complex_transform, side_mode) # tensor A and B have same data type and layout B = TensorDescription(element_b, layout[0], alignment) C = TensorDescription(element_c, layout[1], alignment_c) # SYMM/HEMM update new_operation = SymmOperation(SymmKind.Universal, tile_description.minimum_compute_capability, \ tile_description, A, B, C, element_epilogue, epilogue_functor, swizzling_functor, blas_mode) manifest.append(new_operation) operations.append(new_operation) # SYMM/HEMM update new_operation = SymmOperation(SymmKind.Universal, tile_description.minimum_compute_capability, \ tile_description, A, B, C, element_epilogue, epilogue_functor, swizzling_functor, blas_mode) manifest.append(new_operation) operations.append(new_operation) return operations ########################################################################################################### # ConvolutionOperator support variations # ____________________________________________________________________ # ConvolutionalOperator | Analytic | Optimized # ____________________________________________________________________ # | Fprop | (strided) | (strided) # | Dgrad | (strided, unity*) | (strided, unity) # | Wgrad | (strided) | (strided) # ____________________________________________________________________ # # Note : Operator marked (*) are supported but not generated to keep the instantiated kernel count low ########################################################################################################### # Convolution for 2D operations def CreateConv2dOperator(manifest, layout, tile_descriptions, data_type, alignment_constraints, \ conv_kinds = [ConvKind.Fprop, ConvKind.Dgrad, ConvKind.Wgrad], \ epilogue_functor = EpilogueFunctor.LinearCombination, swizzling_functor = SwizzlingFunctor.Identity4): element_a, element_b, element_c, element_epilogue = data_type # one exceptional case # iterator algorithm (analytic and optimized) iterator_algorithms = [IteratorAlgorithm.Analytic, IteratorAlgorithm.Optimized] # by default, only generate the largest tile size, largest alignment, and optimized iterator if manifest.kernel_filter == '': tile_descriptions = [tile_descriptions[0],] alignment_constraints = [alignment_constraints[0],] iterator_algorithms = [IteratorAlgorithm.Optimized] operations = [] for tile in tile_descriptions: for alignment in alignment_constraints: alignment_c = min(8, alignment) A = TensorDescription(element_a, layout[0], alignment) B = TensorDescription(element_b, layout[1], alignment) C = TensorDescription(element_c, layout[2], alignment_c) swizzling_functor_ = swizzling_functor # # Conv2d Fprop # if ConvKind.Fprop in conv_kinds: # Strided support for Analytic and Optimized Fprop for iterator_algorithm in iterator_algorithms: new_operations = [ # None grouped kernel Conv2dOperation(ConvKind.Fprop, iterator_algorithm, tile.minimum_compute_capability, tile,\ A, B, C, element_epilogue, StrideSupport.Strided, epilogue_functor, swizzling_functor_), ] # Instance group conv kernel if tile.math_instruction.opcode_class == OpcodeClass.TensorOp and A.layout == LayoutType.TensorNHWC and \ tile.minimum_compute_capability >= 80: # SingleGroup kernel new_operations.append(Conv2dOperation(ConvKind.Fprop, iterator_algorithm, tile.minimum_compute_capability, tile,\ A, B, C, element_epilogue, StrideSupport.Strided, epilogue_functor, swizzling_functor_, group_mode=GroupMode.SingleGroup)) # Analytic iterator supports MultipleGroup mode if iterator_algorithm == IteratorAlgorithm.Analytic: new_operations.append(Conv2dOperation(ConvKind.Fprop, iterator_algorithm, tile.minimum_compute_capability, tile,\ A, B, C, element_epilogue, StrideSupport.Strided, epilogue_functor, swizzling_functor_, group_mode=GroupMode.MultipleGroup)) for new_operation in new_operations: manifest.append(new_operation) operations.append(new_operation) # # Conv2d Dgrad # if ConvKind.Dgrad in conv_kinds: # Unity stride for Analytic and Optimized Dgrad for iterator_algorithm in iterator_algorithms: new_operation = Conv2dOperation(ConvKind.Dgrad, iterator_algorithm, tile.minimum_compute_capability, tile,\ A, B, C, element_epilogue, StrideSupport.Unity, epilogue_functor, swizzling_functor_) manifest.append(new_operation) operations.append(new_operation) # Strided support for Analytic Dgrad # strided dgrad uses a special threadblock swizzle # note that SwizzlingFunctor.StridedDgradHorizontal might be # better for problem sizes with large activation channel count swizzling_functor_strided_dgrad_ = SwizzlingFunctor.StridedDgradIdentity1 if IteratorAlgorithm.Analytic in iterator_algorithms: new_operation = Conv2dOperation(ConvKind.Dgrad, IteratorAlgorithm.Analytic, tile.minimum_compute_capability, tile,\ A, B, C, element_epilogue, StrideSupport.Strided, epilogue_functor, swizzling_functor_strided_dgrad_) manifest.append(new_operation) operations.append(new_operation) # Strided support for Optimized Dgrad if IteratorAlgorithm.Optimized in iterator_algorithms: new_operation = Conv2dOperation(ConvKind.Dgrad, IteratorAlgorithm.Optimized, tile.minimum_compute_capability, tile,\ A, B, C, element_epilogue, StrideSupport.Strided, epilogue_functor, swizzling_functor_strided_dgrad_) manifest.append(new_operation) operations.append(new_operation) # # Conv2d Wgrad # if ConvKind.Wgrad in conv_kinds: # Strided support for Analytic and Optimized Wgrad for iterator_algorithm in iterator_algorithms: new_operation = Conv2dOperation(ConvKind.Wgrad, iterator_algorithm, tile.minimum_compute_capability, tile,\ A, B, C, element_epilogue, StrideSupport.Strided, epilogue_functor, swizzling_functor_) manifest.append(new_operation) operations.append(new_operation) return operations # Convolution for 2D operations specialized for few channels def CreateConv2dFixedChannelsOperator(manifest, layout, tile_descriptions, data_type, channel_counts, \ conv_kinds = [ConvKind.Fprop, ConvKind.Dgrad, ConvKind.Wgrad], \ epilogue_functor = EpilogueFunctor.LinearCombination, swizzling_functor = SwizzlingFunctor.Identity4): element_a, element_b, element_c, element_epilogue = data_type # one exceptional case # iterator algorithm (analytic and optimized) iterator_algorithms = [IteratorAlgorithm.FixedChannels,] # by default, only generate the largest tile size, largest alignment, and optimized iterator if manifest.kernel_filter == '': tile_descriptions = [tile_descriptions[0],] channel_counts = [channel_counts[0],] operations = [] for tile in tile_descriptions: for channel_count in channel_counts: alignment_c = EpilogueAlignment(channel_count, tile) A = TensorDescription(element_a, layout[0], channel_count) B = TensorDescription(element_b, layout[1], channel_count) C = TensorDescription(element_c, layout[2], alignment_c) swizzling_functor_ = swizzling_functor # # Conv2d Fprop # if ConvKind.Fprop in conv_kinds: # Strided support for Analytic and Optimized Fprop for iterator_algorithm in iterator_algorithms: new_operation = Conv2dOperation(ConvKind.Fprop, iterator_algorithm, tile.minimum_compute_capability, tile,\ A, B, C, element_epilogue, StrideSupport.Strided, epilogue_functor, swizzling_functor_) manifest.append(new_operation) operations.append(new_operation) return operations # Convolution for 2D operations specialized for few channels def CreateConv2dFewChannelsOperator(manifest, layout, tile_descriptions, data_type, channel_counts, \ conv_kinds = [ConvKind.Fprop, ConvKind.Dgrad, ConvKind.Wgrad], \ epilogue_functor = EpilogueFunctor.LinearCombination, swizzling_functor = SwizzlingFunctor.Identity4): element_a, element_b, element_c, element_epilogue = data_type # one exceptional case # iterator algorithm (analytic and optimized) iterator_algorithms = [IteratorAlgorithm.FewChannels,] # by default, only generate the largest tile size, largest alignment, and optimized iterator if manifest.kernel_filter == '': tile_descriptions = [tile_descriptions[0],] channel_counts = [channel_counts[0],] operations = [] for tile in tile_descriptions: for channel_count in channel_counts: alignment_c = EpilogueAlignment(channel_count, tile) A = TensorDescription(element_a, layout[0], channel_count) B = TensorDescription(element_b, layout[1], channel_count) C = TensorDescription(element_c, layout[2], alignment_c) swizzling_functor_ = swizzling_functor # # Conv2d Fprop # if ConvKind.Fprop in conv_kinds: # Strided support for Analytic and Optimized Fprop for iterator_algorithm in iterator_algorithms: new_operation = Conv2dOperation(ConvKind.Fprop, iterator_algorithm, tile.minimum_compute_capability, tile,\ A, B, C, element_epilogue, StrideSupport.Strided, epilogue_functor, swizzling_functor_) manifest.append(new_operation) operations.append(new_operation) return operations # Convolution for 3D operations def CreateConv3dOperator(manifest, layout, tile_descriptions, data_type, alignment, \ conv_kinds = [ConvKind.Fprop, ConvKind.Dgrad, ConvKind.Wgrad], epilogue_functor = EpilogueFunctor.LinearCombination): element_a, element_b, element_c, element_epilogue = data_type # one exceptional case alignment_c = min(8, alignment) # iterator algorithm (analytic and optimized) iterator_algorithms = [IteratorAlgorithm.Analytic, IteratorAlgorithm.Optimized] # by default, only generate the largest tile size and optimized iterators if manifest.kernel_filter == '': tile_descriptions = [tile_descriptions[0],] iterator_algorithms = [IteratorAlgorithm.Optimized] operations = [] # All tile sizes for Conv3dFprop and Conv3dWgrad for tile in tile_descriptions: A = TensorDescription(element_a, layout, alignment) B = TensorDescription(element_b, layout, alignment) C = TensorDescription(element_c, layout, alignment_c) # # Conv3d Fprop # if ConvKind.Fprop in conv_kinds: # Strided support for Analytic and Optimized Fprop for iterator_algorithm in iterator_algorithms: new_operation = Conv3dOperation(ConvKind.Fprop, iterator_algorithm, tile.minimum_compute_capability, tile,\ A, B, C, element_epilogue, StrideSupport.Strided) manifest.append(new_operation) operations.append(new_operation) # # Conv3d Wgrad # if ConvKind.Wgrad in conv_kinds: # Strided support for Analytic and Optimized Wgrad for iterator_algorithm in iterator_algorithms: new_operation = Conv3dOperation(ConvKind.Wgrad, iterator_algorithm, tile.minimum_compute_capability, tile,\ A, B, C, element_epilogue, StrideSupport.Strided, epilogue_functor) manifest.append(new_operation) operations.append(new_operation) # All tile sizes for Conv3dDgrad for tile in tile_descriptions: A = TensorDescription(element_a, layout, alignment) B = TensorDescription(element_b, layout, alignment) C = TensorDescription(element_c, layout, alignment_c) # # Conv3d Dgrad # if ConvKind.Dgrad in conv_kinds: # Unity stride for Optimized Dgrad new_operation = Conv3dOperation(ConvKind.Dgrad, IteratorAlgorithm.Optimized, tile.minimum_compute_capability, tile,\ A, B, C, element_epilogue, StrideSupport.Unity, epilogue_functor) manifest.append(new_operation) operations.append(new_operation) # Strided support for Analytic Dgrad # Conv3dDgrad has a naive strided support which does not cut down redundant MMAs new_operation = Conv3dOperation(ConvKind.Dgrad, IteratorAlgorithm.Analytic, tile.minimum_compute_capability, tile,\ A, B, C, element_epilogue, StrideSupport.Strided, epilogue_functor) manifest.append(new_operation) operations.append(new_operation) return operations # Convolution for Depthwise 2d conv def CreateDepthwiseConv2dOperator(manifest, layout, tile_descriptions, data_type, alignment_constraints, \ conv_kinds = [ConvKind.Fprop, ConvKind.Dgrad, ConvKind.Wgrad], \ epilogue_functor = EpilogueFunctor.LinearCombination, swizzling_functor = SwizzlingFunctor.Identity4): element_a, element_b, element_c, element_epilogue = data_type # iterator algorithm (FixedStrideDilation, Optimized) iterator_algorithms = [IteratorAlgorithm.FixedStrideDilation, IteratorAlgorithm.Optimized] # by default, only generate the largest tile size, largest alignment, and optimized iterator if manifest.kernel_filter == '': tile_descriptions = [tile_descriptions[0],] alignment_constraints = [alignment_constraints[0],] operations = [] for tile in tile_descriptions: for alignment in alignment_constraints: alignment_c = min(8, alignment) A = TensorDescription(element_a, layout[0], alignment) B = TensorDescription(element_b, layout[1], alignment) C = TensorDescription(element_c, layout[2], alignment_c) swizzling_functor_ = swizzling_functor if ConvKind.Fprop in conv_kinds: # Strided support for Optimized and FixedStridedDilation Depthwise Conv for iterator_algorithm in iterator_algorithms: stride_support = StrideSupport.Strided if iterator_algorithm == IteratorAlgorithm.FixedStrideDilation: if tile.stride == [-1, -1] or tile.dilation == [-1,-1]: continue stride_support = StrideSupport.Fixed if iterator_algorithm == IteratorAlgorithm.Optimized: if tile.stride != [-1, -1] or tile.dilation != [-1,-1]: continue new_operation = Conv2dOperation(ConvKind.Fprop, iterator_algorithm, tile.minimum_compute_capability, tile, A, B, C, element_epilogue, stride_support, epilogue_functor, swizzling_functor_, group_mode=GroupMode.Depthwise) manifest.append(new_operation) operations.append(new_operation) return operations ################################################################################################### ################################################################################################### # def GenerateSM50_Simt(manifest, cuda_version): layouts = [ (LayoutType.ColumnMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.ColumnMajor, LayoutType.RowMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.RowMajor, LayoutType.ColumnMajor), ] math_instructions = [ MathInstruction( \ [1, 1, 1], \ DataType.f32, DataType.f32, DataType.f32, \ OpcodeClass.Simt, \ MathOperation.multiply_add), MathInstruction( \ [1, 1, 1], \ DataType.f64, DataType.f64, DataType.f64, \ OpcodeClass.Simt, \ MathOperation.multiply_add), ] min_cc = 50 max_cc = 1024 alignment_constraints = [1,] for math_inst in math_instructions: tile_descriptions = [ TileDescription([128, 128, 8], 2, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 64, 8], 2, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 128, 8], 2, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 64, 8], 2, [2, 1, 1], math_inst, min_cc, max_cc), TileDescription([128, 32, 8], 2, [2, 1, 1], math_inst, min_cc, max_cc), TileDescription([ 32, 128, 8], 2, [1, 2, 1], math_inst, min_cc, max_cc), ] data_type = [ math_inst.element_a, math_inst.element_b, math_inst.element_accumulator, math_inst.element_accumulator, ] CreateGemmOperator(manifest, layouts, tile_descriptions, \ data_type, alignment_constraints) if math_inst.element_a == DataType.f32: conv_layout = (LayoutType.TensorNHWC, LayoutType.TensorNHWC, LayoutType.TensorNHWC) CreateConv2dOperator(manifest, conv_layout, tile_descriptions, data_type, alignment_constraints) # # def GenerateSM50_Simt_complex(manifest, cuda_version): layouts = [ (LayoutType.ColumnMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.ColumnMajor, LayoutType.RowMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.RowMajor, LayoutType.ColumnMajor), ] math_instructions = [ MathInstruction( \ [1, 1, 1], \ DataType.f32, DataType.f32, DataType.f32, \ OpcodeClass.Simt, \ MathOperation.multiply_add_complex), ] min_cc = 50 max_cc = 1024 alignment_constraints = [1,] for math_inst in math_instructions: tile_descriptions = [ TileDescription([128, 64, 8], 2, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 128, 8], 2, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 64, 8], 2, [2, 1, 1], math_inst, min_cc, max_cc), TileDescription([128, 32, 8], 2, [2, 1, 1], math_inst, min_cc, max_cc), TileDescription([ 32, 128, 8], 2, [1, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 128, 8], 2, [4, 2, 1], math_inst, min_cc, max_cc), ] data_type = [ DataType.cf32, DataType.cf32, DataType.cf32, DataType.cf32, ] CreateGemmOperator(manifest, layouts, tile_descriptions, \ data_type, alignment_constraints) conv_layout = (LayoutType.TensorNHWC, LayoutType.TensorNHWC, LayoutType.TensorNHWC) CreateConv2dOperator(manifest, conv_layout, tile_descriptions, data_type, alignment_constraints) # # def GenerateSM50(manifest, cuda_version): GenerateSM50_Simt(manifest, cuda_version) GenerateSM50_Simt_complex(manifest, cuda_version) ################################################################################################### ################################################################################################### # def GenerateSM60_Simt(manifest, cuda_version): layouts = [ (LayoutType.ColumnMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.ColumnMajor, LayoutType.RowMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.RowMajor, LayoutType.ColumnMajor), ] math_instructions = [ MathInstruction( \ [1, 1, 1], \ DataType.f16, DataType.f16, DataType.f16, \ OpcodeClass.Simt, \ MathOperation.multiply_add), ] min_cc = 60 max_cc = 1024 alignment_constraints = [1,] for math_inst in math_instructions: tile_descriptions = [ TileDescription([256, 128, 8], 2, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 256, 8], 2, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 128, 8], 2, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 64, 8], 2, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 128, 8], 2, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 64, 8], 2, [2, 1, 1], math_inst, min_cc, max_cc), TileDescription([128, 32, 8], 2, [2, 1, 1], math_inst, min_cc, max_cc), TileDescription([ 32, 128, 8], 2, [1, 2, 1], math_inst, min_cc, max_cc), ] data_type = [ math_inst.element_a, math_inst.element_b, math_inst.element_accumulator, math_inst.element_accumulator, ] CreateGemmOperator(manifest, layouts, tile_descriptions, \ data_type, alignment_constraints) # def GenerateSM60_Simt_DepthwiseConv2d(manifest, cuda_version): math_instructions = [ MathInstruction( \ [1, 1, 1], \ DataType.f16, DataType.f16, DataType.f16, \ OpcodeClass.Simt, \ MathOperation.multiply_add), ] min_cc = 60 max_cc = 1024 alignment_constraints = [8,] filter_3x3 = [3, 3] filter_5x5 = [5, 5] # [stride_h, stride_w] # [-1, -1] means all stride size. strides = [[-1,-1], [1, 1], [2, 2]] # [dilation_h, dilation_w] # [-1, -1] means all dilation size. dilations = [[-1,-1], [1, 1], [2, 2]] #groups per thread block g16 = 16 g32 = 32 g64 = 64 #output shape per thread block npq_1x4x4 = [1, 4, 4] npq_1x8x8 = [1, 8, 8] npq_1x10x10 = [1, 10, 10] tile_descriptions = [] for math_inst in math_instructions: for stride, dilation in product(strides, dilations): tile_descriptions.extend([ # filter3x3 ThreadBlock_output, filter, stage, warp Direct2dConvFixedStrideDilationTileDescription(npq_1x8x8+[g32], filter_3x3, 3, stride, dilation,[4, 1, 1],math_inst, min_cc, max_cc), Direct2dConvFixedStrideDilationTileDescription(npq_1x8x8+[g64], filter_3x3, 3, stride, dilation,[4, 1, 1],math_inst, min_cc, max_cc), Direct2dConvFixedStrideDilationTileDescription(npq_1x8x8+[g16], filter_3x3, 3, stride, dilation,[4, 1, 1],math_inst, min_cc, max_cc), Direct2dConvFixedStrideDilationTileDescription(npq_1x10x10+[g64], filter_3x3, 2, stride, dilation,[4, 1, 1],math_inst, min_cc, max_cc), Direct2dConvFixedStrideDilationTileDescription(npq_1x4x4+[g32], filter_3x3, 4, stride, dilation, [4, 1, 1], math_inst, min_cc, max_cc), Direct2dConvFixedStrideDilationTileDescription(npq_1x4x4+[g64], filter_3x3, 4, stride, dilation,[4, 1, 1], math_inst, min_cc, max_cc), Direct2dConvFixedStrideDilationTileDescription(npq_1x4x4+[g16], filter_3x3, 4, stride, dilation, [4, 1, 1], math_inst, min_cc, max_cc), # filter5x5 ThreadBlock_output, filter, stage, warp Direct2dConvFixedStrideDilationTileDescription(npq_1x8x8+[g32], filter_5x5, 3, stride, dilation,[4, 1, 1],math_inst, min_cc, max_cc), Direct2dConvFixedStrideDilationTileDescription(npq_1x8x8+[g64], filter_5x5, 3, stride, dilation,[4, 1, 1],math_inst, min_cc, max_cc), Direct2dConvFixedStrideDilationTileDescription(npq_1x8x8+[g16], filter_5x5, 3, stride, dilation,[4, 1, 1],math_inst, min_cc, max_cc), Direct2dConvFixedStrideDilationTileDescription(npq_1x10x10+[g64], filter_5x5, 2, stride, dilation,[4, 1, 1],math_inst, min_cc, max_cc), Direct2dConvFixedStrideDilationTileDescription(npq_1x4x4+[g32], filter_5x5, 4, stride, dilation,[4, 1, 1],math_inst, min_cc, max_cc), Direct2dConvFixedStrideDilationTileDescription(npq_1x4x4+[g64], filter_5x5, 4, stride, dilation,[4, 1, 1],math_inst, min_cc, max_cc), Direct2dConvFixedStrideDilationTileDescription(npq_1x4x4+[g16], filter_5x5, 4, stride, dilation,[4, 1, 1],math_inst, min_cc, max_cc) ]) data_type = [ math_inst.element_a, math_inst.element_b, math_inst.element_accumulator, math_inst.element_accumulator, ] conv_layout = (LayoutType.TensorNHWC, LayoutType.TensorNHWC, LayoutType.TensorNHWC) CreateDepthwiseConv2dOperator(manifest, conv_layout, tile_descriptions, data_type, alignment_constraints) # # def GenerateSM60(manifest, cuda_version): GenerateSM60_Simt(manifest, cuda_version) GenerateSM60_Simt_DepthwiseConv2d(manifest, cuda_version) ################################################################################################### ################################################################################################### # def GenerateSM61_Simt(manifest, cuda_version): layouts = [ (LayoutType.ColumnMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.ColumnMajor, LayoutType.RowMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.RowMajor, LayoutType.ColumnMajor), ] math_instructions = [ MathInstruction( \ [1, 1, 4], \ DataType.s8, DataType.s8, DataType.s32, \ OpcodeClass.Simt, \ MathOperation.multiply_add), ] min_cc = 61 max_cc = 1024 alignment_constraints = [1,] for math_inst in math_instructions: tile_descriptions = [ TileDescription([128, 128, 32], 2, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 64, 32], 2, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 128, 32], 2, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 64, 32], 2, [2, 1, 1], math_inst, min_cc, max_cc), TileDescription([128, 32, 32], 2, [2, 1, 1], math_inst, min_cc, max_cc), TileDescription([ 32, 128, 32], 2, [1, 2, 1], math_inst, min_cc, max_cc), ] data_type = [ math_inst.element_a, math_inst.element_b, math_inst.element_accumulator, math_inst.element_accumulator, ] data_type_mixed = [ math_inst.element_a, math_inst.element_b, math_inst.element_a, math_inst.element_accumulator, ] CreateGemmOperator(manifest, layouts, tile_descriptions, \ data_type, alignment_constraints) CreateGemmOperator(manifest, layouts, tile_descriptions, \ data_type_mixed, alignment_constraints) # # def GenerateSM61(manifest, cuda_version): GenerateSM61_Simt(manifest, cuda_version) ################################################################################################### ################################################################################################### # def GenerateSM70_TensorOp_884(manifest, cuda_version): if not CudaToolkitVersionSatisfies(cuda_version, 10, 1): return layouts = [ (LayoutType.ColumnMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.ColumnMajor, LayoutType.RowMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.RowMajor, LayoutType.ColumnMajor), ] math_instructions = [ MathInstruction( \ [8, 8, 4], \ DataType.f16, DataType.f16, DataType.f32, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add), MathInstruction( \ [8, 8, 4], \ DataType.f16, DataType.f16, DataType.f16, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add), ] min_cc = 70 max_cc = 75 alignment_constraints = [8, 4, 2, 1] for math_inst in math_instructions: tile_descriptions = [ TileDescription([256, 128, 32], 2, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 256, 32], 2, [2, 4, 1], math_inst, min_cc, max_cc), TileDescription([128, 128, 32], 2, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([256, 64, 32], 2, [4, 1, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 256, 32], 2, [1, 4, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 128, 32], 2, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 64, 32], 2, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 64, 32], 2, [2, 2, 1], math_inst, min_cc, max_cc), ] data_type = [ math_inst.element_a, math_inst.element_b, math_inst.element_accumulator, math_inst.element_accumulator, ] CreateGemmOperator(manifest, layouts, tile_descriptions, \ data_type, alignment_constraints) conv_layout = (LayoutType.TensorNHWC, LayoutType.TensorNHWC, LayoutType.TensorNHWC) CreateConv2dOperator(manifest, conv_layout, tile_descriptions, data_type, alignment_constraints) # Avoid emitting two kernels if the accumulator type does not differ from the input type (e.g. F16 accumulation) if math_inst.element_a != math_inst.element_accumulator: data_type_mixed = [ math_inst.element_a, math_inst.element_b, math_inst.element_a, math_inst.element_accumulator, ] CreateGemmOperator(manifest, layouts, tile_descriptions, \ data_type_mixed, alignment_constraints) CreateConv2dOperator(manifest, conv_layout, tile_descriptions, data_type_mixed, alignment_constraints) # def GenerateSM70_PlanarComplexTensorOp_884(manifest, cuda_version): if not CudaToolkitVersionSatisfies(cuda_version, 10, 1): return layouts = [ (LayoutType.ColumnMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.ColumnMajor, LayoutType.RowMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.RowMajor, LayoutType.ColumnMajor), ] complex_transforms = [ (ComplexTransform.none, ComplexTransform.none), (ComplexTransform.conj, ComplexTransform.none), (ComplexTransform.none, ComplexTransform.conj), (ComplexTransform.conj, ComplexTransform.conj) ] math_instructions = [ MathInstruction( \ [8, 8, 4], \ DataType.f16, DataType.f16, DataType.f32, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add), MathInstruction( \ [8, 8, 4], \ DataType.f16, DataType.f16, DataType.f16, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add), ] min_cc = 70 max_cc = 75 alignment_constraints = [8, 2, 1] for math_inst in math_instructions: tile_descriptions = [ TileDescription([ 64, 64, 32], 2, [2, 2, 1], math_inst, min_cc, max_cc), ] data_type = [ math_inst.element_a, math_inst.element_b, math_inst.element_accumulator, math_inst.element_accumulator, ] CreateGemmPlanarComplexOperator(manifest, layouts, tile_descriptions, \ data_type, alignment_constraints, complex_transforms) # Avoid emitting two kernels if the accumulator type does not differ from the input type (e.g. F16 accumulation) if math_inst.element_a != math_inst.element_accumulator: data_type_mixed = [ math_inst.element_a, math_inst.element_b, math_inst.element_a, math_inst.element_accumulator, ] CreateGemmPlanarComplexOperator(manifest, layouts, tile_descriptions, \ data_type_mixed, alignment_constraints, complex_transforms) # def GenerateSM70_WmmaTensorOp_161616(manifest, cuda_version): layouts = [ (LayoutType.ColumnMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.ColumnMajor, LayoutType.RowMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.RowMajor, LayoutType.ColumnMajor), ] math_instructions = [ MathInstruction( \ [16, 16, 16], \ DataType.f16, DataType.f16, DataType.f32, \ OpcodeClass.WmmaTensorOp, \ MathOperation.multiply_add), MathInstruction( \ [16, 16, 16], \ DataType.f16, DataType.f16, DataType.f16, \ OpcodeClass.WmmaTensorOp, \ MathOperation.multiply_add), ] min_cc = 70 max_cc = 1024 alignment_constraints = [8,] for math_inst in math_instructions: tile_descriptions = [ TileDescription([128, 128, 32], 2, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 128, 32], 2, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 64, 32], 2, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 64, 32], 2, [2, 2, 1], math_inst, min_cc, max_cc), ] data_type = [ math_inst.element_a, math_inst.element_b, math_inst.element_accumulator, math_inst.element_accumulator, ] CreateGemmOperator(manifest, layouts, tile_descriptions, \ data_type, alignment_constraints) # Avoid emitting two kernels if the accumulator type does not differ from the input type (e.g. F16 accumulation) if math_inst.element_a != math_inst.element_accumulator: data_type_mixed = [ math_inst.element_a, math_inst.element_b, math_inst.element_a, math_inst.element_accumulator, ] CreateGemmOperator(manifest, layouts, tile_descriptions, \ data_type_mixed, alignment_constraints) # ################################################################################################## # def GenerateSM70(manifest, cuda_version): GenerateSM70_TensorOp_884(manifest, cuda_version) GenerateSM70_PlanarComplexTensorOp_884(manifest, cuda_version) # To limit build size, WMMA GEMMs are disabled for now. # #GenerateSM70_WmmaTensorOp_161616(manifest, cuda_version) ################################################################################################### ################################################################################################### # def GenerateSM75_TensorOp_1688_FewChannels(manifest, cuda_version, math_inst): min_cc = 75 max_cc = 1024 tile_descriptions = [ TileDescription([128, 64, 32], 2, [2, 4, 1], math_inst, min_cc, max_cc), TileDescription([256, 64, 32], 2, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 128, 32], 2, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 256, 32], 2, [2, 4, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 128, 32], 2, [2, 4, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 64, 32], 2, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 128, 64], 2, [2, 2, 2], math_inst, min_cc, max_cc), ] data_type = [ math_inst.element_a, math_inst.element_b, math_inst.element_accumulator, math_inst.element_accumulator, ] conv_layout = (LayoutType.TensorNHWC, LayoutType.TensorNHWC, LayoutType.TensorNHWC) CreateConv2dFixedChannelsOperator(manifest, conv_layout, tile_descriptions, data_type, [4, 8]) CreateConv2dFewChannelsOperator(manifest, conv_layout, tile_descriptions, data_type, [1, 2, 4]) # Avoid emitting two kernels if the accumulator type does not differ from the input type (e.g. F16 accumulation) if math_inst.element_a != math_inst.element_accumulator: data_type_mixed = [ math_inst.element_a, math_inst.element_b, math_inst.element_a, math_inst.element_accumulator, ] CreateConv2dFixedChannelsOperator(manifest, conv_layout, tile_descriptions, data_type_mixed, [4, 8]) CreateConv2dFewChannelsOperator(manifest, conv_layout, tile_descriptions, data_type_mixed, [1, 2, 4]) # def GenerateSM75_TensorOp_1688(manifest, cuda_version): if not CudaToolkitVersionSatisfies(cuda_version, 10, 2): return layouts = [ (LayoutType.ColumnMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.ColumnMajor, LayoutType.RowMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.RowMajor, LayoutType.ColumnMajor), ] math_instructions = [ MathInstruction( \ [16, 8, 8], \ DataType.f16, DataType.f16, DataType.f32, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add), MathInstruction( \ [16, 8, 8], \ DataType.f16, DataType.f16, DataType.f16, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add), ] min_cc = 75 max_cc = 1024 alignment_constraints = [8, 4, 2, 1] for math_inst in math_instructions: tile_descriptions = [ TileDescription([256, 128, 32], 2, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 256, 32], 2, [2, 4, 1], math_inst, min_cc, max_cc), TileDescription([128, 128, 32], 2, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 256, 32], 2, [1, 4, 1], math_inst, min_cc, max_cc), TileDescription([256, 64, 32], 2, [4, 1, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 128, 32], 2, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 64, 32], 2, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 64, 32], 2, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 128, 64], 2, [1, 2, 2], math_inst, min_cc, max_cc), ] data_type = [ math_inst.element_a, math_inst.element_b, math_inst.element_accumulator, math_inst.element_accumulator, ] CreateGemmOperator(manifest, layouts, tile_descriptions, \ data_type, alignment_constraints) conv_layout = (LayoutType.TensorNHWC, LayoutType.TensorNHWC, LayoutType.TensorNHWC) CreateConv2dOperator(manifest, conv_layout, tile_descriptions, data_type, alignment_constraints) # Avoid emitting two kernels if the accumulator type does not differ from the input type (e.g. F16 accumulation) if math_inst.element_a != math_inst.element_accumulator: data_type_mixed = [ math_inst.element_a, math_inst.element_b, math_inst.element_a, math_inst.element_accumulator, ] CreateGemmOperator(manifest, layouts, tile_descriptions, \ data_type_mixed, alignment_constraints) CreateConv2dOperator(manifest, conv_layout, tile_descriptions, data_type_mixed, alignment_constraints) # Separate generator for 'few channels' specializations GenerateSM75_TensorOp_1688_FewChannels(manifest, cuda_version, math_inst) # # def GenerateSM75_PlanarComplexTensorOp_1688(manifest, cuda_version): if not CudaToolkitVersionSatisfies(cuda_version, 10, 2): return layouts = [ (LayoutType.ColumnMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.ColumnMajor, LayoutType.RowMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.RowMajor, LayoutType.ColumnMajor), ] complex_transforms = [ (ComplexTransform.none, ComplexTransform.none), (ComplexTransform.conj, ComplexTransform.none), (ComplexTransform.none, ComplexTransform.conj), (ComplexTransform.conj, ComplexTransform.conj) ] math_instructions = [ MathInstruction( \ [16, 8, 8], \ DataType.f16, DataType.f16, DataType.f32, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add), MathInstruction( \ [16, 8, 8], \ DataType.f16, DataType.f16, DataType.f16, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add), ] min_cc = 75 max_cc = 1024 alignment_constraints = [8, 2, 1] for math_inst in math_instructions: tile_descriptions = [ TileDescription([ 64, 128, 32], 2, [2, 4, 1], math_inst, min_cc, max_cc), TileDescription([128, 64, 32], 2, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 64, 32], 2, [2, 2, 1], math_inst, min_cc, max_cc), ] data_type = [ math_inst.element_a, math_inst.element_b, math_inst.element_accumulator, math_inst.element_accumulator, ] CreateGemmPlanarComplexOperator(manifest, layouts, tile_descriptions, \ data_type, alignment_constraints, complex_transforms) # Avoid emitting two kernels if the accumulator type does not differ from the input type (e.g. F16 accumulation) if math_inst.element_a != math_inst.element_accumulator: data_type_mixed = [ math_inst.element_a, math_inst.element_b, math_inst.element_a, math_inst.element_accumulator, ] CreateGemmPlanarComplexOperator(manifest, layouts, tile_descriptions, \ data_type_mixed, alignment_constraints, complex_transforms) # def GenerateSM75_TensorOp_8816_TN(manifest, cuda_version): if not CudaToolkitVersionSatisfies(cuda_version, 10, 2): return layouts = [ (LayoutType.RowMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), ] math_instructions = [ MathInstruction( \ [8, 8, 16], \ DataType.s8, DataType.s8, DataType.s32, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add_saturate), MathInstruction( \ [8, 8, 16], \ DataType.u8, DataType.u8, DataType.s32, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add_saturate), ] min_cc = 75 max_cc = 1024 alignment_constraints = [16,] alignment_constraints_small_channels = [16, 8, 4] for math_inst in math_instructions: tile_descriptions = [ TileDescription([256, 128, 64], 2, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 256, 64], 2, [2, 4, 1], math_inst, min_cc, max_cc), TileDescription([128, 128, 64], 2, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 256, 64], 2, [1, 4, 1], math_inst, min_cc, max_cc), TileDescription([256, 64, 64], 2, [4, 1, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 128, 64], 2, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 64, 64], 2, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 64, 64], 2, [2, 2, 1], math_inst, min_cc, max_cc), ] data_type = [ math_inst.element_a, math_inst.element_b, math_inst.element_accumulator, DataType.s32, ] CreateGemmOperator(manifest, layouts, tile_descriptions, \ data_type, alignment_constraints, None, EpilogueFunctor.LinearCombination) conv_layout = (LayoutType.TensorNHWC, LayoutType.TensorNHWC, LayoutType.TensorNHWC) CreateConv2dOperator(manifest, conv_layout, tile_descriptions, data_type, alignment_constraints, [ConvKind.Fprop], EpilogueFunctor.LinearCombination) # Avoid emitting two kernels if the accumulator type does not differ from the input type (e.g. F16 accumulation) if math_inst.element_a != math_inst.element_accumulator: data_type_mixed = [ math_inst.element_a, math_inst.element_b, math_inst.element_a, DataType.f32, ] operations = [] operations += CreateGemmOperator(manifest, layouts, tile_descriptions, \ data_type_mixed, alignment_constraints, None, EpilogueFunctor.LinearCombinationClamp) operations += CreateConv2dOperator(manifest, conv_layout, tile_descriptions, data_type_mixed, alignment_constraints, [ConvKind.Fprop], EpilogueFunctor.LinearCombinationClamp) operations += CreateConv2dFixedChannelsOperator(manifest, conv_layout, tile_descriptions, data_type_mixed, alignment_constraints_small_channels, [ConvKind.Fprop], EpilogueFunctor.LinearCombinationClamp) operations += CreateConv2dFewChannelsOperator(manifest, conv_layout, tile_descriptions, data_type_mixed, alignment_constraints_small_channels, [ConvKind.Fprop], EpilogueFunctor.LinearCombinationClamp) for op in operations: if op.tile_description.threadblock_shape[1] >= 128: op.C.alignment = 16 else: op.C.alignment = 8 # # def GenerateSM75_TensorOp_8816_Interleaved(manifest, cuda_version): if not CudaToolkitVersionSatisfies(cuda_version, 10, 2): return layouts = [ (LayoutType.ColumnMajorInterleaved32, LayoutType.RowMajorInterleaved32, LayoutType.ColumnMajorInterleaved32), ] math_instructions = [ MathInstruction( \ [8, 8, 16], \ DataType.s8, DataType.s8, DataType.s32, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add_saturate), MathInstruction( \ [8, 8, 16], \ DataType.u8, DataType.u8, DataType.s32, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add_saturate), ] min_cc = 75 max_cc = 1024 alignment_constraints = [16,] for math_inst in math_instructions: tile_descriptions = [ TileDescription([256, 128, 64], 2, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 256, 64], 2, [2, 4, 1], math_inst, min_cc, max_cc), TileDescription([128, 128, 64], 2, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([256, 64, 64], 2, [4, 1, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 256, 64], 2, [1, 4, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 128, 64], 2, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 64, 64], 2, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 64, 64], 2, [2, 2, 1], math_inst, min_cc, max_cc), ] data_type_mixed = [ math_inst.element_a, math_inst.element_b, math_inst.element_a, DataType.f32, ] operations = CreateGemmOperator(manifest, layouts, tile_descriptions, \ data_type_mixed, alignment_constraints, None, EpilogueFunctor.LinearCombinationClamp) conv_layout = (LayoutType.TensorNC32HW32, LayoutType.TensorC32RSK32, LayoutType.TensorNC32HW32) operations += CreateConv2dOperator(manifest, conv_layout, tile_descriptions, data_type_mixed, alignment_constraints, [ConvKind.Fprop], EpilogueFunctor.LinearCombinationClamp) for op in operations: op.C.alignment = 8 # # def GenerateSM75_TensorOp_8832_TN(manifest, cuda_version): if not CudaToolkitVersionSatisfies(cuda_version, 10, 2): return layouts = [ (LayoutType.RowMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), ] math_instructions = [ MathInstruction( \ [8, 8, 32], \ DataType.s4, DataType.s4, DataType.s32, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add_saturate), MathInstruction( \ [8, 8, 32], \ DataType.u4, DataType.u4, DataType.s32, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add_saturate), ] min_cc = 75 max_cc = 1024 alignment_constraints = [32,] for math_inst in math_instructions: tile_descriptions = [ TileDescription([256, 128, 128], 2, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 256, 128], 2, [2, 4, 1], math_inst, min_cc, max_cc), TileDescription([128, 128, 128], 2, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([256, 64, 128], 2, [4, 1, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 256, 128], 2, [1, 4, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 128, 128], 2, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 64, 128], 2, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 64, 128], 2, [2, 2, 1], math_inst, min_cc, max_cc), ] data_type = [ math_inst.element_a, math_inst.element_b, math_inst.element_accumulator, DataType.s32, ] CreateGemmOperator(manifest, layouts, tile_descriptions, \ data_type, alignment_constraints, None, EpilogueFunctor.LinearCombination) conv_layout = (LayoutType.TensorNHWC, LayoutType.TensorNHWC, LayoutType.TensorNHWC) CreateConv2dOperator(manifest, conv_layout, tile_descriptions, data_type, alignment_constraints, [ConvKind.Fprop], EpilogueFunctor.LinearCombination) # Avoid emitting two kernels if the accumulator type does not differ from the input type (e.g. F16 accumulation) if math_inst.element_a != math_inst.element_accumulator: data_type_mixed = [ math_inst.element_a, math_inst.element_b, math_inst.element_a, DataType.f32, ] operations = [] operations += CreateGemmOperator(manifest, layouts, tile_descriptions, \ data_type_mixed, alignment_constraints, None, EpilogueFunctor.LinearCombinationClamp) operations += CreateConv2dOperator(manifest, conv_layout, tile_descriptions, data_type_mixed, alignment_constraints, [ConvKind.Fprop], EpilogueFunctor.LinearCombinationClamp) for op in operations: if op.tile_description.threadblock_shape[1] >= 128: op.C.alignment = 16 elif op.tile_description.threadblock_shape[1] == 64: op.C.alignment = 8 else: op.C.alignment = 8 # # def GenerateSM75_TensorOp_8832_Interleaved(manifest, cuda_version): if not CudaToolkitVersionSatisfies(cuda_version, 10, 2): return layouts = [ (LayoutType.ColumnMajorInterleaved64, LayoutType.RowMajorInterleaved64, LayoutType.ColumnMajorInterleaved64), ] math_instructions = [ MathInstruction( \ [8, 8, 32], \ DataType.s4, DataType.s4, DataType.s32, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add_saturate), MathInstruction( \ [8, 8, 32], \ DataType.u4, DataType.u4, DataType.s32, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add_saturate), ] min_cc = 75 max_cc = 1024 alignment_constraints = [32,] for math_inst in math_instructions: tile_descriptions = [ TileDescription([256, 128, 128], 2, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 256, 128], 2, [2, 4, 1], math_inst, min_cc, max_cc), TileDescription([128, 128, 128], 2, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([256, 64, 128], 2, [4, 1, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 256, 128], 2, [1, 4, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 128, 128], 2, [2, 2, 1], math_inst, min_cc, max_cc), ] # Avoid emitting two kernels if the accumulator type does not differ from the input type (e.g. F16 accumulation) if math_inst.element_a != math_inst.element_accumulator: data_type_mixed = [ math_inst.element_a, math_inst.element_b, math_inst.element_a, DataType.f32, ] operations = CreateGemmOperator(manifest, layouts, tile_descriptions, \ data_type_mixed, alignment_constraints, None, EpilogueFunctor.LinearCombinationClamp) conv_layout = (LayoutType.TensorNC64HW64, LayoutType.TensorC64RSK64, LayoutType.TensorNC64HW64) operations += CreateConv2dOperator(manifest, conv_layout, tile_descriptions, data_type_mixed, alignment_constraints, [ConvKind.Fprop], EpilogueFunctor.LinearCombinationClamp) for op in operations: op.C.alignment = 16 # # def GenerateSM75_TensorOp_88128(manifest, cuda_version): if not CudaToolkitVersionSatisfies(cuda_version, 11, 0): return layouts = [ (LayoutType.RowMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), ] math_instructions = [ MathInstruction( \ [8, 8, 128], \ DataType.b1, DataType.b1, DataType.s32, \ OpcodeClass.TensorOp, \ MathOperation.xor_popc), ] min_cc = 75 max_cc = 1024 alignment_constraints = [128,] for math_inst in math_instructions: tile_descriptions = [ TileDescription([256, 128, 512], 2, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 256, 512], 2, [2, 4, 1], math_inst, min_cc, max_cc), TileDescription([128, 128, 512], 2, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 256, 512], 2, [1, 4, 1], math_inst, min_cc, max_cc), TileDescription([256, 64, 512], 2, [4, 1, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 128, 512], 2, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 64, 512], 2, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 64, 512], 2, [2, 2, 1], math_inst, min_cc, max_cc), ] data_type = [DataType.b1, DataType.b1, DataType.s32, DataType.s32] CreateGemmOperator(manifest, layouts, tile_descriptions, \ data_type, alignment_constraints) # # def GenerateSM75_WmmaTensorOp_161616(manifest, cuda_version): if not CudaToolkitVersionSatisfies(cuda_version, 10, 0): return layouts = [ (LayoutType.ColumnMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.ColumnMajor, LayoutType.RowMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.RowMajor, LayoutType.ColumnMajor), ] math_instructions = [ MathInstruction( \ [16, 16, 16], \ DataType.s8, DataType.s8, DataType.s32, \ OpcodeClass.WmmaTensorOp, \ MathOperation.multiply_add), ] min_cc = 75 max_cc = 1024 alignment_constraints = [16,] for math_inst in math_instructions: tile_descriptions = [ TileDescription([128, 128, 32], 2, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 128, 32], 2, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 64, 32], 2, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 64, 32], 2, [2, 2, 1], math_inst, min_cc, max_cc), ] data_type = [ math_inst.element_a, math_inst.element_b, math_inst.element_accumulator, DataType.f32, ] CreateGemmOperator(manifest, layouts, tile_descriptions, \ data_type, alignment_constraints) # Avoid emitting two kernels if the accumulator type does not differ from the input type (e.g. F16 accumulation) if math_inst.element_a != math_inst.element_accumulator: data_type_mixed = [ math_inst.element_a, math_inst.element_b, math_inst.element_a, DataType.f32, ] CreateGemmOperator(manifest, layouts, tile_descriptions, \ data_type_mixed, alignment_constraints) # # def GenerateSM75_Simt_complex(manifest, cuda_version): math_instructions = [ MathInstruction( \ [1, 1, 1], \ DataType.f32, DataType.f32, DataType.f32, \ OpcodeClass.Simt, \ MathOperation.multiply_add_complex), ] min_cc = 75 max_cc = 1024 alignment_constraints = [1,] for math_inst in math_instructions: tile_descriptions = [ TileDescription([128, 128, 8], 5, [4, 2, 1], math_inst, min_cc, max_cc) ] data_type = [ DataType.cf32, DataType.cf32, DataType.cf32, DataType.cf32 ] complex_transforms = [ (ComplexTransform.none, ComplexTransform.none), (ComplexTransform.conj, ComplexTransform.none), (ComplexTransform.none, ComplexTransform.conj), (ComplexTransform.conj, ComplexTransform.conj) ] conv_layout = (LayoutType.TensorNHWC, LayoutType.TensorNHWC, LayoutType.TensorNHWC) CreateConv2dOperator(manifest, conv_layout, tile_descriptions, data_type, alignment_constraints) # def GenerateSM75(manifest, cuda_version): GenerateSM75_TensorOp_1688(manifest, cuda_version) GenerateSM75_PlanarComplexTensorOp_1688(manifest, cuda_version) GenerateSM75_TensorOp_8816_TN(manifest, cuda_version) GenerateSM75_TensorOp_8816_Interleaved(manifest, cuda_version) GenerateSM75_TensorOp_8832_TN(manifest, cuda_version) GenerateSM75_TensorOp_8832_Interleaved(manifest, cuda_version) GenerateSM75_TensorOp_88128(manifest, cuda_version) #GenerateSM75_WmmaTensorOp_161616(manifest, cuda_version) GenerateSM75_Simt_complex(manifest, cuda_version) ################################################################################################### ################################################################################################### # def GenerateSM80_TensorOp_16816(manifest, cuda_version): if not CudaToolkitVersionSatisfies(cuda_version, 11, 0): return layouts = [ (LayoutType.ColumnMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.ColumnMajor, LayoutType.RowMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.RowMajor, LayoutType.ColumnMajor), ] math_instructions = [ MathInstruction( \ [16, 8, 16], \ DataType.f16, DataType.f16, DataType.f32, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add), MathInstruction( \ [16, 8, 16], \ DataType.f16, DataType.f16, DataType.f16, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add), MathInstruction( \ [16, 8, 16], \ DataType.bf16, DataType.bf16, DataType.f32, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add), ] min_cc = 80 max_cc = 1024 alignment_constraints = [8, 4, 2] for math_inst in math_instructions: tile_descriptions = [ TileDescription([256, 128, 32], 3, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 256, 32], 3, [2, 4, 1], math_inst, min_cc, max_cc), TileDescription([256, 64, 32], 3, [4, 1, 1], math_inst, min_cc, max_cc), TileDescription([256, 64, 32], 4, [4, 1, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 256, 32], 4, [1, 4, 1], math_inst, min_cc, max_cc), TileDescription([128, 128, 32], 3, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 128, 32], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 128, 32], 5, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 64, 32], 6, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 128, 32], 6, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 64, 32], 10, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([256, 128, 64], 3, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 256, 64], 3, [2, 4, 1], math_inst, min_cc, max_cc), TileDescription([256, 64, 64], 4, [4, 1, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 256, 64], 4, [1, 4, 1], math_inst, min_cc, max_cc), TileDescription([128, 128, 64], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([256, 64, 64], 3, [4, 1, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 256, 64], 3, [1, 4, 1], math_inst, min_cc, max_cc), TileDescription([128, 128, 64], 3, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 64, 64], 3, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 128, 64], 3, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 64, 64], 5, [2, 2, 1], math_inst, min_cc, max_cc), ] data_type = [ math_inst.element_a, math_inst.element_b, math_inst.element_accumulator, math_inst.element_accumulator, ] CreateGemmOperator(manifest, layouts, tile_descriptions, \ data_type, alignment_constraints) CreateGemmGroupedOperator(manifest, layouts, tile_descriptions, data_type, alignment_constraints) conv_layout = (LayoutType.TensorNHWC, LayoutType.TensorNHWC, LayoutType.TensorNHWC) CreateConv2dOperator(manifest, conv_layout, tile_descriptions, data_type, alignment_constraints) CreateConv2dFixedChannelsOperator(manifest, conv_layout, tile_descriptions, data_type, [4, 8]) CreateConv3dOperator(manifest, LayoutType.TensorNDHWC, tile_descriptions, data_type, 8) # Avoid emitting two kernels if the accumulator type does not differ from the input type (e.g. F16 accumulation) if math_inst.element_a != math_inst.element_accumulator: data_type_mixed = [ math_inst.element_a, math_inst.element_b, math_inst.element_a, math_inst.element_accumulator, ] CreateGemmOperator(manifest, layouts, tile_descriptions, \ data_type_mixed, alignment_constraints) CreateConv2dOperator(manifest, conv_layout, tile_descriptions, data_type_mixed, alignment_constraints) CreateConv2dFixedChannelsOperator(manifest, conv_layout, tile_descriptions, data_type_mixed, [4, 8]) CreateConv3dOperator(manifest, LayoutType.TensorNDHWC, tile_descriptions, data_type_mixed, 8) # # def GenerateSM80_SparseTensorOp_16832(manifest, cuda_version): if not CudaToolkitVersionSatisfies(cuda_version, 11, 1): return layouts = [ (LayoutType.ColumnMajor, LayoutType.ColumnMajor, LayoutType.RowMajor), (LayoutType.ColumnMajor, LayoutType.RowMajor, LayoutType.RowMajor), (LayoutType.RowMajor, LayoutType.ColumnMajor, LayoutType.RowMajor), (LayoutType.RowMajor, LayoutType.RowMajor, LayoutType.RowMajor), ] math_instructions = [ MathInstruction( \ [16, 8, 32], \ DataType.f16, DataType.f16, DataType.f32, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add), MathInstruction( \ [16, 8, 32], \ DataType.f16, DataType.f16, DataType.f16, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add), MathInstruction( \ [16, 8, 32], \ DataType.bf16, DataType.bf16, DataType.f32, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add), ] min_cc = 80 max_cc = 1024 alignment_constraints = [8] for math_inst in math_instructions: tile_descriptions = [ TileDescription([ 64, 128, 64], 6, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([256, 128, 64], 3, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 256, 64], 3, [2, 4, 1], math_inst, min_cc, max_cc), TileDescription([128, 128, 64], 3, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([256, 64, 64], 3, [4, 1, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 256, 64], 4, [1, 4, 1], math_inst, min_cc, max_cc), TileDescription([128, 64, 64], 3, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 64, 64], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 128, 128], 3, [2, 4, 1], math_inst, min_cc, max_cc), TileDescription([256, 64, 128], 3, [4, 1, 1], math_inst, min_cc, max_cc), TileDescription([128, 64, 128], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 128, 128], 3, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 64, 128], 3, [2, 2, 1], math_inst, min_cc, max_cc), ] data_type = [ math_inst.element_a, math_inst.element_b, math_inst.element_accumulator, math_inst.element_accumulator, ] CreateSparseGemmOperator(manifest, layouts, tile_descriptions, \ data_type, alignment_constraints) # Avoid emitting two kernels if the accumulator type does not differ from the input type (e.g. F16 accumulation) if math_inst.element_a != math_inst.element_accumulator: data_type_mixed = [ math_inst.element_a, math_inst.element_b, math_inst.element_a, math_inst.element_accumulator, ] CreateSparseGemmOperator(manifest, layouts, tile_descriptions, \ data_type_mixed, alignment_constraints) # # def GenerateSM80_PlanarComplexTensorOp_16816(manifest, cuda_version): if not CudaToolkitVersionSatisfies(cuda_version, 11, 0): return layouts = [ (LayoutType.ColumnMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.ColumnMajor, LayoutType.RowMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.RowMajor, LayoutType.ColumnMajor), ] complex_transforms = [ (ComplexTransform.none, ComplexTransform.none), (ComplexTransform.conj, ComplexTransform.none), (ComplexTransform.none, ComplexTransform.conj), (ComplexTransform.conj, ComplexTransform.conj) ] math_instructions = [ MathInstruction( \ [16, 8, 16], \ DataType.f16, DataType.f16, DataType.f32, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add), MathInstruction( \ [16, 8, 16], \ DataType.bf16, DataType.bf16, DataType.f32, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add), MathInstruction( \ [16, 8, 16], \ DataType.f16, DataType.f16, DataType.f16, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add), ] min_cc = 80 max_cc = 1024 alignment_constraints = [8, ] for math_inst in math_instructions: tile_descriptions = [ TileDescription([ 64, 128, 32], 3, [2, 4, 1], math_inst, min_cc, max_cc), TileDescription([128, 64, 32], 3, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 64, 32], 4, [2, 2, 1], math_inst, min_cc, max_cc), ] data_type = [ math_inst.element_a, math_inst.element_b, math_inst.element_accumulator, math_inst.element_accumulator, ] CreateGemmPlanarComplexOperator(manifest, layouts, tile_descriptions, \ data_type, alignment_constraints, complex_transforms) # Avoid emitting two kernels if the accumulator type does not differ from the input type (e.g. F16 accumulation) if math_inst.element_a != math_inst.element_accumulator: data_type_mixed = [ math_inst.element_a, math_inst.element_b, math_inst.element_a, math_inst.element_accumulator, ] CreateGemmPlanarComplexOperator(manifest, layouts, tile_descriptions, \ data_type_mixed, alignment_constraints, complex_transforms) # def GenerateSM80_TensorOp_16832_TN(manifest, cuda_version): if not CudaToolkitVersionSatisfies(cuda_version, 11, 0): return layouts = [ (LayoutType.RowMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), ] math_instructions = [ MathInstruction( \ [16, 8, 32], \ DataType.s8, DataType.s8, DataType.s32, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add_saturate), MathInstruction( \ [16, 8, 32], \ DataType.u8, DataType.u8, DataType.s32, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add_saturate), ] min_cc = 80 max_cc = 1024 smem_usage = 164 alignment_constraints = [16,] alignment_constraints_small_channels = [16, 8, 4] for math_inst in math_instructions: tile_descriptions = [ TileDescription([256, 128, 64], 3, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 256, 64], 3, [2, 4, 1], math_inst, min_cc, max_cc), TileDescription([256, 64, 64], 4, [4, 1, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 256, 64], 4, [1, 4, 1], math_inst, min_cc, max_cc), TileDescription([128, 128, 64], 5, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 64, 64], 6, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 128, 64], 6, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 64, 64], 10, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([256, 128, 128], 3, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 256, 128], 3, [2, 4, 1], math_inst, min_cc, max_cc), TileDescription([256, 64, 128], 4, [4, 1, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 256, 128], 4, [1, 4, 1], math_inst, min_cc, max_cc), TileDescription([128, 128, 128], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 64, 128], 3, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 128, 128], 3, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 64, 128], 5, [2, 2, 1], math_inst, min_cc, max_cc), ] data_type = [math_inst.element_a, math_inst.element_b, math_inst.element_accumulator, DataType.s32] data_type_mixed = [math_inst.element_a, math_inst.element_b, math_inst.element_a, DataType.f32] CreateGemmOperator(manifest, layouts, tile_descriptions, \ data_type, alignment_constraints, None, EpilogueFunctor.LinearCombination) conv_layout = (LayoutType.TensorNHWC, LayoutType.TensorNHWC, LayoutType.TensorNHWC) CreateConv2dOperator(manifest, conv_layout, tile_descriptions, data_type, alignment_constraints, [ConvKind.Fprop], EpilogueFunctor.LinearCombination) operations = [] operations += CreateGemmOperator(manifest, layouts, tile_descriptions, \ data_type_mixed, alignment_constraints, None, EpilogueFunctor.LinearCombinationClamp) operations += CreateConv2dOperator(manifest, conv_layout, tile_descriptions, data_type_mixed, alignment_constraints, [ConvKind.Fprop], EpilogueFunctor.LinearCombinationClamp) operations += CreateConv2dFixedChannelsOperator(manifest, conv_layout, tile_descriptions, data_type_mixed, alignment_constraints_small_channels, [ConvKind.Fprop], EpilogueFunctor.LinearCombinationClamp) operations += CreateConv2dFewChannelsOperator(manifest, conv_layout, tile_descriptions, data_type_mixed, alignment_constraints_small_channels, [ConvKind.Fprop], EpilogueFunctor.LinearCombinationClamp) for op in operations: if op.tile_description.threadblock_shape[1] >= 128: op.C.alignment = 16 else: op.C.alignment = 8 # # def GenerateSM80_SparseTensorOp_16864_TN(manifest, cuda_version): if not CudaToolkitVersionSatisfies(cuda_version, 11, 1): return layouts = [ (LayoutType.RowMajor, LayoutType.ColumnMajor, LayoutType.RowMajor), ] math_inst = \ MathInstruction( \ [16, 8, 64], \ DataType.s8, DataType.s8, DataType.s32, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add_saturate) min_cc = 80 max_cc = 1024 alignment_constraints = [16,] tile_descriptions = [ TileDescription([128, 64, 128], 3, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([256, 128, 128], 3, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 256, 128], 3, [2, 4, 1], math_inst, min_cc, max_cc), TileDescription([128, 128, 128], 3, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([256, 64, 128], 3, [4, 1, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 256, 128], 4, [1, 4, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 128, 128], 6, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 64, 128], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 128, 256], 3, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 64, 256], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 128, 256], 3, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 64, 256], 3, [2, 2, 1], math_inst, min_cc, max_cc), ] data_type = [DataType.s8, DataType.s8, DataType.s32, DataType.s32] data_type_mixed = [DataType.s8, DataType.s8, DataType.s8, DataType.f32] CreateSparseGemmOperator(manifest, layouts, tile_descriptions, \ data_type, alignment_constraints, None, EpilogueFunctor.LinearCombination) operations = [] operations += CreateSparseGemmOperator(manifest, layouts, tile_descriptions, \ data_type_mixed, alignment_constraints, None, EpilogueFunctor.LinearCombinationClamp) for op in operations: if op.tile_description.threadblock_shape[1] >= 128: op.C.alignment = 16 else: op.C.alignment = 8 # # def GenerateSM80_TensorOp_16832_Interleaved(manifest, cuda_version): if not CudaToolkitVersionSatisfies(cuda_version, 11, 0): return layouts = [ (LayoutType.ColumnMajorInterleaved32, LayoutType.RowMajorInterleaved32, LayoutType.ColumnMajorInterleaved32), ] math_instructions = [ MathInstruction( \ [16, 8, 32], \ DataType.s8, DataType.s8, DataType.s32, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add_saturate), MathInstruction( \ [16, 8, 32], \ DataType.u8, DataType.u8, DataType.s32, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add_saturate), ] min_cc = 80 max_cc = 1024 alignment_constraints = [16,] for math_inst in math_instructions: tile_descriptions = [ TileDescription([256, 128, 64], 3, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 256, 64], 3, [2, 4, 1], math_inst, min_cc, max_cc), TileDescription([256, 64, 64], 4, [4, 1, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 256, 64], 4, [1, 4, 1], math_inst, min_cc, max_cc), TileDescription([128, 128, 64], 5, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 64, 64], 6, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 128, 64], 6, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 64, 64], 10, [2, 2, 1], math_inst, min_cc, max_cc), ] data_type_mixed = [math_inst.element_a, math_inst.element_b, math_inst.element_a, DataType.f32] operations = CreateGemmOperator(manifest, layouts, tile_descriptions, \ data_type_mixed, alignment_constraints, None, EpilogueFunctor.LinearCombinationClamp) conv_layout = (LayoutType.TensorNC32HW32, LayoutType.TensorC32RSK32, LayoutType.TensorNC32HW32) operations += CreateConv2dOperator(manifest, conv_layout, tile_descriptions, data_type_mixed, alignment_constraints, [ConvKind.Fprop], EpilogueFunctor.LinearCombinationClamp) for op in operations: op.C.alignment = 8 # # def GenerateSM80_TensorOp_16864_TN(manifest, cuda_version): if not CudaToolkitVersionSatisfies(cuda_version, 11, 0): return layouts = [ (LayoutType.RowMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), ] math_instructions = [ MathInstruction( \ [16, 8, 64], \ DataType.s4, DataType.s4, DataType.s32, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add_saturate), MathInstruction( \ [16, 8, 64], \ DataType.u4, DataType.u4, DataType.s32, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add_saturate), ] min_cc = 80 max_cc = 1024 alignment_constraints = [32,] for math_inst in math_instructions: tile_descriptions = [ TileDescription([256, 128, 128], 3, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 256, 128], 3, [2, 4, 1], math_inst, min_cc, max_cc), TileDescription([256, 64, 128], 4, [4, 1, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 256, 128], 4, [1, 4, 1], math_inst, min_cc, max_cc), TileDescription([128, 128, 128], 5, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 64, 128], 6, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 128, 128], 6, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 64, 128], 10, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([256, 128, 256], 3, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 256, 256], 3, [2, 4, 1], math_inst, min_cc, max_cc), TileDescription([256, 64, 256], 4, [4, 1, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 256, 256], 4, [1, 4, 1], math_inst, min_cc, max_cc), TileDescription([128, 128, 256], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 128, 256], 3, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 64, 256], 3, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 128, 256], 3, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 64, 256], 5, [2, 2, 1], math_inst, min_cc, max_cc), ] data_type = [math_inst.element_a, math_inst.element_b, math_inst.element_accumulator, DataType.s32] data_type_mixed = [math_inst.element_a, math_inst.element_b, math_inst.element_a, DataType.f32] CreateGemmOperator(manifest, layouts, tile_descriptions, \ data_type, alignment_constraints, None, EpilogueFunctor.LinearCombination) operations = [] operations += CreateGemmOperator(manifest, layouts, tile_descriptions, \ data_type_mixed, alignment_constraints, None, EpilogueFunctor.LinearCombinationClamp) conv_layout = (LayoutType.TensorNHWC, LayoutType.TensorNHWC, LayoutType.TensorNHWC) CreateConv2dOperator(manifest, conv_layout, tile_descriptions, data_type, alignment_constraints, [ConvKind.Fprop], EpilogueFunctor.LinearCombination) operations += CreateConv2dOperator(manifest, conv_layout, tile_descriptions, data_type_mixed, alignment_constraints, [ConvKind.Fprop], EpilogueFunctor.LinearCombinationClamp) for op in operations: if op.tile_description.threadblock_shape[1] >= 128: op.C.alignment = 16 elif op.tile_description.threadblock_shape[1] == 64: op.C.alignment = 8 else: op.C.alignment = 8 # # def GenerateSM80_SparseTensorOp_168128_TN(manifest, cuda_version): if not CudaToolkitVersionSatisfies(cuda_version, 11, 1): return layouts = [ (LayoutType.RowMajor, LayoutType.ColumnMajor, LayoutType.RowMajor), ] math_inst = \ MathInstruction( \ [16, 8, 128], \ DataType.s4, DataType.s4, DataType.s32, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add_saturate) min_cc = 80 max_cc = 1024 alignment_constraints = [32,] tile_descriptions = [ TileDescription([ 64, 64, 256], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([256, 64, 256], 3, [4, 1, 1], math_inst, min_cc, max_cc), TileDescription([256, 128, 256], 3, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 256, 256], 3, [2, 4, 1], math_inst, min_cc, max_cc), TileDescription([128, 128, 256], 3, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 256, 256], 4, [1, 4, 1], math_inst, min_cc, max_cc), TileDescription([128, 64, 256], 3, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 128, 256], 6, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 128, 512], 3, [2, 4, 1], math_inst, min_cc, max_cc), TileDescription([128, 64, 512], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 128, 512], 3, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 64, 512], 3, [2, 2, 1], math_inst, min_cc, max_cc), ] data_type = [DataType.s4, DataType.s4, DataType.s32, DataType.s32] data_type_mixed = [DataType.s4, DataType.s4, DataType.s4, DataType.f32] CreateSparseGemmOperator(manifest, layouts, tile_descriptions, \ data_type, alignment_constraints, None, EpilogueFunctor.LinearCombination) operations = [] operations += CreateSparseGemmOperator(manifest, layouts, tile_descriptions, \ data_type_mixed, alignment_constraints, None, EpilogueFunctor.LinearCombinationClamp) for op in operations: if op.tile_description.threadblock_shape[1] > 128: op.C.alignment = 16 else: op.C.alignment = 8 # # def GenerateSM80_TensorOp_16864_Interleaved(manifest, cuda_version): if not CudaToolkitVersionSatisfies(cuda_version, 11, 0): return layouts = [ (LayoutType.ColumnMajorInterleaved64, LayoutType.RowMajorInterleaved64, LayoutType.ColumnMajorInterleaved64), ] math_instructions = [ MathInstruction( \ [16, 8, 64], \ DataType.s4, DataType.s4, DataType.s32, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add_saturate), MathInstruction( \ [16, 8, 64], \ DataType.u4, DataType.u4, DataType.s32, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add_saturate), ] min_cc = 80 max_cc = 1024 alignment_constraints = [32,] for math_inst in math_instructions: tile_descriptions = [ TileDescription([256, 128, 128], 3, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 256, 128], 3, [2, 4, 1], math_inst, min_cc, max_cc), TileDescription([256, 64, 128], 4, [4, 1, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 256, 128], 4, [1, 4, 1], math_inst, min_cc, max_cc), TileDescription([128, 128, 128], 5, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 128, 128], 6, [2, 2, 1], math_inst, min_cc, max_cc), ] data_type_mixed = [math_inst.element_a, math_inst.element_b, math_inst.element_a, DataType.f32] operations = [] operations += CreateGemmOperator(manifest, layouts, tile_descriptions, \ data_type_mixed, alignment_constraints, None, EpilogueFunctor.LinearCombinationClamp) conv_layout = (LayoutType.TensorNC64HW64, LayoutType.TensorC64RSK64, LayoutType.TensorNC64HW64) operations += CreateConv2dOperator(manifest, conv_layout, tile_descriptions, data_type_mixed, alignment_constraints, [ConvKind.Fprop], EpilogueFunctor.LinearCombinationClamp) for op in operations: op.C.alignment = 16 # # def GenerateSM80_TensorOp_168256(manifest, cuda_version): if not CudaToolkitVersionSatisfies(cuda_version, 11, 0): return layouts = [ (LayoutType.RowMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), ] math_instructions = [ MathInstruction( \ [16, 8, 256], \ DataType.b1, DataType.b1, DataType.s32, \ OpcodeClass.TensorOp, \ MathOperation.xor_popc), ] min_cc = 80 max_cc = { MathOperation.xor_popc: 1024 } alignment_constraints = [128,] for math_inst in math_instructions: tile_descriptions = [ TileDescription([256, 128, 512], 3, [4, 2, 1], math_inst, min_cc, max_cc[math_inst.math_operation]), TileDescription([128, 256, 512], 3, [2, 4, 1], math_inst, min_cc, max_cc[math_inst.math_operation]), TileDescription([256, 64, 512], 4, [4, 1, 1], math_inst, min_cc, max_cc[math_inst.math_operation]), TileDescription([ 64, 256, 512], 4, [1, 4, 1], math_inst, min_cc, max_cc[math_inst.math_operation]), TileDescription([128, 128, 512], 5, [2, 2, 1], math_inst, min_cc, max_cc[math_inst.math_operation]), TileDescription([128, 64, 512], 6, [2, 2, 1], math_inst, min_cc, max_cc[math_inst.math_operation]), TileDescription([ 64, 128, 512], 6, [2, 2, 1], math_inst, min_cc, max_cc[math_inst.math_operation]), TileDescription([ 64, 64, 512], 10, [2, 2, 1], math_inst, min_cc, max_cc[math_inst.math_operation]), TileDescription([256, 128, 1024], 3, [4, 2, 1], math_inst, min_cc, max_cc[math_inst.math_operation]), TileDescription([128, 256, 1024], 3, [2, 4, 1], math_inst, min_cc, max_cc[math_inst.math_operation]), TileDescription([256, 64, 1024], 4, [4, 1, 1], math_inst, min_cc, max_cc[math_inst.math_operation]), TileDescription([ 64, 256, 1024], 4, [1, 4, 1], math_inst, min_cc, max_cc[math_inst.math_operation]), TileDescription([128, 128, 1024], 4, [2, 2, 1], math_inst, min_cc, max_cc[math_inst.math_operation]), TileDescription([128, 64, 1024], 3, [2, 2, 1], math_inst, min_cc, max_cc[math_inst.math_operation]), TileDescription([ 64, 128, 1024], 3, [2, 2, 1], math_inst, min_cc, max_cc[math_inst.math_operation]), TileDescription([ 64, 64, 1024], 5, [2, 2, 1], math_inst, min_cc, max_cc[math_inst.math_operation]), ] data_type = [DataType.b1, DataType.b1, DataType.s32, DataType.s32] CreateGemmOperator(manifest, layouts, tile_descriptions, \ data_type, alignment_constraints) # # def GenerateSM80_TensorOp_1688(manifest, cuda_version): if not CudaToolkitVersionSatisfies(cuda_version, 11, 0): return layouts = [ (LayoutType.ColumnMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.ColumnMajor, LayoutType.RowMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.RowMajor, LayoutType.ColumnMajor), ] math_instructions = [ MathInstruction( \ [16, 8, 8], \ DataType.tf32, DataType.tf32, DataType.f32, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add) ] min_cc = 80 max_cc = 1024 alignment_constraints = [4, 2, 1] for math_inst in math_instructions: tile_descriptions = [ TileDescription([256, 128, 16], 3, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 256, 16], 3, [2, 4, 1], math_inst, min_cc, max_cc), TileDescription([256, 64, 16], 4, [4, 1, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 256, 16], 4, [1, 4, 1], math_inst, min_cc, max_cc), TileDescription([128, 128, 16], 5, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 128, 16], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 128, 16], 3, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 64, 16], 6, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 128, 16], 6, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 64, 16], 10, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([256, 128, 32], 3, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 256, 32], 3, [2, 4, 1], math_inst, min_cc, max_cc), TileDescription([256, 64, 32], 4, [4, 1, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 256, 32], 4, [1, 4, 1], math_inst, min_cc, max_cc), TileDescription([128, 128, 32], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 128, 32], 3, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 64, 32], 3, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([64, 128, 32], 3, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 64, 32], 5, [2, 2, 1], math_inst, min_cc, max_cc), ] data_type = [ math_inst.element_a, math_inst.element_b, math_inst.element_accumulator, math_inst.element_accumulator, ] data_type_mixed = [ math_inst.element_a, math_inst.element_b, math_inst.element_a, math_inst.element_accumulator, ] CreateGemmOperator(manifest, layouts, tile_descriptions, \ data_type, alignment_constraints) CreateGemmOperator(manifest, layouts, tile_descriptions, \ data_type_mixed, alignment_constraints) conv_layout = (LayoutType.TensorNHWC, LayoutType.TensorNHWC, LayoutType.TensorNHWC) CreateConv2dOperator(manifest, conv_layout, tile_descriptions, data_type, alignment_constraints) CreateConv2dOperator(manifest, conv_layout, tile_descriptions, data_type_mixed, alignment_constraints) # # def GenerateSM80_TensorOp_1688_fast_math(manifest, cuda_version): if not CudaToolkitVersionSatisfies(cuda_version, 11, 0): return layouts = [ (LayoutType.ColumnMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.ColumnMajor, LayoutType.RowMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.RowMajor, LayoutType.ColumnMajor), ] math_instructions = [ MathInstruction( \ [16, 8, 8], \ DataType.tf32, DataType.tf32, DataType.f32, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add), MathInstruction( \ [16, 8, 8], \ DataType.f16, DataType.f16, DataType.f32, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add_fast_f16), MathInstruction( \ [16, 8, 8], \ DataType.bf16, DataType.bf16, DataType.f32, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add_fast_bf16), ] min_cc = 80 max_cc = 1024 alignment_constraints = [4, 2, 1] for math_inst in math_instructions: tile_descriptions = [ TileDescription([256, 128, 16], 3, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 256, 16], 3, [2, 4, 1], math_inst, min_cc, max_cc), TileDescription([256, 64, 16], 4, [4, 1, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 256, 16], 4, [1, 4, 1], math_inst, min_cc, max_cc), TileDescription([128, 128, 16], 5, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 128, 16], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 128, 16], 3, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 64, 16], 6, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 128, 16], 6, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 64, 16], 10, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([256, 128, 32], 3, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 256, 32], 3, [2, 4, 1], math_inst, min_cc, max_cc), TileDescription([256, 64, 32], 4, [4, 1, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 256, 32], 4, [1, 4, 1], math_inst, min_cc, max_cc), TileDescription([128, 128, 32], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 128, 32], 3, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 64, 32], 3, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 128, 32], 3, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 64, 32], 5, [2, 2, 1], math_inst, min_cc, max_cc), ] data_type = [DataType.f32, DataType.f32, DataType.f32, DataType.f32] CreateGemmOperator(manifest, layouts, tile_descriptions, \ data_type, alignment_constraints) conv_layout = (LayoutType.TensorNHWC, LayoutType.TensorNHWC, LayoutType.TensorNHWC) CreateConv2dOperator(manifest, conv_layout, tile_descriptions, data_type, alignment_constraints) # # def GenerateSM80_TensorOp_1688_fast_fp32_math(manifest, cuda_version): if not CudaToolkitVersionSatisfies(cuda_version, 11, 0): return layouts = [ (LayoutType.ColumnMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.ColumnMajor, LayoutType.RowMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.RowMajor, LayoutType.ColumnMajor), ] math_instructions = [ MathInstruction( \ [16, 8, 8], \ DataType.f32, DataType.f32, DataType.f32, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add_fast_f32), ] min_cc = 80 max_cc = 1024 alignment_constraints = [4, 2, 1] for math_inst in math_instructions: tile_descriptions = [ TileDescription([128, 128, 16], 4, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 128, 16], 3, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([256, 64, 16], 3, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 256, 16], 3, [2, 4, 1], math_inst, min_cc, max_cc), TileDescription([128, 64, 16], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 128, 16], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 64, 16], 3, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 128, 32], 3, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([256, 64, 32], 3, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 256, 32], 3, [2, 4, 1], math_inst, min_cc, max_cc), TileDescription([128, 64, 32], 3, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 128, 32], 3, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 64, 32], 3, [2, 2, 1], math_inst, min_cc, max_cc), ] data_type = [DataType.f32, DataType.f32, DataType.f32, DataType.f32] CreateGemmOperator(manifest, layouts, tile_descriptions, \ data_type, alignment_constraints) conv_layout = (LayoutType.TensorNHWC, LayoutType.TensorNHWC, LayoutType.TensorNHWC) CreateConv2dOperator(manifest, conv_layout, tile_descriptions, data_type, alignment_constraints) # def GenerateSM80_TensorOp_1688_fast_fp32_math_complex(manifest, cuda_version): if not CudaToolkitVersionSatisfies(cuda_version, 11, 0): return layouts = [ (LayoutType.ColumnMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.ColumnMajor, LayoutType.RowMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.RowMajor, LayoutType.ColumnMajor), ] math_inst = MathInstruction( \ [16, 8, 8], \ DataType.f32, DataType.f32, DataType.f32, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add_complex_fast_f32) min_cc = 80 max_cc = 1024 tile_descriptions = [ TileDescription([128, 64, 16], 3, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([64, 128, 16], 3, [2, 4, 1], math_inst, min_cc, max_cc), TileDescription([64, 64, 16], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([64, 32, 16], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([32, 64, 16], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([32, 32, 16], 3, [2, 2, 1], math_inst, min_cc, max_cc), ] data_type = [ DataType.cf32, DataType.cf32, DataType.cf32, DataType.cf32 ] alignment_constraints = [1,] complex_transforms = [ (ComplexTransform.none, ComplexTransform.none), (ComplexTransform.conj, ComplexTransform.none), (ComplexTransform.none, ComplexTransform.conj), (ComplexTransform.conj, ComplexTransform.conj) ] CreateGemmOperator(manifest, layouts, tile_descriptions, \ data_type, alignment_constraints, complex_transforms) # def GenerateSM80_SparseTensorOp_16816_fast_math(manifest, cuda_version): if not CudaToolkitVersionSatisfies(cuda_version, 11, 1): return layouts = [ (LayoutType.ColumnMajor, LayoutType.ColumnMajor, LayoutType.RowMajor), (LayoutType.ColumnMajor, LayoutType.RowMajor, LayoutType.RowMajor), (LayoutType.RowMajor, LayoutType.ColumnMajor, LayoutType.RowMajor), (LayoutType.RowMajor, LayoutType.RowMajor, LayoutType.RowMajor), ] math_instructions = [ MathInstruction( \ [16, 8, 16], \ DataType.tf32, DataType.tf32, DataType.f32, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add), ] min_cc = 80 max_cc = 1024 alignment_constraints = [4] for math_inst in math_instructions: tile_descriptions = [ TileDescription([128, 64, 32], 3, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 128, 32], 3, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([256, 128, 32], 3, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 256, 32], 3, [2, 4, 1], math_inst, min_cc, max_cc), TileDescription([256, 64, 32], 3, [4, 1, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 256, 32], 4, [1, 4, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 128, 32], 6, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 64, 32], 6, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 128, 64], 3, [2, 4, 1], math_inst, min_cc, max_cc), TileDescription([256, 64, 64], 3, [4, 1, 1], math_inst, min_cc, max_cc), TileDescription([128, 64, 64], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 128, 64], 3, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 64, 64], 3, [2, 2, 1], math_inst, min_cc, max_cc), ] data_type = [DataType.f32, DataType.f32, DataType.f32, DataType.f32] CreateSparseGemmOperator(manifest, layouts, tile_descriptions, \ data_type, alignment_constraints) # # def GenerateSM80_TensorOp_1688_complex(manifest, cuda_version): if not CudaToolkitVersionSatisfies(cuda_version, 11, 0): return layouts = [ (LayoutType.ColumnMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.ColumnMajor, LayoutType.RowMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.RowMajor, LayoutType.ColumnMajor), ] math_inst = MathInstruction( \ [16, 8, 8], \ DataType.tf32, DataType.tf32, DataType.f32, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add_complex) min_cc = 80 max_cc = 1024 tile_descriptions = [ TileDescription([128, 128, 16], 4, [2, 4, 1], math_inst, min_cc, max_cc), TileDescription([128, 64, 16], 4, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([64, 128, 16], 4, [2, 4, 1], math_inst, min_cc, max_cc), TileDescription([64, 64, 16], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([64, 32, 16], 4, [2, 1, 1], math_inst, min_cc, max_cc), TileDescription([32, 64, 16], 4, [1, 2, 1], math_inst, min_cc, max_cc), TileDescription([32, 32, 16], 4, [2, 2, 1], math_inst, min_cc, max_cc), ] data_type = [ DataType.cf32, DataType.cf32, DataType.cf32, DataType.cf32 ] alignment_constraints = [1,] complex_transforms = [ (ComplexTransform.none, ComplexTransform.none), (ComplexTransform.conj, ComplexTransform.none), (ComplexTransform.none, ComplexTransform.conj), (ComplexTransform.conj, ComplexTransform.conj) ] CreateGemmOperator(manifest, layouts, tile_descriptions, \ data_type, alignment_constraints, complex_transforms) # # def GenerateSM80_TensorOp_1688_rank_k(manifest, cuda_version): if not CudaToolkitVersionSatisfies(cuda_version, 11, 0): return layouts = [ (LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.ColumnMajor), ] fill_modes = [ FillMode.Lower, FillMode.Upper, ] math_instructions = [ MathInstruction( \ [16, 8, 8], \ DataType.tf32, DataType.tf32, DataType.f32, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add), MathInstruction( \ [16, 8, 8], \ DataType.f32, DataType.f32, DataType.f32, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add_fast_f32), ] min_cc = 80 max_cc = 1024 alignment_constraints = [1, 2, 4] # Alignment only applies to A in SYRK for math_inst in math_instructions: tile_descriptions = [ TileDescription([256, 128, 16], 3, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 256, 16], 3, [2, 4, 1], math_inst, min_cc, max_cc), #TileDescription([256, 64, 16], 4, [4, 1, 1], math_inst, min_cc, max_cc), #TileDescription([ 64, 256, 16], 4, [1, 4, 1], math_inst, min_cc, max_cc), TileDescription([128, 128, 16], 5, [2, 2, 1], math_inst, min_cc, max_cc), #TileDescription([128, 64, 16], 6, [2, 2, 1], math_inst, min_cc, max_cc), #TileDescription([ 64, 128, 16], 6, [2, 2, 1], math_inst, min_cc, max_cc), #TileDescription([ 64, 64, 16], 10, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([256, 128, 32], 3, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 256, 32], 3, [2, 4, 1], math_inst, min_cc, max_cc), #TileDescription([256, 64, 32], 4, [4, 1, 1], math_inst, min_cc, max_cc), #TileDescription([ 64, 256, 32], 4, [1, 4, 1], math_inst, min_cc, max_cc), TileDescription([128, 128, 32], 4, [2, 2, 1], math_inst, min_cc, max_cc), #TileDescription([128, 64, 32], 3, [2, 2, 1], math_inst, min_cc, max_cc), #TileDescription([ 64, 128, 32], 3, [2, 2, 1], math_inst, min_cc, max_cc), #TileDescription([ 64, 64, 32], 5, [2, 2, 1], math_inst, min_cc, max_cc), ] data_type = [DataType.f32, DataType.f32, DataType.f32] CreateRankKOperator(manifest, layouts, fill_modes, tile_descriptions, \ data_type, alignment_constraints, BlasMode.symmetric) # # def GenerateSM80_TensorOp_1688_rank_k_complex(manifest, cuda_version): if not CudaToolkitVersionSatisfies(cuda_version, 11, 0): return layouts = [ (LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.ColumnMajor), ] fill_modes = [ FillMode.Lower, FillMode.Upper, ] math_instructions = [ MathInstruction( \ [16, 8, 8], \ DataType.tf32, DataType.tf32, DataType.f32, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add_complex), MathInstruction( \ [16, 8, 8], \ DataType.f32, DataType.f32, DataType.f32, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add_complex_fast_f32), ] min_cc = 80 max_cc = 1024 for math_inst in math_instructions: tile_descriptions = [ TileDescription([128, 64, 16], 4, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([64, 128, 16], 4, [2, 4, 1], math_inst, min_cc, max_cc), TileDescription([64, 64, 16], 4, [2, 2, 1], math_inst, min_cc, max_cc), #TileDescription([64, 32, 16], 4, [2, 1, 1], math_inst, min_cc, max_cc), #TileDescription([32, 32, 16], 4, [2, 2, 1], math_inst, min_cc, max_cc), ] data_type = [ DataType.cf32, DataType.cf32, DataType.cf32 ] alignment_constraints = [1,] # SYRK CreateRankKOperator(manifest, layouts, fill_modes, tile_descriptions, \ data_type, alignment_constraints, BlasMode.symmetric) # HERK CreateRankKOperator(manifest, layouts, fill_modes, tile_descriptions, \ data_type, alignment_constraints, BlasMode.hermitian) # # def GenerateSM80_TensorOp_1688_trmm(manifest, cuda_version): if not CudaToolkitVersionSatisfies(cuda_version, 11, 0): return layouts = [ (LayoutType.ColumnMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), ] side_modes = [ SideMode.Left, SideMode.Right, ] fill_modes = [ FillMode.Lower, FillMode.Upper, ] diag_types = [ DiagType.NonUnit, DiagType.Unit, ] math_instructions = [ MathInstruction( \ [16, 8, 8], \ DataType.tf32, DataType.tf32, DataType.f32, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add), MathInstruction( \ [16, 8, 8], \ DataType.f32, DataType.f32, DataType.f32, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add_fast_f32), ] min_cc = 80 max_cc = 1024 alignment_constraints = [1, 2, 4] for math_inst in math_instructions: tile_descriptions = [ TileDescription([256, 128, 16], 3, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 256, 16], 3, [2, 4, 1], math_inst, min_cc, max_cc), TileDescription([256, 64, 16], 4, [4, 1, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 256, 16], 4, [1, 4, 1], math_inst, min_cc, max_cc), TileDescription([128, 128, 16], 5, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 64, 16], 6, [2, 2, 1], math_inst, min_cc, max_cc), #TileDescription([ 64, 128, 16], 6, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 64, 16], 10, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([256, 128, 32], 3, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 256, 32], 3, [2, 4, 1], math_inst, min_cc, max_cc), #TileDescription([256, 64, 32], 4, [4, 1, 1], math_inst, min_cc, max_cc), #TileDescription([ 64, 256, 32], 4, [1, 4, 1], math_inst, min_cc, max_cc), TileDescription([128, 128, 32], 4, [2, 2, 1], math_inst, min_cc, max_cc), #TileDescription([128, 64, 32], 3, [2, 2, 1], math_inst, min_cc, max_cc), #TileDescription([ 64, 128, 32], 3, [2, 2, 1], math_inst, min_cc, max_cc), #TileDescription([ 64, 64, 32], 5, [2, 2, 1], math_inst, min_cc, max_cc), ] data_type = [DataType.f32, DataType.f32, DataType.f32, DataType.f32] CreateTrmmOperator(manifest, layouts, side_modes, fill_modes, diag_types, tile_descriptions, \ data_type, alignment_constraints) # # def GenerateSM80_TensorOp_1688_trmm_complex(manifest, cuda_version): if not CudaToolkitVersionSatisfies(cuda_version, 11, 0): return layouts = [ (LayoutType.ColumnMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), ] side_modes = [ SideMode.Left, SideMode.Right, ] fill_modes = [ FillMode.Lower, FillMode.Upper, ] diag_types = [ DiagType.NonUnit, DiagType.Unit, ] math_instructions = [ MathInstruction( \ [16, 8, 8], \ DataType.tf32, DataType.tf32, DataType.f32, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add_complex), MathInstruction( \ [16, 8, 8], \ DataType.f32, DataType.f32, DataType.f32, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add_complex_fast_f32), ] min_cc = 80 max_cc = 1024 for math_inst in math_instructions: tile_descriptions = [ TileDescription([128, 64, 16], 4, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([64, 128, 16], 4, [2, 4, 1], math_inst, min_cc, max_cc), TileDescription([64, 64, 16], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([64, 32, 16], 4, [2, 1, 1], math_inst, min_cc, max_cc), TileDescription([32, 32, 16], 4, [2, 2, 1], math_inst, min_cc, max_cc), ] data_type = [ DataType.cf32, DataType.cf32, DataType.cf32, DataType.cf32 ] alignment_constraints = [1,] complex_transforms = [ ComplexTransform.none, ComplexTransform.conj, ] CreateTrmmOperator(manifest, layouts, side_modes, fill_modes, diag_types, tile_descriptions, \ data_type, alignment_constraints, complex_transforms) # # def GenerateSM80_TensorOp_1688_symm(manifest, cuda_version): if not CudaToolkitVersionSatisfies(cuda_version, 11, 0): return # A and B have same layouts layouts = [ (LayoutType.ColumnMajor, LayoutType.ColumnMajor), ] side_modes = [ SideMode.Left, SideMode.Right, ] fill_modes = [ FillMode.Lower, FillMode.Upper, ] math_instructions = [ MathInstruction( \ [16, 8, 8], \ DataType.tf32, DataType.tf32, DataType.f32, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add), MathInstruction( \ [16, 8, 8], \ DataType.f32, DataType.f32, DataType.f32, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add_fast_f32), ] min_cc = 80 max_cc = 1024 alignment_constraints = [ 1, 2, 4 ] for math_inst in math_instructions: tile_descriptions = [ TileDescription([256, 128, 16], 3, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 256, 16], 3, [2, 4, 1], math_inst, min_cc, max_cc), #TileDescription([256, 64, 16], 4, [4, 1, 1], math_inst, min_cc, max_cc), #TileDescription([ 64, 256, 16], 4, [1, 4, 1], math_inst, min_cc, max_cc), TileDescription([128, 128, 16], 5, [2, 2, 1], math_inst, min_cc, max_cc), #TileDescription([128, 64, 16], 6, [2, 2, 1], math_inst, min_cc, max_cc), #TileDescription([ 64, 128, 16], 6, [2, 2, 1], math_inst, min_cc, max_cc), #TileDescription([ 64, 64, 16], 10, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([256, 128, 32], 3, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 256, 32], 3, [2, 4, 1], math_inst, min_cc, max_cc), #TileDescription([256, 64, 32], 4, [4, 1, 1], math_inst, min_cc, max_cc), #TileDescription([ 64, 256, 32], 4, [1, 4, 1], math_inst, min_cc, max_cc), TileDescription([128, 128, 32], 4, [2, 2, 1], math_inst, min_cc, max_cc), #TileDescription([128, 64, 32], 3, [2, 2, 1], math_inst, min_cc, max_cc), #TileDescription([ 64, 128, 32], 3, [2, 2, 1], math_inst, min_cc, max_cc), #TileDescription([ 64, 64, 32], 5, [2, 2, 1], math_inst, min_cc, max_cc), ] data_type = [DataType.f32, DataType.f32, DataType.f32, DataType.f32] CreateSymmOperator(manifest, layouts, side_modes, fill_modes, tile_descriptions, \ data_type, alignment_constraints, BlasMode.symmetric) # # def GenerateSM80_TensorOp_1688_symm_complex(manifest, cuda_version): if not CudaToolkitVersionSatisfies(cuda_version, 11, 0): return layouts = [ (LayoutType.ColumnMajor, LayoutType.ColumnMajor), ] side_modes = [ SideMode.Left, SideMode.Right, ] fill_modes = [ FillMode.Lower, FillMode.Upper, ] math_instructions = [ MathInstruction( \ [16, 8, 8], \ DataType.tf32, DataType.tf32, DataType.f32, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add_complex), MathInstruction( \ [16, 8, 8], \ DataType.f32, DataType.f32, DataType.f32, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add_complex_fast_f32), ] min_cc = 80 max_cc = 1024 for math_inst in math_instructions: tile_descriptions = [ TileDescription([128, 64, 16], 4, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([64, 128, 16], 4, [2, 4, 1], math_inst, min_cc, max_cc), TileDescription([64, 64, 16], 4, [2, 2, 1], math_inst, min_cc, max_cc), #TileDescription([64, 32, 16], 4, [2, 1, 1], math_inst, min_cc, max_cc), #TileDescription([32, 32, 16], 4, [2, 2, 1], math_inst, min_cc, max_cc), ] data_type = [ DataType.cf32, DataType.cf32, DataType.cf32, DataType.cf32 ] alignment_constraints = [1,] # SYMM CreateSymmOperator(manifest, layouts, side_modes, fill_modes, tile_descriptions, \ data_type, alignment_constraints, BlasMode.symmetric) # HEMM CreateSymmOperator(manifest, layouts, side_modes, fill_modes, tile_descriptions, \ data_type, alignment_constraints, BlasMode.hermitian) # # def GenerateSM80_TensorOp_884(manifest, cuda_version): if not CudaToolkitVersionSatisfies(cuda_version, 11, 0): return layouts = [ (LayoutType.ColumnMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.ColumnMajor, LayoutType.RowMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.RowMajor, LayoutType.ColumnMajor), ] math_inst = \ MathInstruction( \ [8, 8, 4], \ DataType.f64, DataType.f64, DataType.f64, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add) min_cc = 80 max_cc = 1024 alignment_constraints = [1,] tile_descriptions = [ TileDescription([128, 128, 16], 3, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([256, 64, 16], 3, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([64, 256, 16], 3, [2, 4, 1], math_inst, min_cc, max_cc), TileDescription([256, 32, 16], 3, [4, 1, 1], math_inst, min_cc, max_cc), TileDescription([32, 256, 16], 3, [1, 4, 1], math_inst, min_cc, max_cc), TileDescription([128, 64, 16], 3, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([64, 128, 16], 3, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([64, 64, 16], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([64, 32, 16], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([32, 64, 16], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([32, 32, 16], 5, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([16, 32, 16], 5, [1, 2, 1], math_inst, min_cc, max_cc), TileDescription([32, 16, 16], 5, [2, 1, 1], math_inst, min_cc, max_cc), ] data_type = [DataType.f64, DataType.f64, DataType.f64, DataType.f64] CreateGemmOperator(manifest, layouts, tile_descriptions, \ data_type, alignment_constraints) # # def GenerateSM80_TensorOp_884_complex(manifest, cuda_version): if not CudaToolkitVersionSatisfies(cuda_version, 11, 0): return layouts = [ (LayoutType.ColumnMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.ColumnMajor, LayoutType.RowMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.RowMajor, LayoutType.ColumnMajor), ] math_inst = \ MathInstruction( \ [8, 8, 4], \ DataType.f64, DataType.f64, DataType.f64, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add_complex) min_cc = 80 max_cc = 1024 alignment_constraints = [1,] tile_descriptions = [ TileDescription([128, 64, 8 ], 3, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([64, 128, 8 ], 3, [2, 4, 1], math_inst, min_cc, max_cc), TileDescription([64, 64, 8 ], 3, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([64, 32, 8 ], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([32, 64, 8 ], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([32, 32, 8 ], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([16, 32, 8 ], 4, [1, 2, 1], math_inst, min_cc, max_cc), TileDescription([32, 16, 8 ], 4, [2, 1, 1], math_inst, min_cc, max_cc), TileDescription([128, 64, 16], 3, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([64, 128, 16], 3, [2, 4, 1], math_inst, min_cc, max_cc), TileDescription([64, 64, 16], 3, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([64, 32, 16], 3, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([32, 64, 16], 3, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([32, 32, 16], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([16, 32, 16], 4, [1, 2, 1], math_inst, min_cc, max_cc), TileDescription([32, 16, 16], 3, [2, 1, 1], math_inst, min_cc, max_cc), ] data_type = [DataType.cf64, DataType.cf64, DataType.cf64, DataType.cf64] complex_transforms = [ (ComplexTransform.none, ComplexTransform.none), (ComplexTransform.conj, ComplexTransform.none), (ComplexTransform.none, ComplexTransform.conj), (ComplexTransform.conj, ComplexTransform.conj) ] CreateGemmOperator(manifest, layouts, tile_descriptions, \ data_type, alignment_constraints, complex_transforms) # def GenerateSM80_TensorOp_884_complex_gaussian(manifest, cuda_version): if not CudaToolkitVersionSatisfies(cuda_version, 11, 0): return layouts = [ (LayoutType.ColumnMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.ColumnMajor, LayoutType.RowMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.RowMajor, LayoutType.ColumnMajor), ] math_inst = \ MathInstruction( \ [8, 8, 4], \ DataType.f64, DataType.f64, DataType.f64, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add_complex_gaussian) min_cc = 80 max_cc = 1024 alignment_constraints = [1,] tile_descriptions = [ TileDescription([64, 64, 8], 3, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([64, 32, 8], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([32, 64, 8], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([32, 32, 8], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([16, 32, 8], 4, [1, 2, 1], math_inst, min_cc, max_cc), TileDescription([32, 16, 8], 4, [2, 1, 1], math_inst, min_cc, max_cc), ] data_type = [DataType.cf64, DataType.cf64, DataType.cf64, DataType.cf64] complex_transforms = [ (ComplexTransform.none, ComplexTransform.none), (ComplexTransform.conj, ComplexTransform.none), (ComplexTransform.none, ComplexTransform.conj), (ComplexTransform.conj, ComplexTransform.conj) ] CreateGemmOperator(manifest, layouts, tile_descriptions, \ data_type, alignment_constraints, complex_transforms) # # def GenerateSM80_TensorOp_884_rank_k(manifest, cuda_version): if not CudaToolkitVersionSatisfies(cuda_version, 11, 0): return layouts = [ (LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.ColumnMajor), ] fill_modes = [ FillMode.Lower, FillMode.Upper, ] math_inst = \ MathInstruction( \ [8, 8, 4], \ DataType.f64, DataType.f64, DataType.f64, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add) min_cc = 80 max_cc = 1024 alignment_constraints = [1,] tile_descriptions = [ TileDescription([128, 128, 16], 3, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([64, 128, 16], 3, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 64, 16], 3, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([64, 64, 16], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([64, 32, 16], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([32, 64, 16], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([32, 32, 16], 5, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([16, 32, 16], 5, [1, 2, 1], math_inst, min_cc, max_cc), TileDescription([32, 16, 16], 5, [2, 1, 1], math_inst, min_cc, max_cc), ] data_type = [DataType.f64, DataType.f64, DataType.f64] CreateRankKOperator(manifest, layouts, fill_modes, tile_descriptions, \ data_type, alignment_constraints, BlasMode.symmetric) # # def GenerateSM80_TensorOp_884_rank_k_complex(manifest, cuda_version): if not CudaToolkitVersionSatisfies(cuda_version, 11, 0): return layouts = [ (LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.ColumnMajor), ] fill_modes = [ FillMode.Lower, FillMode.Upper, ] math_inst = \ MathInstruction( \ [8, 8, 4], \ DataType.f64, DataType.f64, DataType.f64, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add_complex) min_cc = 80 max_cc = 1024 alignment_constraints = [1,] tile_descriptions = [ TileDescription([128, 64, 8], 3, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([64, 128, 8], 3, [2, 4, 1], math_inst, min_cc, max_cc), TileDescription([64, 64, 8], 3, [2, 2, 1], math_inst, min_cc, max_cc), #TileDescription([64, 32, 8], 4, [2, 2, 1], math_inst, min_cc, max_cc), #TileDescription([32, 64, 8], 4, [2, 2, 1], math_inst, min_cc, max_cc), #TileDescription([32, 32, 8], 4, [2, 2, 1], math_inst, min_cc, max_cc), #TileDescription([16, 32, 8], 4, [1, 2, 1], math_inst, min_cc, max_cc), #TileDescription([32, 16, 8], 4, [2, 1, 1], math_inst, min_cc, max_cc), ] data_type = [DataType.cf64, DataType.cf64, DataType.cf64] # SYRK computation CreateRankKOperator(manifest, layouts, fill_modes, tile_descriptions, \ data_type, alignment_constraints, BlasMode.symmetric) # HERK computation CreateRankKOperator(manifest, layouts, fill_modes, tile_descriptions, \ data_type, alignment_constraints, BlasMode.hermitian) # # def GenerateSM80_TensorOp_884_rank_k_complex_gaussian(manifest, cuda_version): if not CudaToolkitVersionSatisfies(cuda_version, 11, 0): return layouts = [ (LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.ColumnMajor), ] fill_modes = [ FillMode.Lower, FillMode.Upper, ] math_inst = \ MathInstruction( \ [8, 8, 4], \ DataType.f64, DataType.f64, DataType.f64, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add_complex_gaussian) min_cc = 80 max_cc = 1024 alignment_constraints = [1,] tile_descriptions = [ TileDescription([64, 64, 8], 3, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([64, 32, 8], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([32, 64, 8], 4, [2, 2, 1], math_inst, min_cc, max_cc), #TileDescription([32, 32, 8], 4, [2, 2, 1], math_inst, min_cc, max_cc), #TileDescription([16, 32, 8], 4, [1, 2, 1], math_inst, min_cc, max_cc), #TileDescription([32, 16, 8], 4, [2, 1, 1], math_inst, min_cc, max_cc), ] data_type = [DataType.cf64, DataType.cf64, DataType.cf64] complex_transforms = [ComplexTransform.none,] # SYRK computation CreateRankKOperator(manifest, layouts, fill_modes, tile_descriptions, \ data_type, alignment_constraints, BlasMode.symmetric) # HERK computation CreateRankKOperator(manifest, layouts, fill_modes, tile_descriptions, \ data_type, alignment_constraints, BlasMode.hermitian) # # def GenerateSM80_TensorOp_884_trmm(manifest, cuda_version): if not CudaToolkitVersionSatisfies(cuda_version, 11, 0): return layouts = [ (LayoutType.ColumnMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), ] side_modes = [ SideMode.Left, SideMode.Right, ] fill_modes = [ FillMode.Lower, FillMode.Upper, ] diag_types = [ DiagType.NonUnit, DiagType.Unit, ] math_inst = \ MathInstruction( \ [8, 8, 4], \ DataType.f64, DataType.f64, DataType.f64, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add) min_cc = 80 max_cc = 1024 alignment_constraints = [1,] tile_descriptions = [ TileDescription([128, 128, 16], 3, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([64, 128, 16], 3, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 64, 16], 3, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([64, 64, 16], 4, [2, 2, 1], math_inst, min_cc, max_cc), ] data_type = [DataType.f64, DataType.f64, DataType.f64, DataType.f64] CreateTrmmOperator(manifest, layouts, side_modes, fill_modes, diag_types, tile_descriptions, \ data_type, alignment_constraints) # # def GenerateSM80_TensorOp_884_trmm_complex(manifest, cuda_version): if not CudaToolkitVersionSatisfies(cuda_version, 11, 0): return layouts = [ (LayoutType.ColumnMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), ] side_modes = [ SideMode.Left, SideMode.Right, ] fill_modes = [ FillMode.Lower, FillMode.Upper, ] diag_types = [ DiagType.NonUnit, DiagType.Unit, ] math_inst = \ MathInstruction( \ [8, 8, 4], \ DataType.f64, DataType.f64, DataType.f64, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add_complex) min_cc = 80 max_cc = 1024 alignment_constraints = [1,] tile_descriptions = [ TileDescription([128, 64, 8], 3, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([64, 128, 8], 3, [2, 4, 1], math_inst, min_cc, max_cc), TileDescription([64, 64, 8], 3, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([64, 32, 8], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([32, 64, 8], 4, [2, 2, 1], math_inst, min_cc, max_cc), ] data_type = [DataType.cf64, DataType.cf64, DataType.cf64, DataType.cf64] complex_transforms = [ ComplexTransform.none, ComplexTransform.conj, ] CreateTrmmOperator(manifest, layouts, side_modes, fill_modes, diag_types, tile_descriptions, \ data_type, alignment_constraints, complex_transforms) # # def GenerateSM80_TensorOp_884_trmm_complex_gaussian(manifest, cuda_version): if not CudaToolkitVersionSatisfies(cuda_version, 11, 0): return layouts = [ (LayoutType.ColumnMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), ] side_modes = [ SideMode.Left, SideMode.Right, ] fill_modes = [ FillMode.Lower, FillMode.Upper, ] diag_types = [ DiagType.NonUnit, DiagType.Unit, ] math_inst = \ MathInstruction( \ [8, 8, 4], \ DataType.f64, DataType.f64, DataType.f64, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add_complex_gaussian) min_cc = 80 max_cc = 1024 alignment_constraints = [1,] tile_descriptions = [ TileDescription([64, 64, 8], 3, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([64, 32, 8], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([32, 64, 8], 4, [2, 2, 1], math_inst, min_cc, max_cc), ] data_type = [DataType.cf64, DataType.cf64, DataType.cf64, DataType.cf64] complex_transforms = [ ComplexTransform.none, ComplexTransform.conj, ] CreateTrmmOperator(manifest, layouts, side_modes, fill_modes, diag_types, tile_descriptions, \ data_type, alignment_constraints, complex_transforms) # # def GenerateSM80_TensorOp_884_symm(manifest, cuda_version): if not CudaToolkitVersionSatisfies(cuda_version, 11, 0): return layouts = [ (LayoutType.ColumnMajor, LayoutType.ColumnMajor), ] side_modes = [ SideMode.Left, SideMode.Right, ] fill_modes = [ FillMode.Lower, FillMode.Upper, ] math_inst = \ MathInstruction( \ [8, 8, 4], \ DataType.f64, DataType.f64, DataType.f64, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add) min_cc = 80 max_cc = 1024 alignment_constraints = [1,] tile_descriptions = [ TileDescription([128, 128, 16], 3, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([64, 128, 16], 3, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 64, 16], 3, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([64, 64, 16], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([64, 32, 16], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([32, 64, 16], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([32, 32, 16], 5, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([16, 32, 16], 5, [1, 2, 1], math_inst, min_cc, max_cc), TileDescription([32, 16, 16], 5, [2, 1, 1], math_inst, min_cc, max_cc), ] data_type = [DataType.f64, DataType.f64, DataType.f64, DataType.f64] CreateSymmOperator(manifest, layouts, side_modes, fill_modes, tile_descriptions, \ data_type, alignment_constraints, BlasMode.symmetric) # # def GenerateSM80_TensorOp_884_symm_complex(manifest, cuda_version): if not CudaToolkitVersionSatisfies(cuda_version, 11, 0): return layouts = [ (LayoutType.ColumnMajor, LayoutType.ColumnMajor), ] side_modes = [ SideMode.Left, SideMode.Right, ] fill_modes = [ FillMode.Lower, FillMode.Upper, ] math_inst = \ MathInstruction( \ [8, 8, 4], \ DataType.f64, DataType.f64, DataType.f64, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add_complex) min_cc = 80 max_cc = 1024 alignment_constraints = [1,] tile_descriptions = [ TileDescription([128, 64, 8], 3, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([64, 128, 8], 3, [2, 4, 1], math_inst, min_cc, max_cc), TileDescription([64, 64, 8], 3, [2, 2, 1], math_inst, min_cc, max_cc), #TileDescription([64, 32, 8], 4, [2, 2, 1], math_inst, min_cc, max_cc), #TileDescription([32, 64, 8], 4, [2, 2, 1], math_inst, min_cc, max_cc), #TileDescription([32, 32, 8], 4, [2, 2, 1], math_inst, min_cc, max_cc), #TileDescription([16, 32, 8], 4, [1, 2, 1], math_inst, min_cc, max_cc), #TileDescription([32, 16, 8], 4, [2, 1, 1], math_inst, min_cc, max_cc), ] data_type = [DataType.cf64, DataType.cf64, DataType.cf64, DataType.cf64] # SYMM computation CreateSymmOperator(manifest, layouts, side_modes, fill_modes, tile_descriptions, \ data_type, alignment_constraints, BlasMode.symmetric) # HEMM computation CreateSymmOperator(manifest, layouts, side_modes, fill_modes, tile_descriptions, \ data_type, alignment_constraints, BlasMode.hermitian) # # def GenerateSM80_TensorOp_884_symm_complex_gaussian(manifest, cuda_version): if not CudaToolkitVersionSatisfies(cuda_version, 11, 0): return layouts = [ (LayoutType.ColumnMajor, LayoutType.ColumnMajor), ] side_modes = [ SideMode.Left, SideMode.Right, ] fill_modes = [ FillMode.Lower, FillMode.Upper, ] math_inst = \ MathInstruction( \ [8, 8, 4], \ DataType.f64, DataType.f64, DataType.f64, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add_complex_gaussian) min_cc = 80 max_cc = 1024 alignment_constraints = [1,] tile_descriptions = [ TileDescription([64, 64, 8], 3, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([64, 32, 8], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([32, 64, 8], 4, [2, 2, 1], math_inst, min_cc, max_cc), #TileDescription([32, 32, 8], 4, [2, 2, 1], math_inst, min_cc, max_cc), #TileDescription([16, 32, 8], 4, [1, 2, 1], math_inst, min_cc, max_cc), #TileDescription([32, 16, 8], 4, [2, 1, 1], math_inst, min_cc, max_cc), ] data_type = [DataType.cf64, DataType.cf64, DataType.cf64, DataType.cf64] complex_transforms = [ComplexTransform.none,] # SYMM computation CreateSymmOperator(manifest, layouts, side_modes, fill_modes, tile_descriptions, \ data_type, alignment_constraints, BlasMode.symmetric) # HEMM computation CreateSymmOperator(manifest, layouts, side_modes, fill_modes, tile_descriptions, \ data_type, alignment_constraints, BlasMode.hermitian) # ################################################################################################### # def GenerateSM80_Simt_f32(manifest, cuda_version): layouts = [ (LayoutType.ColumnMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.ColumnMajor, LayoutType.RowMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.RowMajor, LayoutType.ColumnMajor), ] math_instructions = [ MathInstruction( \ [1, 1, 1], \ DataType.f32, DataType.f32, DataType.f32, \ OpcodeClass.Simt, \ MathOperation.multiply_add), ] min_cc = 80 max_cc = 1024 alignment_constraints = [1,] for math_inst in math_instructions: tile_descriptions = [ TileDescription([256, 128, 8], 5, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 256, 8], 5, [2, 4, 1], math_inst, min_cc, max_cc), TileDescription([128, 128, 8], 5, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([256, 128, 8], 4, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 256, 8], 4, [2, 4, 1], math_inst, min_cc, max_cc), TileDescription([128, 128, 8], 4, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 64, 8], 5, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 128, 8], 5, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 64, 8], 5, [2, 1, 1], math_inst, min_cc, max_cc), TileDescription([128, 32, 8], 5, [2, 1, 1], math_inst, min_cc, max_cc), TileDescription([ 32, 128, 8], 5, [1, 2, 1], math_inst, min_cc, max_cc), ] data_type = [ math_inst.element_a, math_inst.element_b, math_inst.element_accumulator, math_inst.element_accumulator, ] CreateGemmOperator(manifest, layouts, tile_descriptions, \ data_type, alignment_constraints) conv_layout = (LayoutType.TensorNHWC, LayoutType.TensorNHWC, LayoutType.TensorNHWC) CreateConv2dOperator(manifest, conv_layout, tile_descriptions, data_type, alignment_constraints) # # def GenerateSM80_Simt_f64(manifest, cuda_version): layouts = [ (LayoutType.ColumnMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.ColumnMajor, LayoutType.RowMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.RowMajor, LayoutType.ColumnMajor), ] math_instructions = [ MathInstruction( \ [1, 1, 1], \ DataType.f64, DataType.f64, DataType.f64, \ OpcodeClass.Simt, \ MathOperation.multiply_add), ] min_cc = 80 max_cc = 1024 alignment_constraints = [1,] for math_inst in math_instructions: tile_descriptions = [ TileDescription([128, 128, 8], 3, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 64, 8], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 128, 8], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 64, 8], 5, [2, 1, 1], math_inst, min_cc, max_cc), TileDescription([128, 32, 8], 5, [2, 1, 1], math_inst, min_cc, max_cc), TileDescription([ 32, 128, 8], 5, [1, 2, 1], math_inst, min_cc, max_cc), ] data_type = [ math_inst.element_a, math_inst.element_b, math_inst.element_accumulator, math_inst.element_accumulator, ] CreateGemmOperator(manifest, layouts, tile_descriptions, \ data_type, alignment_constraints) # ################################################################################################## # def GenerateSM80_Simt_complex(manifest, cuda_version): math_instructions = [ MathInstruction( \ [1, 1, 1], \ DataType.f32, DataType.f32, DataType.f32, \ OpcodeClass.Simt, \ MathOperation.multiply_add_complex), ] min_cc = 80 max_cc = 1024 alignment_constraints = [1,] data_type = [ DataType.cf32, DataType.cf32, DataType.cf32, DataType.cf32 ] layouts = [ (LayoutType.ColumnMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.ColumnMajor, LayoutType.RowMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.RowMajor, LayoutType.ColumnMajor), ] complex_transforms = [ (ComplexTransform.none, ComplexTransform.none), (ComplexTransform.conj, ComplexTransform.none), (ComplexTransform.none, ComplexTransform.conj), (ComplexTransform.conj, ComplexTransform.conj) ] for math_inst in math_instructions: tile_descriptions = [ TileDescription([128, 128, 8], 5, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 128, 8], 4, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([64, 64, 8], 3, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([ 64, 128, 16], 6, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 64, 16], 6, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([64, 32, 16], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([32, 64, 16], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([32, 32, 16], 5, [2, 2, 1], math_inst, min_cc, max_cc), ] CreateGemmOperator(manifest, layouts, tile_descriptions, data_type, alignment_constraints, complex_transforms) conv_layout = (LayoutType.TensorNHWC, LayoutType.TensorNHWC, LayoutType.TensorNHWC) CreateConv2dOperator(manifest, conv_layout, tile_descriptions, data_type, alignment_constraints) # ################################################################################################### # def GenerateSM80(manifest, cuda_version): GenerateSM80_TensorOp_16816(manifest, cuda_version) GenerateSM80_SparseTensorOp_16832(manifest, cuda_version) GenerateSM80_PlanarComplexTensorOp_16816(manifest, cuda_version) GenerateSM80_TensorOp_1688(manifest, cuda_version) GenerateSM80_TensorOp_1688_fast_math(manifest, cuda_version) GenerateSM80_SparseTensorOp_16816_fast_math(manifest, cuda_version) GenerateSM80_TensorOp_1688_complex(manifest, cuda_version) # 3xTF32 GenerateSM80_TensorOp_1688_fast_fp32_math(manifest, cuda_version) GenerateSM80_TensorOp_1688_fast_fp32_math_complex(manifest, cuda_version) GenerateSM80_TensorOp_1688_rank_k(manifest, cuda_version) GenerateSM80_TensorOp_1688_rank_k_complex(manifest, cuda_version) GenerateSM80_TensorOp_1688_trmm(manifest, cuda_version) GenerateSM80_TensorOp_1688_trmm_complex(manifest, cuda_version) GenerateSM80_TensorOp_1688_symm(manifest, cuda_version) GenerateSM80_TensorOp_1688_symm_complex(manifest, cuda_version) GenerateSM80_TensorOp_884(manifest, cuda_version) GenerateSM80_TensorOp_884_complex(manifest, cuda_version) GenerateSM80_TensorOp_884_complex_gaussian(manifest, cuda_version) GenerateSM80_TensorOp_884_rank_k(manifest, cuda_version) GenerateSM80_TensorOp_884_rank_k_complex(manifest, cuda_version) GenerateSM80_TensorOp_884_rank_k_complex_gaussian(manifest, cuda_version) GenerateSM80_TensorOp_884_trmm(manifest, cuda_version) GenerateSM80_TensorOp_884_trmm_complex(manifest, cuda_version) GenerateSM80_TensorOp_884_trmm_complex_gaussian(manifest, cuda_version) GenerateSM80_TensorOp_884_symm(manifest, cuda_version) GenerateSM80_TensorOp_884_symm_complex(manifest, cuda_version) GenerateSM80_TensorOp_884_symm_complex_gaussian(manifest, cuda_version) GenerateSM80_TensorOp_16832_TN(manifest, cuda_version) GenerateSM80_SparseTensorOp_16864_TN(manifest, cuda_version) GenerateSM80_TensorOp_16832_Interleaved(manifest, cuda_version) GenerateSM80_TensorOp_16864_TN(manifest, cuda_version) GenerateSM80_SparseTensorOp_168128_TN(manifest, cuda_version) GenerateSM80_TensorOp_16864_Interleaved(manifest, cuda_version) GenerateSM80_TensorOp_168256(manifest, cuda_version) GenerateSM80_Simt_f32(manifest, cuda_version) GenerateSM80_Simt_f64(manifest, cuda_version) GenerateSM80_Simt_complex(manifest, cuda_version) ################################################################################################### # def GenerateSM90_TensorOp_16b_WGMMA_gemm(manifest, cuda_version): if not CudaToolkitVersionSatisfies(cuda_version, 12, 0): return # layouts for ABC and their alignments. layouts = [ [[LayoutType.ColumnMajor, 8], [LayoutType.ColumnMajor, 8], [LayoutType.ColumnMajor, 1]], [[LayoutType.ColumnMajor, 8], [LayoutType.RowMajor, 8], [LayoutType.ColumnMajor, 1]], [[LayoutType.RowMajor, 8], [LayoutType.ColumnMajor, 8], [LayoutType.ColumnMajor, 1]], [[LayoutType.RowMajor, 8], [LayoutType.RowMajor, 8], [LayoutType.ColumnMajor, 1]], ] math_instructions = [ MathInstruction( [64, 128, 16], DataType.f16, DataType.f16, DataType.f16, OpcodeClass.TensorOp, MathOperation.multiply_add), MathInstruction( [64, 128, 16], DataType.f16, DataType.f16, DataType.f32, OpcodeClass.TensorOp, MathOperation.multiply_add), MathInstruction( [64, 128, 16], DataType.bf16, DataType.bf16, DataType.f32, OpcodeClass.TensorOp, MathOperation.multiply_add), ] min_cc = 90 max_cc = 90 for math_inst in math_instructions: tile_descriptions = [ TileDescription([128, math_inst.instruction_shape[1], math_inst.instruction_shape[2]*4], 0, [4, 1, 1], math_inst, min_cc, max_cc, [2,1,1]), TileDescription([ 64, math_inst.instruction_shape[1], math_inst.instruction_shape[2]*4], 0, [4, 1, 1], math_inst, min_cc, max_cc, [2,1,1]), TileDescription([128, math_inst.instruction_shape[1], math_inst.instruction_shape[2]*4], 0, [4, 1, 1], math_inst, min_cc, max_cc, [1,2,1]), TileDescription([ 64, math_inst.instruction_shape[1], math_inst.instruction_shape[2]*4], 0, [4, 1, 1], math_inst, min_cc, max_cc, [1,2,1]), TileDescription([128, math_inst.instruction_shape[1], math_inst.instruction_shape[2]*4], 0, [4, 1, 1], math_inst, min_cc, max_cc, [1,1,1]), TileDescription([ 64, math_inst.instruction_shape[1], math_inst.instruction_shape[2]*4], 0, [4, 1, 1], math_inst, min_cc, max_cc, [1,1,1]), ] data_type = [ math_inst.element_a, math_inst.element_b, math_inst.element_accumulator, math_inst.element_accumulator, ] CreateGemmUniversal3xOperator(manifest, layouts, tile_descriptions, data_type) # for mixed precision kernels, also generate kernels that write output matrix in the A/B format # Avoid emitting two kernels if the accumulator type does not differ from the input type (e.g. F16 accumulation) if math_inst.element_a != math_inst.element_accumulator: data_type_mixed = [ math_inst.element_a, math_inst.element_b, math_inst.element_a, math_inst.element_accumulator, ] CreateGemmUniversal3xOperator(manifest, layouts, tile_descriptions, data_type_mixed) # def GenerateSM90_TensorOp_tf32_WGMMA_gemm(manifest, cuda_version): if not CudaToolkitVersionSatisfies(cuda_version, 12, 0): return # layouts for ABC and their alignments layouts_tf32 = [ [[LayoutType.ColumnMajor, 1], [LayoutType.ColumnMajor, 4], [LayoutType.ColumnMajor, 1]], [[LayoutType.ColumnMajor, 1], [LayoutType.RowMajor, 1], [LayoutType.ColumnMajor, 1]], [[LayoutType.RowMajor, 4], [LayoutType.ColumnMajor, 4], [LayoutType.ColumnMajor, 1]], [[LayoutType.RowMajor, 4], [LayoutType.RowMajor, 1], [LayoutType.ColumnMajor, 1]], ] math_inst = MathInstruction( [64, 128, 8], DataType.tf32, DataType.tf32, DataType.f32, OpcodeClass.TensorOp, MathOperation.multiply_add) min_cc = 90 max_cc = 90 tile_descriptions = [ TileDescription([128, math_inst.instruction_shape[1], math_inst.instruction_shape[2]*4], 0, [4, 1, 1], math_inst, min_cc, max_cc, [2,1,1]), TileDescription([ 64, math_inst.instruction_shape[1], math_inst.instruction_shape[2]*4], 0, [4, 1, 1], math_inst, min_cc, max_cc, [2,1,1]), TileDescription([128, math_inst.instruction_shape[1], math_inst.instruction_shape[2]*4], 0, [4, 1, 1], math_inst, min_cc, max_cc, [1,2,1]), TileDescription([ 64, math_inst.instruction_shape[1], math_inst.instruction_shape[2]*4], 0, [4, 1, 1], math_inst, min_cc, max_cc, [1,2,1]), TileDescription([128, math_inst.instruction_shape[1], math_inst.instruction_shape[2]*4], 0, [4, 1, 1], math_inst, min_cc, max_cc, [1,1,1]), TileDescription([ 64, math_inst.instruction_shape[1], math_inst.instruction_shape[2]*4], 0, [4, 1, 1], math_inst, min_cc, max_cc, [1,1,1]), ] data_type_tf32 = [ math_inst.element_a, math_inst.element_b, math_inst.element_accumulator, math_inst.element_accumulator, ] CreateGemmUniversal3xOperator(manifest, layouts_tf32, tile_descriptions, data_type_tf32) # F32 kernel, TN only supported for now layouts_f32 = [layouts_tf32[2]] data_type_f32 = [ DataType.f32, DataType.f32, math_inst.element_accumulator, DataType.f32, ] CreateGemmUniversal3xOperator(manifest, layouts_f32, tile_descriptions, data_type_f32) def GenerateSM90_TensorOp_int8_WGMMA_gemm(manifest, cuda_version): if not CudaToolkitVersionSatisfies(cuda_version, 12, 0): return # layouts for ABC and their alignments layouts = [ [[LayoutType.RowMajor, 16], [LayoutType.ColumnMajor, 16], [LayoutType.ColumnMajor, 1]], ] math_instructions = [ MathInstruction( [64, 128, 32], DataType.s8, DataType.s8, DataType.s32, OpcodeClass.TensorOp, MathOperation.multiply_add), MathInstruction( [64, 128, 32], DataType.u8, DataType.u8, DataType.s32, OpcodeClass.TensorOp, MathOperation.multiply_add), ] min_cc = 90 max_cc = 90 for math_inst in math_instructions: tile_descriptions = [ TileDescription([128, math_inst.instruction_shape[1], math_inst.instruction_shape[2]*4], 0, [4, 1, 1], math_inst, min_cc, max_cc, [2,1,1]), TileDescription([ 64, math_inst.instruction_shape[1], math_inst.instruction_shape[2]*4], 0, [4, 1, 1], math_inst, min_cc, max_cc, [2,1,1]), TileDescription([128, math_inst.instruction_shape[1], math_inst.instruction_shape[2]*4], 0, [4, 1, 1], math_inst, min_cc, max_cc, [1,2,1]), TileDescription([ 64, math_inst.instruction_shape[1], math_inst.instruction_shape[2]*4], 0, [4, 1, 1], math_inst, min_cc, max_cc, [1,2,1]), TileDescription([128, math_inst.instruction_shape[1], math_inst.instruction_shape[2]*4], 0, [4, 1, 1], math_inst, min_cc, max_cc, [1,1,1]), TileDescription([ 64, math_inst.instruction_shape[1], math_inst.instruction_shape[2]*4], 0, [4, 1, 1], math_inst, min_cc, max_cc, [1,1,1]), ] data_type = [ math_inst.element_a, math_inst.element_b, math_inst.element_accumulator, math_inst.element_accumulator, ] CreateGemmUniversal3xOperator(manifest, layouts, tile_descriptions, data_type) # def GenerateSM90_TensorOp_1684(manifest, cuda_version): if not CudaToolkitVersionSatisfies(cuda_version, 11, 8): return layouts = [ (LayoutType.ColumnMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.ColumnMajor, LayoutType.RowMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.RowMajor, LayoutType.ColumnMajor), ] math_inst = MathInstruction( [16, 8, 4], DataType.f64, DataType.f64, DataType.f64, OpcodeClass.TensorOp, MathOperation.multiply_add) min_cc = 90 max_cc = 1024 alignment_constraints = [1,] tile_descriptions = [ TileDescription([128, 128, 16], 3, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([256, 64, 16], 3, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([64, 256, 16], 3, [2, 4, 1], math_inst, min_cc, max_cc), TileDescription([256, 32, 16], 3, [4, 1, 1], math_inst, min_cc, max_cc), TileDescription([32, 256, 16], 3, [1, 4, 1], math_inst, min_cc, max_cc), TileDescription([128, 64, 16], 3, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([64, 128, 16], 3, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([64, 64, 16], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([64, 32, 16], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([32, 64, 16], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([32, 32, 16], 5, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([16, 32, 16], 5, [1, 2, 1], math_inst, min_cc, max_cc), TileDescription([32, 16, 16], 5, [2, 1, 1], math_inst, min_cc, max_cc), ] data_type = [DataType.f64, DataType.f64, DataType.f64, DataType.f64] CreateGemmOperator(manifest, layouts, tile_descriptions, data_type, alignment_constraints) # # def GenerateSM90_TensorOp_1684_complex(manifest, cuda_version): if not CudaToolkitVersionSatisfies(cuda_version, 11, 8): return layouts = [ (LayoutType.ColumnMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.ColumnMajor, LayoutType.RowMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.RowMajor, LayoutType.ColumnMajor), ] math_inst = \ MathInstruction( \ [16, 8, 4], \ DataType.f64, DataType.f64, DataType.f64, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add_complex) min_cc = 90 max_cc = 1024 alignment_constraints = [1,] tile_descriptions = [ TileDescription([128, 64, 8 ], 3, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([64, 128, 8 ], 3, [2, 4, 1], math_inst, min_cc, max_cc), TileDescription([64, 64, 8 ], 3, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([64, 32, 8 ], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([32, 64, 8 ], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([32, 32, 8 ], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([16, 32, 8 ], 4, [1, 2, 1], math_inst, min_cc, max_cc), TileDescription([32, 16, 8 ], 4, [2, 1, 1], math_inst, min_cc, max_cc), TileDescription([128, 64, 16], 3, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([64, 128, 16], 3, [2, 4, 1], math_inst, min_cc, max_cc), TileDescription([64, 64, 16], 3, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([64, 32, 16], 3, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([32, 64, 16], 3, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([32, 32, 16], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([16, 32, 16], 4, [1, 2, 1], math_inst, min_cc, max_cc), TileDescription([32, 16, 16], 3, [2, 1, 1], math_inst, min_cc, max_cc), ] data_type = [DataType.cf64, DataType.cf64, DataType.cf64, DataType.cf64] complex_transforms = [ (ComplexTransform.none, ComplexTransform.none), (ComplexTransform.conj, ComplexTransform.none), (ComplexTransform.none, ComplexTransform.conj), (ComplexTransform.conj, ComplexTransform.conj) ] CreateGemmOperator(manifest, layouts, tile_descriptions, \ data_type, alignment_constraints, complex_transforms) # # def GenerateSM90_TensorOp_1684_complex_gaussian(manifest, cuda_version): if not CudaToolkitVersionSatisfies(cuda_version, 11, 8): return layouts = [ (LayoutType.ColumnMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.ColumnMajor, LayoutType.RowMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.RowMajor, LayoutType.ColumnMajor), ] math_inst = \ MathInstruction( \ [16, 8, 4], \ DataType.f64, DataType.f64, DataType.f64, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add_complex_gaussian) min_cc = 90 max_cc = 1024 alignment_constraints = [1,] tile_descriptions = [ TileDescription([64, 64, 8], 3, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([64, 32, 8], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([32, 64, 8], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([32, 32, 8], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([16, 32, 8], 4, [1, 2, 1], math_inst, min_cc, max_cc), TileDescription([32, 16, 8], 4, [2, 1, 1], math_inst, min_cc, max_cc), ] data_type = [DataType.cf64, DataType.cf64, DataType.cf64, DataType.cf64] complex_transforms = [ (ComplexTransform.none, ComplexTransform.none), (ComplexTransform.conj, ComplexTransform.none), (ComplexTransform.none, ComplexTransform.conj), (ComplexTransform.conj, ComplexTransform.conj) ] CreateGemmOperator(manifest, layouts, tile_descriptions, \ data_type, alignment_constraints, complex_transforms) # # def GenerateSM90_TensorOp_1684_rank_k(manifest, cuda_version): if not CudaToolkitVersionSatisfies(cuda_version, 11, 8): return layouts = [ (LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.ColumnMajor), ] fill_modes = [ FillMode.Lower, FillMode.Upper, ] math_inst = \ MathInstruction( \ [16, 8, 4], \ DataType.f64, DataType.f64, DataType.f64, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add) min_cc = 90 max_cc = 1024 alignment_constraints = [1,] tile_descriptions = [ TileDescription([128, 128, 16], 3, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([64, 128, 16], 3, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 64, 16], 3, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([64, 64, 16], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([64, 32, 16], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([32, 64, 16], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([32, 32, 16], 5, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([16, 32, 16], 5, [1, 2, 1], math_inst, min_cc, max_cc), TileDescription([32, 16, 16], 5, [2, 1, 1], math_inst, min_cc, max_cc), ] data_type = [DataType.f64, DataType.f64, DataType.f64] CreateRankKOperator(manifest, layouts, fill_modes, tile_descriptions, \ data_type, alignment_constraints, BlasMode.symmetric) # # def GenerateSM90_TensorOp_1684_rank_k_complex(manifest, cuda_version): if not CudaToolkitVersionSatisfies(cuda_version, 11, 8): return layouts = [ (LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.ColumnMajor), ] fill_modes = [ FillMode.Lower, FillMode.Upper, ] math_inst = \ MathInstruction( \ [16, 8, 4], \ DataType.f64, DataType.f64, DataType.f64, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add_complex) min_cc = 90 max_cc = 1024 alignment_constraints = [1,] tile_descriptions = [ TileDescription([128, 64, 8], 3, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([64, 128, 8], 3, [2, 4, 1], math_inst, min_cc, max_cc), TileDescription([64, 64, 8], 3, [2, 2, 1], math_inst, min_cc, max_cc), #TileDescription([64, 32, 8], 4, [2, 2, 1], math_inst, min_cc, max_cc), #TileDescription([32, 64, 8], 4, [2, 2, 1], math_inst, min_cc, max_cc), #TileDescription([32, 32, 8], 4, [2, 2, 1], math_inst, min_cc, max_cc), #TileDescription([16, 32, 8], 4, [1, 2, 1], math_inst, min_cc, max_cc), #TileDescription([32, 16, 8], 4, [2, 1, 1], math_inst, min_cc, max_cc), ] data_type = [DataType.cf64, DataType.cf64, DataType.cf64] # SYRK computation CreateRankKOperator(manifest, layouts, fill_modes, tile_descriptions, \ data_type, alignment_constraints, BlasMode.symmetric) # HERK computation CreateRankKOperator(manifest, layouts, fill_modes, tile_descriptions, \ data_type, alignment_constraints, BlasMode.hermitian) # # def GenerateSM90_TensorOp_1684_rank_k_complex_gaussian(manifest, cuda_version): if not CudaToolkitVersionSatisfies(cuda_version, 11, 8): return layouts = [ (LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.ColumnMajor), ] fill_modes = [ FillMode.Lower, FillMode.Upper, ] math_inst = \ MathInstruction( \ [16, 8, 4], \ DataType.f64, DataType.f64, DataType.f64, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add_complex_gaussian) min_cc = 90 max_cc = 1024 alignment_constraints = [1,] tile_descriptions = [ TileDescription([64, 64, 8], 3, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([64, 32, 8], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([32, 64, 8], 4, [2, 2, 1], math_inst, min_cc, max_cc), #TileDescription([32, 32, 8], 4, [2, 2, 1], math_inst, min_cc, max_cc), #TileDescription([16, 32, 8], 4, [1, 2, 1], math_inst, min_cc, max_cc), #TileDescription([32, 16, 8], 4, [2, 1, 1], math_inst, min_cc, max_cc), ] data_type = [DataType.cf64, DataType.cf64, DataType.cf64] complex_transforms = [ComplexTransform.none,] # SYRK computation CreateRankKOperator(manifest, layouts, fill_modes, tile_descriptions, \ data_type, alignment_constraints, BlasMode.symmetric) # HERK computation CreateRankKOperator(manifest, layouts, fill_modes, tile_descriptions, \ data_type, alignment_constraints, BlasMode.hermitian) # # def GenerateSM90_TensorOp_1684_trmm(manifest, cuda_version): if not CudaToolkitVersionSatisfies(cuda_version, 11, 8): return layouts = [ (LayoutType.ColumnMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), ] side_modes = [ SideMode.Left, SideMode.Right, ] fill_modes = [ FillMode.Lower, FillMode.Upper, ] diag_types = [ DiagType.NonUnit, DiagType.Unit, ] math_inst = \ MathInstruction( \ [16, 8, 4], \ DataType.f64, DataType.f64, DataType.f64, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add) min_cc = 90 max_cc = 1024 alignment_constraints = [1,] tile_descriptions = [ TileDescription([128, 128, 16], 3, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([64, 128, 16], 3, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 64, 16], 3, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([64, 64, 16], 4, [2, 2, 1], math_inst, min_cc, max_cc), ] data_type = [DataType.f64, DataType.f64, DataType.f64, DataType.f64] CreateTrmmOperator(manifest, layouts, side_modes, fill_modes, diag_types, tile_descriptions, \ data_type, alignment_constraints) # # def GenerateSM90_TensorOp_1684_trmm_complex(manifest, cuda_version): if not CudaToolkitVersionSatisfies(cuda_version, 11, 8): return layouts = [ (LayoutType.ColumnMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), ] side_modes = [ SideMode.Left, SideMode.Right, ] fill_modes = [ FillMode.Lower, FillMode.Upper, ] diag_types = [ DiagType.NonUnit, DiagType.Unit, ] math_inst = \ MathInstruction( \ [16, 8, 4], \ DataType.f64, DataType.f64, DataType.f64, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add_complex) min_cc = 90 max_cc = 1024 alignment_constraints = [1,] tile_descriptions = [ TileDescription([128, 64, 8], 3, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([64, 128, 8], 3, [2, 4, 1], math_inst, min_cc, max_cc), TileDescription([64, 64, 8], 3, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([64, 32, 8], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([32, 64, 8], 4, [2, 2, 1], math_inst, min_cc, max_cc), ] data_type = [DataType.cf64, DataType.cf64, DataType.cf64, DataType.cf64] complex_transforms = [ ComplexTransform.none, ComplexTransform.conj, ] CreateTrmmOperator(manifest, layouts, side_modes, fill_modes, diag_types, tile_descriptions, \ data_type, alignment_constraints, complex_transforms) # # def GenerateSM90_TensorOp_1684_trmm_complex_gaussian(manifest, cuda_version): if not CudaToolkitVersionSatisfies(cuda_version, 11, 8): return layouts = [ (LayoutType.ColumnMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), (LayoutType.RowMajor, LayoutType.ColumnMajor, LayoutType.ColumnMajor), ] side_modes = [ SideMode.Left, SideMode.Right, ] fill_modes = [ FillMode.Lower, FillMode.Upper, ] diag_types = [ DiagType.NonUnit, DiagType.Unit, ] math_inst = \ MathInstruction( \ [16, 8, 4], \ DataType.f64, DataType.f64, DataType.f64, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add_complex_gaussian) min_cc = 90 max_cc = 1024 alignment_constraints = [1,] tile_descriptions = [ TileDescription([64, 64, 8], 3, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([64, 32, 8], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([32, 64, 8], 4, [2, 2, 1], math_inst, min_cc, max_cc), ] data_type = [DataType.cf64, DataType.cf64, DataType.cf64, DataType.cf64] complex_transforms = [ ComplexTransform.none, ComplexTransform.conj, ] CreateTrmmOperator(manifest, layouts, side_modes, fill_modes, diag_types, tile_descriptions, \ data_type, alignment_constraints, complex_transforms) # # def GenerateSM90_TensorOp_1684_symm(manifest, cuda_version): if not CudaToolkitVersionSatisfies(cuda_version, 11, 8): return layouts = [ (LayoutType.ColumnMajor, LayoutType.ColumnMajor), ] side_modes = [ SideMode.Left, SideMode.Right, ] fill_modes = [ FillMode.Lower, FillMode.Upper, ] math_inst = \ MathInstruction( \ [16, 8, 4], \ DataType.f64, DataType.f64, DataType.f64, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add) min_cc = 90 max_cc = 1024 alignment_constraints = [1,] tile_descriptions = [ TileDescription([128, 128, 16], 3, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([64, 128, 16], 3, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([128, 64, 16], 3, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([64, 64, 16], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([64, 32, 16], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([32, 64, 16], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([32, 32, 16], 5, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([16, 32, 16], 5, [1, 2, 1], math_inst, min_cc, max_cc), TileDescription([32, 16, 16], 5, [2, 1, 1], math_inst, min_cc, max_cc), ] data_type = [DataType.f64, DataType.f64, DataType.f64, DataType.f64] CreateSymmOperator(manifest, layouts, side_modes, fill_modes, tile_descriptions, \ data_type, alignment_constraints, BlasMode.symmetric) # # def GenerateSM90_TensorOp_1684_symm_complex(manifest, cuda_version): if not CudaToolkitVersionSatisfies(cuda_version, 11, 8): return layouts = [ (LayoutType.ColumnMajor, LayoutType.ColumnMajor), ] side_modes = [ SideMode.Left, SideMode.Right, ] fill_modes = [ FillMode.Lower, FillMode.Upper, ] math_inst = \ MathInstruction( \ [16, 8, 4], \ DataType.f64, DataType.f64, DataType.f64, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add_complex) min_cc = 90 max_cc = 1024 alignment_constraints = [1,] tile_descriptions = [ TileDescription([128, 64, 8], 3, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([64, 128, 8], 3, [2, 4, 1], math_inst, min_cc, max_cc), TileDescription([64, 64, 8], 3, [2, 2, 1], math_inst, min_cc, max_cc), #TileDescription([64, 32, 8], 4, [2, 2, 1], math_inst, min_cc, max_cc), #TileDescription([32, 64, 8], 4, [2, 2, 1], math_inst, min_cc, max_cc), #TileDescription([32, 32, 8], 4, [2, 2, 1], math_inst, min_cc, max_cc), #TileDescription([16, 32, 8], 4, [1, 2, 1], math_inst, min_cc, max_cc), #TileDescription([32, 16, 8], 4, [2, 1, 1], math_inst, min_cc, max_cc), ] data_type = [DataType.cf64, DataType.cf64, DataType.cf64, DataType.cf64] # SYMM computation CreateSymmOperator(manifest, layouts, side_modes, fill_modes, tile_descriptions, \ data_type, alignment_constraints, BlasMode.symmetric) # HEMM computation CreateSymmOperator(manifest, layouts, side_modes, fill_modes, tile_descriptions, \ data_type, alignment_constraints, BlasMode.hermitian) # # def GenerateSM90_TensorOp_1684_symm_complex_gaussian(manifest, cuda_version): if not CudaToolkitVersionSatisfies(cuda_version, 11, 8): return layouts = [ (LayoutType.ColumnMajor, LayoutType.ColumnMajor), ] side_modes = [ SideMode.Left, SideMode.Right, ] fill_modes = [ FillMode.Lower, FillMode.Upper, ] math_inst = \ MathInstruction( \ [16, 8, 4], \ DataType.f64, DataType.f64, DataType.f64, \ OpcodeClass.TensorOp, \ MathOperation.multiply_add_complex_gaussian) min_cc = 90 max_cc = 1024 alignment_constraints = [1,] tile_descriptions = [ TileDescription([64, 64, 8], 3, [4, 2, 1], math_inst, min_cc, max_cc), TileDescription([64, 32, 8], 4, [2, 2, 1], math_inst, min_cc, max_cc), TileDescription([32, 64, 8], 4, [2, 2, 1], math_inst, min_cc, max_cc), #TileDescription([32, 32, 8], 4, [2, 2, 1], math_inst, min_cc, max_cc), #TileDescription([16, 32, 8], 4, [1, 2, 1], math_inst, min_cc, max_cc), #TileDescription([32, 16, 8], 4, [2, 1, 1], math_inst, min_cc, max_cc), ] data_type = [DataType.cf64, DataType.cf64, DataType.cf64, DataType.cf64] complex_transforms = [ComplexTransform.none,] # SYMM computation CreateSymmOperator(manifest, layouts, side_modes, fill_modes, tile_descriptions, \ data_type, alignment_constraints, BlasMode.symmetric) # HEMM computation CreateSymmOperator(manifest, layouts, side_modes, fill_modes, tile_descriptions, \ data_type, alignment_constraints, BlasMode.hermitian) # ################################################################################################### # def GenerateSM90(manifest, cuda_version): GenerateSM90_TensorOp_16b_WGMMA_gemm(manifest, cuda_version) GenerateSM90_TensorOp_int8_WGMMA_gemm(manifest, cuda_version) GenerateSM90_TensorOp_tf32_WGMMA_gemm(manifest, cuda_version) GenerateSM90_TensorOp_1684(manifest, cuda_version) GenerateSM90_TensorOp_1684_complex(manifest, cuda_version) GenerateSM90_TensorOp_1684_complex_gaussian(manifest, cuda_version) GenerateSM90_TensorOp_1684_rank_k(manifest, cuda_version) GenerateSM90_TensorOp_1684_rank_k_complex(manifest, cuda_version) GenerateSM90_TensorOp_1684_rank_k_complex_gaussian(manifest, cuda_version) GenerateSM90_TensorOp_1684_trmm(manifest, cuda_version) GenerateSM90_TensorOp_1684_trmm_complex(manifest, cuda_version) GenerateSM90_TensorOp_1684_trmm_complex_gaussian(manifest, cuda_version) GenerateSM90_TensorOp_1684_symm(manifest, cuda_version) GenerateSM90_TensorOp_1684_symm_complex(manifest, cuda_version) GenerateSM90_TensorOp_1684_symm_complex_gaussian(manifest, cuda_version) ################################################################################################### if __name__ == "__main__": parser = argparse.ArgumentParser(description="Generates device kernel registration code for CUTLASS Kernels") parser.add_argument("--operations", default="all", help="Specifies the operation to generate (gemm, all)") parser.add_argument("--build-dir", default=".", required=False, help="CUTLASS top-level build directory") parser.add_argument("--curr-build-dir", default=".", help="CUTLASS current build directory. cmake files will be emitted in this directory") parser.add_argument("--generator-target", default='library', help="Target of CUTLASS Library Generator.") parser.add_argument("--architectures", default='53;60;61;70;75;80', help="Target compute architectures") parser.add_argument("--kernels", default='', help='Comma delimited list to filter kernels by name.') parser.add_argument("--ignore-kernels", default='', help='Comma delimited list of kernels to exclude from build.') parser.add_argument("--filter-by-cc", default='True', type=str, help='If enabled, kernels whose compute capability range is not satisfied by the build target are excluded.') parser.add_argument("--cuda-version", default="11.0.0", help="Semantic version string of CUDA Toolkit") parser.add_argument('--kernel-filter-file', type=str, default=None, required=False, help='Full path of filter file') parser.add_argument('--selected-kernel-list', type=str, default=None, required=False, help='Specify the output log file containing all enabled kernels in this build') parser.add_argument("--interface-dir", default=None, required=False, help="Interface header to kernels") parser.add_argument("--disable-full-archs-compilation", action="store_true", required=False, help="Disable compilation for every archs in --architectures") def numeric_log_level(log_level: str) -> int: """ Converts the string identifier of the log level into the numeric identifier used in setting the log level :param x: string representation of log level (e.g., 'INFO', 'DEBUG') :type x: str :return: numeric representation of log level :rtype: int """ numeric_level = getattr(logging, log_level.upper(), None) if not isinstance(numeric_level, int): raise ValueError(f'Invalid log level: {log_level}') return numeric_level parser.add_argument("--log-level", default='info', type=numeric_log_level, required=False, help='Logging level to be used by the generator script') args = parser.parse_args() # Set the logging level based on the user-provided `--log-level` command-line option logging.basicConfig(level=args.log_level) manifest = Manifest(args) GenerateSM50(manifest, args.cuda_version) GenerateSM60(manifest, args.cuda_version) GenerateSM61(manifest, args.cuda_version) GenerateSM70(manifest, args.cuda_version) GenerateSM75(manifest, args.cuda_version) GenerateSM80(manifest, args.cuda_version) GenerateSM90(manifest, args.cuda_version) if 'library' in args.generator_target.split(','): manifest.emit(GeneratorTarget.Library) if args.selected_kernel_list is not None: if len(manifest.selected_kernels) > 0: with open(args.selected_kernel_list, 'w') as file_writer: for line in manifest.selected_kernels: file_writer.write("%s\n" % line) # ###################################################################################################
# # \file generator.py # # \brief Generates the CUTLASS Library's instances # # import enum import os.path import shutil from library import * ################################################################################################### # class Conv3dOperation: # def __init__(self, conv_kind, iterator_algorithm, arch, tile_description, A, B, C, element_epilogue, \ stride_support, epilogue_functor = EpilogueFunctor.LinearCombination, swizzling_functor = SwizzlingFunctor.Identity4): self.operation_kind = OperationKind.Conv3d self.arch = arch self.tile_description = tile_description self.conv_kind = conv_kind self.A = A self.B = B self.C = C self.element_epilogue = element_epilogue self.epilogue_functor = epilogue_functor self.iterator_algorithm = iterator_algorithm self.stride_support = stride_support self.swizzling_functor = swizzling_functor # def core_name(self): ''' The basic operation kind is prefixed with a letter indicating the accumulation type. ''' intermediate_type = '' if self.tile_description.math_instruction.opcode_class == OpcodeClass.TensorOp: inst_shape = "%d%d%d" % tuple(self.tile_description.math_instruction.instruction_shape) if self.tile_description.math_instruction.element_a != self.A.element and \ self.tile_description.math_instruction.element_a != self.tile_description.math_instruction.element_accumulator: intermediate_type = DataTypeNames[self.tile_description.math_instruction.element_a] else: inst_shape = '' return "%s%s%s%s3d_%s" % (ShortDataTypeNames[self.tile_description.math_instruction.element_accumulator], \ inst_shape, intermediate_type, ConvKindNames[self.conv_kind], IteratorAlgorithmNames[self.iterator_algorithm]) # def extended_name(self): ''' Append data types if they differ from compute type. ''' if self.C.element != self.tile_description.math_instruction.element_accumulator and \ self.A.element != self.tile_description.math_instruction.element_accumulator: extended_name = "${element_c}_${core_name}_${element_a}" elif self.C.element == self.tile_description.math_instruction.element_accumulator and \ self.A.element != self.tile_description.math_instruction.element_accumulator: extended_name = "${core_name}_${element_a}" else: extended_name = "${core_name}" extended_name = SubstituteTemplate(extended_name, { 'element_a': DataTypeNames[self.A.element], 'element_c': DataTypeNames[self.C.element], 'core_name': self.core_name() }) return extended_name # def configuration_name(self): ''' The full procedural name indicates architecture, extended name, tile size, and layout. ''' opcode_class_name = OpcodeClassNames[self.tile_description.math_instruction.opcode_class] threadblock = "%dx%d_%dx%d" % ( self.tile_description.threadblock_shape[0], self.tile_description.threadblock_shape[1], self.tile_description.threadblock_shape[2], self.tile_description.stages ) if self.stride_support == StrideSupport.Unity: configuration_name = "cutlass_${opcode_class}_${extended_name}_${threadblock}_unity_stride" else: configuration_name = "cutlass_${opcode_class}_${extended_name}_${threadblock}" return SubstituteTemplate( configuration_name, { 'opcode_class': opcode_class_name, 'extended_name': self.extended_name(), 'threadblock': threadblock, } ) # def procedural_name(self): ''' The full procedural name indicates architecture, extended name, tile size, and layout. ''' return self.configuration_name() ################################################################################################### # # Emits single instances of a CUTLASS device-wide operator # ################################################################################################### class EmitConv3dInstance: def __init__(self): self.template = """ // Conv3d${conv_kind_name} ${iterator_algorithm_name} kernel instance "${operation_name}" using ${operation_name}_base = typename cutlass::conv::kernel::DefaultConv3d${conv_kind_name}< ${element_a}, cutlass::layout::TensorNDHWC, ${element_b}, cutlass::layout::TensorNDHWC, ${element_c}, cutlass::layout::TensorNDHWC, ${element_accumulator}, ${opcode_class}, ${arch}, cutlass::gemm::GemmShape<${threadblock_shape_m}, ${threadblock_shape_n}, ${threadblock_shape_k}>, cutlass::gemm::GemmShape<${warp_shape_m}, ${warp_shape_n}, ${warp_shape_k} >, cutlass::gemm::GemmShape<${instruction_shape_m}, ${instruction_shape_n}, ${instruction_shape_k}>, ${epilogue_functor}< ${element_c}, ${epilogue_vector_length}, ${element_accumulator}, ${element_epilogue} >, ${swizzling_functor}, // cutlass::gemm::threadblock::GemmSplitKIdentityThreadblockSwizzle<>, ${stages}, cutlass::arch::OpMultiplyAdd, ${iterator_algorithm}, ${stride_support} >::Kernel; """ def emit(self, operation): warp_shape = [int(operation.tile_description.threadblock_shape[idx] / operation.tile_description.warp_count[idx]) for idx in range(3)] epilogue_vector_length = int(min(operation.C.alignment * DataTypeSize[operation.C.element], 128) / DataTypeSize[operation.C.element]) values = { 'operation_name': operation.procedural_name(), 'conv_kind': ConvKindTag[operation.conv_kind], 'conv_kind_name': ConvKindNames[operation.conv_kind].capitalize(), 'element_a': DataTypeTag[operation.A.element], 'layout_a': LayoutTag[operation.A.layout], 'element_b': DataTypeTag[operation.B.element], 'layout_b': LayoutTag[operation.B.layout], 'element_c': DataTypeTag[operation.C.element], 'layout_c': LayoutTag[operation.C.layout], 'element_accumulator': DataTypeTag[operation.tile_description.math_instruction.element_accumulator], 'opcode_class': OpcodeClassTag[operation.tile_description.math_instruction.opcode_class], 'arch': "cutlass::arch::Sm%d" % operation.arch, 'threadblock_shape_m': str(operation.tile_description.threadblock_shape[0]), 'threadblock_shape_n': str(operation.tile_description.threadblock_shape[1]), 'threadblock_shape_k': str(operation.tile_description.threadblock_shape[2]), 'warp_shape_m': str(warp_shape[0]), 'warp_shape_n': str(warp_shape[1]), 'warp_shape_k': str(warp_shape[2]), 'instruction_shape_m': str(operation.tile_description.math_instruction.instruction_shape[0]), 'instruction_shape_n': str(operation.tile_description.math_instruction.instruction_shape[1]), 'instruction_shape_k': str(operation.tile_description.math_instruction.instruction_shape[2]), 'epilogue_vector_length': str(epilogue_vector_length), 'epilogue_functor': EpilogueFunctorTag[operation.epilogue_functor], 'element_epilogue': str(DataTypeTag[operation.element_epilogue]), 'swizzling_functor': SwizzlingFunctorTag[operation.swizzling_functor], 'stages': str(operation.tile_description.stages), 'iterator_algorithm': IteratorAlgorithmTag[operation.iterator_algorithm], 'iterator_algorithm_name': IteratorAlgorithmNames[operation.iterator_algorithm].capitalize(), 'stride_support': StrideSupportTag[operation.stride_support] } return SubstituteTemplate(self.template, values) ################################################################################################### # # Generator functions for all layouts # ################################################################################################### # def GenerateConv3dTensorOp(manifest, tile_descriptions, min_cc, align = 128): for tile in tile_descriptions: for conv_kind in [ConvKind.Fprop, ConvKind.Dgrad, ConvKind.Wgrad]: if conv_kind == ConvKind.Fprop or (tile.math_instruction.element_accumulator in [DataType.f16, DataType.f32]): # output_types = [tile.math_instruction.element_a, tile.math_instruction.element_accumulator] \ if DataTypeSize[tile.math_instruction.element_accumulator] == 32 \ else [tile.math_instruction.element_accumulator,] for output_type in output_types: A = TensorDescription(tile.math_instruction.element_a, LayoutType.TensorNDHWC, int(align / DataTypeSize[tile.math_instruction.element_a])) B = TensorDescription(tile.math_instruction.element_b, LayoutType.TensorNDHWC, int(align / DataTypeSize[tile.math_instruction.element_b])) C = TensorDescription(output_type, LayoutType.TensorNDHWC, max(1, int(align / DataTypeSize[output_type]))) manifest.append(Conv3dOperation(conv_kind, min_cc, tile, A, B, C, tile.math_instruction.element_accumulator)) ################################################################################################### # # Emitters functions for all targets # ################################################################################################### class EmitConv3dConfigurationLibrary: def __init__(self, operation_path, configuration_name): self.configuration_name = configuration_name self.configuration_path = os.path.join(operation_path, "%s.cu" % configuration_name) self.instance_emitter = EmitConv3dInstance() self.instance_template = """ ${operation_instance} // Derived class struct ${operation_name} : public ${operation_name}_base { }; /////////////////////////////////////////////////////////////////////////////////////////////////// """ self.header_template = """ /* Generated by conv3d_operation.py - Do not edit. */ /////////////////////////////////////////////////////////////////////////////////////////////////// #include "cutlass/cutlass.h" #include "cutlass/library/library.h" #include "cutlass/library/manifest.h" #include "library_internal.h" #include "conv3d_operation.h" /////////////////////////////////////////////////////////////////////////////////////////////////// """ self.configuration_header = """ namespace cutlass { namespace library { // Initialize all instances void initialize_${configuration_name}(Manifest &manifest) { """ self.configuration_instance = """ using Operation_${operation_name} = cutlass::conv::device::ImplicitGemmConvolution< ${operation_name}>; manifest.append(new cutlass::library::Conv3dOperation< Operation_${operation_name}>( "${operation_name}")); """ self.configuration_epilogue = """ } """ self.epilogue_template = """ /////////////////////////////////////////////////////////////////////////////////////////////////// } // namespace library } // namespace cutlass /////////////////////////////////////////////////////////////////////////////////////////////////// """ # def __enter__(self): self.configuration_file = open(self.configuration_path, "w") self.configuration_file.write(SubstituteTemplate(self.header_template, { 'configuration_name': self.configuration_name })) self.operations = [] return self # def emit(self, operation): self.operations.append(operation) self.configuration_file.write(SubstituteTemplate(self.instance_template, { 'configuration_name': self.configuration_name, 'operation_name': operation.procedural_name(), 'operation_instance': self.instance_emitter.emit(operation) })) # def __exit__(self, exception_type, exception_value, traceback): self.configuration_file.write(SubstituteTemplate(self.configuration_header, { 'configuration_name': self.configuration_name })) for operation in self.operations: self.configuration_file.write(SubstituteTemplate(self.configuration_instance, { 'configuration_name': self.configuration_name, 'operation_name': operation.procedural_name() })) self.configuration_file.write(self.configuration_epilogue) self.configuration_file.write(self.epilogue_template) self.configuration_file.close() ################################################################################################### ###################################################################################################
# # \file generator.py # # \brief Generates the CUTLASS Library's instances # # import enum import os.path import shutil import functools import operator from library import * ################################################################################################### # # Data structure modeling a Rank K update operation # ################################################################################################### # class Rank2KOperation: # def __init__(self, rank_k_kind, arch, tile_description, A, C, element_epilogue, \ epilogue_functor = EpilogueFunctor.LinearCombination, swizzling_functor = SwizzlingFunctor.Identity8, \ blas_mode = BlasMode.symmetric): self.blas_mode = blas_mode self.operation_kind = OperationKind.Rank2K self.arch = arch self.tile_description = tile_description self.rank_k_kind = rank_k_kind # tensor A and B have same data type and layout self.A = A self.B = A self.C = C self.element_epilogue = element_epilogue self.epilogue_functor = epilogue_functor self.swizzling_functor = swizzling_functor # def is_complex(self): complex_operators = [ MathOperation.multiply_add_complex, MathOperation.multiply_add_complex_gaussian, MathOperation.multiply_add_complex_fast_f32 ] return self.tile_description.math_instruction.math_operation in complex_operators return False # def is_planar_complex(self): return False # def accumulator_type(self): accum = self.tile_description.math_instruction.element_accumulator if self.is_complex(): return get_complex_from_real(accum) return accum # def short_math_name(self): if self.tile_description.math_instruction.math_operation == MathOperation.multiply_add_complex_gaussian: return "g%s" % ShortDataTypeNames[self.accumulator_type()] return ShortDataTypeNames[self.accumulator_type()] # def core_name(self): ''' The basic operation kind is prefixed with a letter indicating the accumulation type. ''' inst_shape = '' inst_operation = '' intermediate_type = '' math_operations_map = { MathOperation.xor_popc: 'xor', } if self.tile_description.math_instruction.opcode_class == OpcodeClass.TensorOp or \ self.tile_description.math_instruction.opcode_class == OpcodeClass.WmmaTensorOp: math_op = self.tile_description.math_instruction.math_operation math_op_string = math_operations_map[math_op] if math_op in math_operations_map.keys() else '' inst_shape = "%d%d%d" % tuple(self.tile_description.math_instruction.instruction_shape) inst_shape += math_op_string if self.tile_description.math_instruction.element_a != self.A.element and \ self.tile_description.math_instruction.element_a != self.tile_description.math_instruction.element_accumulator: intermediate_type = DataTypeNames[self.tile_description.math_instruction.element_a] operation_name = 'syr2k' if self.blas_mode == BlasMode.symmetric else 'her2k' return "%s%s%s%s" % (self.short_math_name(), inst_shape, intermediate_type, operation_name) # def extended_name(self): ''' Append data types if they differ from compute type. ''' if self.is_complex(): extended_name = "${core_name}" else: if self.C.element != self.tile_description.math_instruction.element_accumulator and \ self.A.element != self.tile_description.math_instruction.element_accumulator: extended_name = "${element_c}_${core_name}_${element_a}" elif self.C.element == self.tile_description.math_instruction.element_accumulator and \ self.A.element != self.tile_description.math_instruction.element_accumulator: extended_name = "${core_name}_${element_a}" else: extended_name = "${core_name}" extended_name = SubstituteTemplate(extended_name, { 'element_a': DataTypeNames[self.A.element], 'element_c': DataTypeNames[self.C.element], 'core_name': self.core_name() }) return extended_name # def layout_name(self): if self.is_complex() or self.is_planar_complex(): return "%s" % ( ShortComplexLayoutNames[(self.A.layout, self.A.complex_transform)] ) return "%s" % (ShortLayoutTypeNames[self.A.layout]) # def fill_mode_name(self): return "%s" % (ShortFillModeNames[self.C.fill_mode]) # def procedural_name(self): ''' The full procedural name indicates architecture, extended name, tile size, and layout. ''' threadblock = self.tile_description.procedural_name() opcode_class_name = OpcodeClassNames[self.tile_description.math_instruction.opcode_class] alignment = max([self.A.alignment, self.C.alignment]) return SubstituteTemplate( "cutlass_${opcode_class}_${extended_name}_${threadblock}_${layout}_${fill_mode}_align${alignment}", { 'opcode_class': opcode_class_name, 'extended_name': self.extended_name(), 'threadblock': threadblock, 'layout': self.layout_name(), 'fill_mode': self.fill_mode_name(), 'alignment': "%d" % self.A.alignment, } ) # def configuration_name(self): ''' The full procedural name indicates architecture, extended name, tile size, and layout. ''' return self.procedural_name() ################################################################################################### # # Emits single instances of a CUTLASS device-wide operator # ################################################################################################### # class EmitRank2KUniversalInstance: ''' Responsible for emitting a CUTLASS template definition''' def __init__(self): self.rank_k_template = """ // Rank K operator ${operation_name} using Operation_${operation_name} = typename cutlass::gemm::device::Rank2K< ${element_a}, ${layout_a}, ${element_b}, ${layout_b}, ${element_c}, ${layout_c}, ${fill_mode}, ${element_accumulator}, ${opcode_class}, ${arch}, cutlass::gemm::GemmShape<${threadblock_shape_m}, ${threadblock_shape_n}, ${threadblock_shape_k}>, cutlass::gemm::GemmShape<${warp_shape_m}, ${warp_shape_n}, ${warp_shape_k}>, cutlass::gemm::GemmShape<${instruction_shape_m}, ${instruction_shape_n}, ${instruction_shape_k}>, ${epilogue_functor}< ${element_c}, ${epilogue_vector_length}, ${element_accumulator}, ${element_epilogue} >, ${swizzling_functor}, ${stages}, ${align_a}, ${align_b}, ${split_k_serial}, ${math_operation} >; """ self.rank_k_complex_template = """ // Rank K operator ${operation_name} using Operation_${operation_name} = typename cutlass::gemm::device::Rank2K< ${element_a}, ${layout_a}, ${element_b}, ${layout_b}, ${element_c}, ${layout_c}, ${fill_mode}, ${element_accumulator}, ${opcode_class}, ${arch}, cutlass::gemm::GemmShape<${threadblock_shape_m}, ${threadblock_shape_n}, ${threadblock_shape_k}>, cutlass::gemm::GemmShape<${warp_shape_m}, ${warp_shape_n}, ${warp_shape_k}>, cutlass::gemm::GemmShape<${instruction_shape_m}, ${instruction_shape_n}, ${instruction_shape_k}>, ${epilogue_functor}< ${element_c}, ${epilogue_vector_length}, ${element_accumulator}, ${element_epilogue} >, ${swizzling_functor}, ${stages}, ${align_a}, ${align_b}, ${split_k_serial}, ${math_operation}, ${transform_a}, ${transform_b}, ${blas_mode} >; """ def emit(self, operation): threadblock_shape = operation.tile_description.threadblock_shape warp_count = operation.tile_description.warp_count warp_shape = [threadblock_shape[idx] // warp_count[idx] for idx in range(3)] epilogue_vector_length = int(min(operation.C.alignment * DataTypeSize[operation.C.element], 128) / DataTypeSize[operation.C.element]) values = { 'operation_name': operation.procedural_name(), 'element_a': DataTypeTag[operation.A.element], 'layout_a': LayoutTag[operation.A.layout], 'element_b': DataTypeTag[operation.B.element], 'layout_b': LayoutTag[operation.B.layout], 'element_c': DataTypeTag[operation.C.element], 'layout_c': LayoutTag[operation.C.layout], 'fill_mode': FillModeTag[operation.C.fill_mode], 'element_accumulator': DataTypeTag[operation.accumulator_type()], 'opcode_class': OpcodeClassTag[operation.tile_description.math_instruction.opcode_class], 'arch': "cutlass::arch::Sm%d" % operation.arch, 'threadblock_shape_m': str(operation.tile_description.threadblock_shape[0]), 'threadblock_shape_n': str(operation.tile_description.threadblock_shape[1]), 'threadblock_shape_k': str(operation.tile_description.threadblock_shape[2]), 'warp_shape_m': str(warp_shape[0]), 'warp_shape_n': str(warp_shape[1]), 'warp_shape_k': str(warp_shape[2]), 'instruction_shape_m': str(operation.tile_description.math_instruction.instruction_shape[0]), 'instruction_shape_n': str(operation.tile_description.math_instruction.instruction_shape[1]), 'instruction_shape_k': str(operation.tile_description.math_instruction.instruction_shape[2]), 'epilogue_vector_length': str(epilogue_vector_length), 'element_epilogue': str(DataTypeTag[operation.element_epilogue]), 'epilogue_functor': EpilogueFunctorTag[operation.epilogue_functor], 'swizzling_functor': SwizzlingFunctorTag[operation.swizzling_functor], 'stages': str(operation.tile_description.stages), 'align_a': str(operation.A.alignment), 'align_b': str(operation.B.alignment), 'split_k_serial': 'false', 'math_operation': MathOperationTag[operation.tile_description.math_instruction.math_operation], 'transform_a': ComplexTransformTag[operation.A.complex_transform], 'transform_b': ComplexTransformTag[operation.B.complex_transform], 'blas_mode': BlasModeTag[operation.blas_mode] } rank_k_template = self.rank_k_complex_template if operation.is_complex() else self.rank_k_template return SubstituteTemplate(rank_k_template, values) ################################################################################################### ################################################################################################### # # Emitters functions for all targets # ################################################################################################### class EmitRank2KConfigurationLibrary: def __init__(self, operation_path, configuration_name): self.configuration_name = configuration_name self.configuration_path = os.path.join(operation_path, "%s.cu" % configuration_name).replace('\\', '/') self.instance_emitter = { RankKKind.Universal: EmitRank2KUniversalInstance, } self.rank_k_kind_wrappers = { RankKKind.Universal: 'Rank2KOperation', } self.instance_template = { RankKKind.Universal: """ ${compile_guard_start} manifest.append(new ${rank_k_kind}< Operation_${operation_name} >("${operation_name}")); ${compile_guard_end} """ } self.header_template = """ /* Generated by rank_2k_operation.py - Do not edit. */ /////////////////////////////////////////////////////////////////////////////////////////////////// #include "cutlass/cutlass.h" #include "cutlass/library/library.h" #include "cutlass/library/manifest.h" #include "library_internal.h" #include "rank_2k_operation.h" /////////////////////////////////////////////////////////////////////////////////////////////////// """ self.initialize_function_template = """ /////////////////////////////////////////////////////////////////////////////////////////////////// namespace cutlass { namespace library { /////////////////////////////////////////////////////////////////////////////////////////////////// void initialize_${configuration_name}(Manifest &manifest) { """ self.epilogue_template = """ } /////////////////////////////////////////////////////////////////////////////////////////////////// } // namespace library } // namespace cutlass /////////////////////////////////////////////////////////////////////////////////////////////////// """ def __enter__(self): self.configuration_file = open(self.configuration_path, "w") self.configuration_file.write(self.header_template) self.instance_definitions = [] self.instance_wrappers = [] self.operations = [] return self def emit(self, operation): emitter = self.instance_emitter[operation.rank_k_kind]() self.operations.append(operation) self.instance_definitions.append(emitter.emit(operation)) self.instance_wrappers.append(SubstituteTemplate(self.instance_template[operation.rank_k_kind], { 'configuration_name': self.configuration_name, 'operation_name': operation.procedural_name(), 'rank_k_kind': self.rank_k_kind_wrappers[operation.rank_k_kind], 'compile_guard_start': SubstituteTemplate(self.wmma_guard_start, {'sm_number': str(operation.arch)}) \ if operation.tile_description.math_instruction.opcode_class == OpcodeClass.WmmaTensorOp else "", 'compile_guard_end': "#endif" \ if operation.tile_description.math_instruction.opcode_class == OpcodeClass.WmmaTensorOp else "" })) def __exit__(self, exception_type, exception_value, traceback): # Write instance definitions in top-level namespace for instance_definition in self.instance_definitions: self.configuration_file.write(instance_definition) # Add wrapper objects within initialize() function self.configuration_file.write(SubstituteTemplate(self.initialize_function_template, { 'configuration_name': self.configuration_name })) for instance_wrapper in self.instance_wrappers: self.configuration_file.write(instance_wrapper) self.configuration_file.write(self.epilogue_template) self.configuration_file.close() ###################################################################################################
# # \file generator.py # # \brief Generates the CUTLASS Library's instances # # import enum import os.path import shutil import functools import operator from library import * ################################################################################################### # # Data structure modeling a Symm update operation # ################################################################################################### # class SymmOperation: # def __init__(self, symm_kind, arch, tile_description, A, B, C, element_epilogue, \ epilogue_functor = EpilogueFunctor.LinearCombination, swizzling_functor = SwizzlingFunctor.Identity8, \ blas_mode = BlasMode.symmetric): self.blas_mode = blas_mode self.operation_kind = OperationKind.Symm self.arch = arch self.tile_description = tile_description self.symm_kind = symm_kind # tensor A and B have same data type and layout self.A = A self.B = B self.C = C self.element_epilogue = element_epilogue self.epilogue_functor = epilogue_functor self.swizzling_functor = swizzling_functor # def is_complex(self): complex_operators = [ MathOperation.multiply_add_complex, MathOperation.multiply_add_complex_gaussian, MathOperation.multiply_add_complex_fast_f32 ] return self.tile_description.math_instruction.math_operation in complex_operators return False # def is_planar_complex(self): return False # def accumulator_type(self): accum = self.tile_description.math_instruction.element_accumulator if self.is_complex(): return get_complex_from_real(accum) return accum # def short_math_name(self): if self.tile_description.math_instruction.math_operation == MathOperation.multiply_add_complex_gaussian: return "g%s" % ShortDataTypeNames[self.accumulator_type()] return ShortDataTypeNames[self.accumulator_type()] # def core_name(self): ''' The basic operation kind is prefixed with a letter indicating the accumulation type. ''' inst_shape = '' inst_operation = '' intermediate_type = '' math_operations_map = { MathOperation.xor_popc: 'xor', } if self.tile_description.math_instruction.opcode_class == OpcodeClass.TensorOp or \ self.tile_description.math_instruction.opcode_class == OpcodeClass.WmmaTensorOp: math_op = self.tile_description.math_instruction.math_operation math_op_string = math_operations_map[math_op] if math_op in math_operations_map.keys() else '' inst_shape = "%d%d%d" % tuple(self.tile_description.math_instruction.instruction_shape) inst_shape += math_op_string if self.tile_description.math_instruction.element_a != self.A.element and \ self.tile_description.math_instruction.element_a != self.tile_description.math_instruction.element_accumulator: intermediate_type = DataTypeNames[self.tile_description.math_instruction.element_a] operation_name = 'symm' if self.blas_mode == BlasMode.symmetric else 'hemm' return "%s%s%s%s" % (self.short_math_name(), inst_shape, intermediate_type, operation_name) # def extended_name(self): ''' Append data types if they differ from compute type. ''' if self.is_complex(): extended_name = "${core_name}" else: if self.C.element != self.tile_description.math_instruction.element_accumulator and \ self.A.element != self.tile_description.math_instruction.element_accumulator: extended_name = "${element_c}_${core_name}_${element_a}" elif self.C.element == self.tile_description.math_instruction.element_accumulator and \ self.A.element != self.tile_description.math_instruction.element_accumulator: extended_name = "${core_name}_${element_a}" else: extended_name = "${core_name}" extended_name = SubstituteTemplate(extended_name, { 'element_a': DataTypeNames[self.A.element], 'element_c': DataTypeNames[self.C.element], 'core_name': self.core_name() }) return extended_name # def layout_name(self): if self.is_complex() or self.is_planar_complex(): return "%s" % ( ShortComplexLayoutNames[(self.A.layout, self.A.complex_transform)] ) return "%s" % (ShortLayoutTypeNames[self.A.layout]) # def side_mode_name(self): return "%s" % (ShortSideModeNames[self.A.side_mode]) # def fill_mode_name(self): return "%s" % (ShortFillModeNames[self.A.fill_mode]) # def procedural_name(self): ''' The full procedural name indicates architecture, extended name, tile size, and layout. ''' threadblock = self.tile_description.procedural_name() opcode_class_name = OpcodeClassNames[self.tile_description.math_instruction.opcode_class] alignment = self.C.alignment return SubstituteTemplate( "cutlass_${opcode_class}_${extended_name}_${threadblock}_${layout}_${side_mode}_${fill_mode}_align${alignment}", { 'opcode_class': opcode_class_name, 'extended_name': self.extended_name(), 'threadblock': threadblock, 'layout': self.layout_name(), 'side_mode': self.side_mode_name(), 'fill_mode': self.fill_mode_name(), 'alignment': "%d" % alignment, } ) # def configuration_name(self): ''' The full procedural name indicates architecture, extended name, tile size, and layout. ''' return self.procedural_name() ################################################################################################### # # Emits single instances of a CUTLASS device-wide operator # ################################################################################################### # class EmitSymmUniversalInstance: ''' Responsible for emitting a CUTLASS template definition''' def __init__(self): self.symm_template = """ // Symm operator ${operation_name} using Operation_${operation_name} = typename cutlass::gemm::device::Symm< ${element_a}, ${layout_a}, ${side_mode}, ${fill_mode}, ${element_b}, ${layout_b}, ${element_c}, ${layout_c}, ${element_accumulator}, ${opcode_class}, ${arch}, cutlass::gemm::GemmShape<${threadblock_shape_m}, ${threadblock_shape_n}, ${threadblock_shape_k}>, cutlass::gemm::GemmShape<${warp_shape_m}, ${warp_shape_n}, ${warp_shape_k}>, cutlass::gemm::GemmShape<${instruction_shape_m}, ${instruction_shape_n}, ${instruction_shape_k}>, ${epilogue_functor}< ${element_c}, ${epilogue_vector_length}, ${element_accumulator}, ${element_epilogue} >, ${swizzling_functor}, ${stages}, ${align_a}, ${align_b}, ${split_k_serial}, ${math_operation} >; """ self.symm_complex_template = """ // Symm operator ${operation_name} using Operation_${operation_name} = typename cutlass::gemm::device::Symm< ${element_a}, ${layout_a}, ${side_mode}, ${fill_mode}, ${element_b}, ${layout_b}, ${element_c}, ${layout_c}, ${element_accumulator}, ${opcode_class}, ${arch}, cutlass::gemm::GemmShape<${threadblock_shape_m}, ${threadblock_shape_n}, ${threadblock_shape_k}>, cutlass::gemm::GemmShape<${warp_shape_m}, ${warp_shape_n}, ${warp_shape_k}>, cutlass::gemm::GemmShape<${instruction_shape_m}, ${instruction_shape_n}, ${instruction_shape_k}>, ${epilogue_functor}< ${element_c}, ${epilogue_vector_length}, ${element_accumulator}, ${element_epilogue} >, ${swizzling_functor}, ${stages}, ${align_a}, ${align_b}, ${split_k_serial}, ${math_operation}, ${blas_mode} >; """ def emit(self, operation): threadblock_shape = operation.tile_description.threadblock_shape warp_count = operation.tile_description.warp_count warp_shape = [threadblock_shape[idx] // warp_count[idx] for idx in range(3)] epilogue_vector_length = int(min(operation.C.alignment * DataTypeSize[operation.C.element], 128) / DataTypeSize[operation.C.element]) values = { 'operation_name': operation.procedural_name(), 'element_a': DataTypeTag[operation.A.element], 'layout_a': LayoutTag[operation.A.layout], 'side_mode': SideModeTag[operation.A.side_mode], 'fill_mode': FillModeTag[operation.A.fill_mode], 'element_b': DataTypeTag[operation.B.element], 'layout_b': LayoutTag[operation.B.layout], 'element_c': DataTypeTag[operation.C.element], 'layout_c': LayoutTag[operation.C.layout], 'element_accumulator': DataTypeTag[operation.accumulator_type()], 'opcode_class': OpcodeClassTag[operation.tile_description.math_instruction.opcode_class], 'arch': "cutlass::arch::Sm%d" % operation.arch, 'threadblock_shape_m': str(operation.tile_description.threadblock_shape[0]), 'threadblock_shape_n': str(operation.tile_description.threadblock_shape[1]), 'threadblock_shape_k': str(operation.tile_description.threadblock_shape[2]), 'warp_shape_m': str(warp_shape[0]), 'warp_shape_n': str(warp_shape[1]), 'warp_shape_k': str(warp_shape[2]), 'instruction_shape_m': str(operation.tile_description.math_instruction.instruction_shape[0]), 'instruction_shape_n': str(operation.tile_description.math_instruction.instruction_shape[1]), 'instruction_shape_k': str(operation.tile_description.math_instruction.instruction_shape[2]), 'epilogue_vector_length': str(epilogue_vector_length), 'element_epilogue': str(DataTypeTag[operation.element_epilogue]), 'epilogue_functor': EpilogueFunctorTag[operation.epilogue_functor], 'swizzling_functor': SwizzlingFunctorTag[operation.swizzling_functor], 'stages': str(operation.tile_description.stages), 'align_a': str(operation.A.alignment), 'align_b': str(operation.B.alignment), 'split_k_serial': 'false', 'math_operation': MathOperationTag[operation.tile_description.math_instruction.math_operation], 'blas_mode': BlasModeTag[operation.blas_mode] } symm_template = self.symm_complex_template if operation.is_complex() else self.symm_template return SubstituteTemplate(symm_template, values) ################################################################################################### ################################################################################################### # # Emitters functions for all targets # ################################################################################################### class EmitSymmConfigurationLibrary: def __init__(self, operation_path, configuration_name): self.configuration_name = configuration_name self.configuration_path = os.path.join(operation_path, "%s.cu" % configuration_name).replace('\\', '/') self.instance_emitter = { SymmKind.Universal: EmitSymmUniversalInstance, } self.symm_kind_wrappers = { SymmKind.Universal: 'SymmOperation', } self.instance_template = { SymmKind.Universal: """ ${compile_guard_start} manifest.append(new ${symm_kind}< Operation_${operation_name} >("${operation_name}")); ${compile_guard_end} """ } self.header_template = """ /* Generated by symm_operation.py - Do not edit. */ /////////////////////////////////////////////////////////////////////////////////////////////////// #include "cutlass/cutlass.h" #include "cutlass/library/library.h" #include "cutlass/library/manifest.h" #include "library_internal.h" #include "symm_operation.h" /////////////////////////////////////////////////////////////////////////////////////////////////// """ self.initialize_function_template = """ /////////////////////////////////////////////////////////////////////////////////////////////////// namespace cutlass { namespace library { /////////////////////////////////////////////////////////////////////////////////////////////////// void initialize_${configuration_name}(Manifest &manifest) { """ self.epilogue_template = """ } /////////////////////////////////////////////////////////////////////////////////////////////////// } // namespace library } // namespace cutlass /////////////////////////////////////////////////////////////////////////////////////////////////// """ def __enter__(self): self.configuration_file = open(self.configuration_path, "w") self.configuration_file.write(self.header_template) self.instance_definitions = [] self.instance_wrappers = [] self.operations = [] return self def emit(self, operation): emitter = self.instance_emitter[operation.symm_kind]() self.operations.append(operation) self.instance_definitions.append(emitter.emit(operation)) self.instance_wrappers.append(SubstituteTemplate(self.instance_template[operation.symm_kind], { 'configuration_name': self.configuration_name, 'operation_name': operation.procedural_name(), 'symm_kind': self.symm_kind_wrappers[operation.symm_kind], 'compile_guard_start': SubstituteTemplate(self.wmma_guard_start, {'sm_number': str(operation.arch)}) \ if operation.tile_description.math_instruction.opcode_class == OpcodeClass.WmmaTensorOp else "", 'compile_guard_end': "#endif" \ if operation.tile_description.math_instruction.opcode_class == OpcodeClass.WmmaTensorOp else "" })) def __exit__(self, exception_type, exception_value, traceback): # Write instance definitions in top-level namespace for instance_definition in self.instance_definitions: self.configuration_file.write(instance_definition) # Add wrapper objects within initialize() function self.configuration_file.write(SubstituteTemplate(self.initialize_function_template, { 'configuration_name': self.configuration_name })) for instance_wrapper in self.instance_wrappers: self.configuration_file.write(instance_wrapper) self.configuration_file.write(self.epilogue_template) self.configuration_file.close() ###################################################################################################
# # \file generator.py # # \brief Generates the CUTLASS Library's instances # # import enum import os.path import shutil import functools import operator from library import * ################################################################################################### # # Data structure modeling a TRMM operation # ################################################################################################### # class TrmmOperation: # def __init__(self, trmm_kind, arch, tile_description, A, B, C, element_epilogue, \ epilogue_functor = EpilogueFunctor.LinearCombination, swizzling_functor = SwizzlingFunctor.Identity8): self.operation_kind = OperationKind.Trmm self.arch = arch self.tile_description = tile_description self.trmm_kind = trmm_kind self.A = A self.B = B self.C = C self.element_epilogue = element_epilogue self.epilogue_functor = epilogue_functor self.swizzling_functor = swizzling_functor # def is_complex(self): complex_operators = [ MathOperation.multiply_add_complex, MathOperation.multiply_add_complex_gaussian, MathOperation.multiply_add_complex_fast_f32 ] return self.tile_description.math_instruction.math_operation in complex_operators return False # def is_planar_complex(self): # return self.trmm_kind in (TrmmKind.PlanarComplex, TrmmKind.PlanarComplexArray) return False # def accumulator_type(self): accum = self.tile_description.math_instruction.element_accumulator if self.is_complex(): return get_complex_from_real(accum) return accum # def short_math_name(self): if self.tile_description.math_instruction.math_operation == MathOperation.multiply_add_complex_gaussian: return "g%s" % ShortDataTypeNames[self.accumulator_type()] return ShortDataTypeNames[self.accumulator_type()] # def core_name(self): ''' The basic operation kind is prefixed with a letter indicating the accumulation type. ''' inst_shape = '' inst_operation = '' intermediate_type = '' math_operations_map = { MathOperation.xor_popc: 'xor', } if self.tile_description.math_instruction.opcode_class == OpcodeClass.TensorOp or \ self.tile_description.math_instruction.opcode_class == OpcodeClass.WmmaTensorOp: math_op = self.tile_description.math_instruction.math_operation math_op_string = math_operations_map[math_op] if math_op in math_operations_map.keys() else '' inst_shape = "%d%d%d" % tuple(self.tile_description.math_instruction.instruction_shape) inst_shape += math_op_string if self.tile_description.math_instruction.element_a != self.A.element and \ self.tile_description.math_instruction.element_a != self.tile_description.math_instruction.element_accumulator: intermediate_type = DataTypeNames[self.tile_description.math_instruction.element_a] return "%s%s%s%s" % (self.short_math_name(), inst_shape, intermediate_type, TrmmKindNames[self.trmm_kind]) # def extended_name(self): ''' Append data types if they differ from compute type. ''' if self.is_complex(): extended_name = "${core_name}" else: if self.C.element != self.tile_description.math_instruction.element_accumulator and \ self.A.element != self.tile_description.math_instruction.element_accumulator: extended_name = "${element_c}_${core_name}_${element_a}" elif self.C.element == self.tile_description.math_instruction.element_accumulator and \ self.A.element != self.tile_description.math_instruction.element_accumulator: extended_name = "${core_name}_${element_a}" else: extended_name = "${core_name}" extended_name = SubstituteTemplate(extended_name, { 'element_a': DataTypeNames[self.A.element], 'element_c': DataTypeNames[self.C.element], 'core_name': self.core_name() }) return extended_name # def layout_name(self): if self.is_complex() or self.is_planar_complex(): return "%s%s" % ( ShortComplexLayoutNames[(self.A.layout, self.A.complex_transform)], ShortComplexLayoutNames[(self.B.layout, self.B.complex_transform)] ) return "%s%s" % (ShortLayoutTypeNames[self.A.layout], ShortLayoutTypeNames[self.B.layout]) # def side_mode_name(self): return "%s" % (ShortSideModeNames[self.A.side_mode]) # def fill_mode_name(self): return "%s" % (ShortFillModeNames[self.A.fill_mode]) # def diag_type_name(self): return "%s" % (ShortDiagTypeNames[self.A.diag_type]) # def procedural_name(self): ''' The full procedural name indicates architecture, extended name, tile size, and layout. ''' threadblock = self.tile_description.procedural_name() opcode_class_name = OpcodeClassNames[self.tile_description.math_instruction.opcode_class] alignment = max([self.C.alignment]) return SubstituteTemplate( "cutlass_${opcode_class}_${extended_name}_${threadblock}_${layout}_${side_mode}_${fill_mode}_${diag_type}_align${alignment}", { 'opcode_class': opcode_class_name, 'extended_name': self.extended_name(), 'threadblock': threadblock, 'layout': self.layout_name(), 'side_mode': self.side_mode_name(), 'fill_mode': self.fill_mode_name(), 'diag_type': self.diag_type_name(), 'alignment': "%d" % self.C.alignment, } ) # def configuration_name(self): ''' The full procedural name indicates architecture, extended name, tile size, and layout. ''' return self.procedural_name() ################################################################################################### # # Emits single instances of a CUTLASS device-wide operator # ################################################################################################### # class EmitTrmmUniversalInstance: ''' Responsible for emitting a CUTLASS template definition''' def __init__(self): self.trmm_template = """ // Trmm operator ${operation_name} using Operation_${operation_name} = typename cutlass::gemm::device::Trmm< ${element_a}, ${layout_a}, ${side_mode}, ${fill_mode}, ${diag_type}, ${element_b}, ${layout_b}, ${element_c}, ${layout_c}, ${element_accumulator}, ${opcode_class}, ${arch}, cutlass::gemm::GemmShape<${threadblock_shape_m}, ${threadblock_shape_n}, ${threadblock_shape_k}>, cutlass::gemm::GemmShape<${warp_shape_m}, ${warp_shape_n}, ${warp_shape_k}>, cutlass::gemm::GemmShape<${instruction_shape_m}, ${instruction_shape_n}, ${instruction_shape_k}>, ${epilogue_functor}< ${element_c}, ${epilogue_vector_length}, ${element_accumulator}, ${element_epilogue}, cutlass::epilogue::thread::ScaleType::OnlyAlphaScaling >, ${swizzling_functor}, ${stages}, ${align_a}, ${align_b}, ${split_k_serial}, ${math_operation} >; """ self.trmm_complex_template = """ // Trmm operator ${operation_name} using Operation_${operation_name} = typename cutlass::gemm::device::Trmm< ${element_a}, ${layout_a}, ${side_mode}, ${fill_mode}, ${diag_type}, ${element_b}, ${layout_b}, ${element_c}, ${layout_c}, ${element_accumulator}, ${opcode_class}, ${arch}, cutlass::gemm::GemmShape<${threadblock_shape_m}, ${threadblock_shape_n}, ${threadblock_shape_k}>, cutlass::gemm::GemmShape<${warp_shape_m}, ${warp_shape_n}, ${warp_shape_k}>, cutlass::gemm::GemmShape<${instruction_shape_m}, ${instruction_shape_n}, ${instruction_shape_k}>, ${epilogue_functor}< ${element_c}, ${epilogue_vector_length}, ${element_accumulator}, ${element_epilogue}, cutlass::epilogue::thread::ScaleType::OnlyAlphaScaling >, ${swizzling_functor}, ${stages}, ${align_a}, ${align_b}, ${split_k_serial}, ${math_operation}, ${transform_a} >; """ def emit(self, operation): threadblock_shape = operation.tile_description.threadblock_shape warp_count = operation.tile_description.warp_count warp_shape = [threadblock_shape[idx] // warp_count[idx] for idx in range(3)] epilogue_vector_length = int(min(operation.C.alignment * DataTypeSize[operation.C.element], 128) / DataTypeSize[operation.C.element]) values = { 'operation_name': operation.procedural_name(), 'element_a': DataTypeTag[operation.A.element], 'layout_a': LayoutTag[operation.A.layout], 'side_mode' : SideModeTag[operation.A.side_mode], 'fill_mode': FillModeTag[operation.A.fill_mode], 'diag_type' : DiagTypeTag[operation.A.diag_type], 'element_b': DataTypeTag[operation.B.element], 'layout_b': LayoutTag[operation.B.layout], 'element_c': DataTypeTag[operation.C.element], 'layout_c': LayoutTag[operation.C.layout], 'element_accumulator': DataTypeTag[operation.accumulator_type()], 'opcode_class': OpcodeClassTag[operation.tile_description.math_instruction.opcode_class], 'arch': "cutlass::arch::Sm%d" % operation.arch, 'threadblock_shape_m': str(operation.tile_description.threadblock_shape[0]), 'threadblock_shape_n': str(operation.tile_description.threadblock_shape[1]), 'threadblock_shape_k': str(operation.tile_description.threadblock_shape[2]), 'warp_shape_m': str(warp_shape[0]), 'warp_shape_n': str(warp_shape[1]), 'warp_shape_k': str(warp_shape[2]), 'instruction_shape_m': str(operation.tile_description.math_instruction.instruction_shape[0]), 'instruction_shape_n': str(operation.tile_description.math_instruction.instruction_shape[1]), 'instruction_shape_k': str(operation.tile_description.math_instruction.instruction_shape[2]), 'epilogue_vector_length': str(epilogue_vector_length), 'element_epilogue': str(DataTypeTag[operation.element_epilogue]), 'epilogue_functor': EpilogueFunctorTag[operation.epilogue_functor], 'swizzling_functor': SwizzlingFunctorTag[operation.swizzling_functor], 'stages': str(operation.tile_description.stages), 'align_a': str(1), # TRMM A's alignment is always 1 for no padding to work until we make zfill work with variable bytes 'align_b': str(operation.B.alignment), 'split_k_serial': 'false', 'math_operation': MathOperationTag[operation.tile_description.math_instruction.math_operation], 'transform_a': ComplexTransformTag[operation.A.complex_transform] } trmm_template = self.trmm_complex_template if operation.is_complex() else self.trmm_template return SubstituteTemplate(trmm_template, values) ################################################################################################### ################################################################################################### # # Emitters functions for all targets # ################################################################################################### class EmitTrmmConfigurationLibrary: def __init__(self, operation_path, configuration_name): self.configuration_name = configuration_name self.configuration_path = os.path.join(operation_path, "%s.cu" % configuration_name).replace('\\', '/') self.instance_emitter = { TrmmKind.Universal: EmitTrmmUniversalInstance, } self.trmm_kind_wrappers = { TrmmKind.Universal: 'TrmmOperation', } self.instance_template = { TrmmKind.Universal: """ ${compile_guard_start} manifest.append(new ${trmm_kind}< Operation_${operation_name} >("${operation_name}")); ${compile_guard_end} """ } self.header_template = """ /* Generated by trmm_operation.py - Do not edit. */ /////////////////////////////////////////////////////////////////////////////////////////////////// #include "cutlass/cutlass.h" #include "cutlass/library/library.h" #include "cutlass/library/manifest.h" #include "library_internal.h" #include "trmm_operation.h" /////////////////////////////////////////////////////////////////////////////////////////////////// """ self.initialize_function_template = """ /////////////////////////////////////////////////////////////////////////////////////////////////// namespace cutlass { namespace library { /////////////////////////////////////////////////////////////////////////////////////////////////// void initialize_${configuration_name}(Manifest &manifest) { """ self.epilogue_template = """ } /////////////////////////////////////////////////////////////////////////////////////////////////// } // namespace library } // namespace cutlass /////////////////////////////////////////////////////////////////////////////////////////////////// """ def __enter__(self): self.configuration_file = open(self.configuration_path, "w") self.configuration_file.write(self.header_template) self.instance_definitions = [] self.instance_wrappers = [] self.operations = [] return self def emit(self, operation): emitter = self.instance_emitter[operation.trmm_kind]() self.operations.append(operation) self.instance_definitions.append(emitter.emit(operation)) self.instance_wrappers.append(SubstituteTemplate(self.instance_template[operation.trmm_kind], { 'configuration_name': self.configuration_name, 'operation_name': operation.procedural_name(), 'trmm_kind': self.trmm_kind_wrappers[operation.trmm_kind], 'compile_guard_start': SubstituteTemplate(self.wmma_guard_start, {'sm_number': str(operation.arch)}) \ if operation.tile_description.math_instruction.opcode_class == OpcodeClass.WmmaTensorOp else "", 'compile_guard_end': "#endif" \ if operation.tile_description.math_instruction.opcode_class == OpcodeClass.WmmaTensorOp else "" })) def __exit__(self, exception_type, exception_value, traceback): # Write instance definitions in top-level namespace for instance_definition in self.instance_definitions: self.configuration_file.write(instance_definition) # Add wrapper objects within initialize() function self.configuration_file.write(SubstituteTemplate(self.initialize_function_template, { 'configuration_name': self.configuration_name })) for instance_wrapper in self.instance_wrappers: self.configuration_file.write(instance_wrapper) self.configuration_file.write(self.epilogue_template) self.configuration_file.close() ###################################################################################################
################################################################################################# # # Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: BSD-3-Clause # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # 3. Neither the name of the copyright holder nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ################################################################################################# import distutils.cmd from setuptools import setup import setuptools.command.build_py import os # build rmm dependency class BuildRMM(distutils.cmd.Command): user_options = [] def initialize_options(self): pass def finalize_options(self): pass def run(self): try: import rmm except ImportError: print("installing rmm") os.system("git clone -b branch-22.10 --recurse-submodules https://github.com/rapidsai/rmm.git") os.chdir("./rmm") os.system("./build.sh librmm rmm") os.chdir("./python") os.system("python setup.py build_ext --inplace") os.system("python setup.py install") cutlass_path = os.getenv('CUTLASS_PATH') assert cutlass_path is not None, "Environment variable 'CUTLASS_PATH' is not defined." cuda_install_path = os.getenv('CUDA_INSTALL_PATH') assert cuda_install_path is not None, "Environment variable 'CUDA_INSTALL_PATH' is not defined." ext_modules = [] try: from pybind11.setup_helpers import Pybind11Extension, build_ext include_dirs = [ cutlass_path + "/include", cuda_install_path + "/include", cutlass_path + "/tools/util/include", cutlass_path + "/test", cutlass_path + "/tools/library/scripts/pycutlass/googletest/googletest/include" ] ext_modules = [ Pybind11Extension("cutlass", ["src/cpp/cutlass.cpp"], include_dirs=include_dirs, extra_compile_args=["-fpermissive", "-w", "-std=c++17"]), Pybind11Extension("cute", ["src/cpp/cute.cpp"], include_dirs=include_dirs, extra_compile_args=["-fpermissive", "-w", "-std=c++17"]) ] except ImportError: pass setup( name="PyCutlass", version="0.0.1", author="Zhaodong Chen; Andrew Kerr; Haicheng Wu; Szymon Migacz; Graham Markall", author_email="[email protected]", description="Python interface for CUTLASS", classifiers=[ "Programming Language :: Python :: 3", "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", ], package_dir={"": "src"}, packages=['pycutlass', 'pycutlass.utils', 'pycutlass.test'], setup_requires=["pybind11", "numpy<1.23"], install_requires=[ "numpy<1.23", 'pybind11', 'cuda-python>=11.8.0', 'typeguard', 'bfloat16', 'typing', 'scikit-build', 'treelib' ], cmdclass={ 'rmm': BuildRMM }, ext_modules=ext_modules, python_requires=">=3.6", )
################################################################################################# # # Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: BSD-3-Clause # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # 3. Neither the name of the copyright holder nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ################################################################################################# ## Test case generator for SM80 import pycutlass from pycutlass import * from pycutlass.test import * from pycutlass.utils.device import device_cc import unittest import xmlrunner import argparse # # Create GEMM operation # @unittest.skipIf(device_cc() < 80, "Device compute capability is insufficient for SM80 tests.") def TestGemmOperator(gemm_kind, math_inst, layout, alignment, tiling, arch, mixed=False, epilogue_functor=None, swizzling_functor=cutlass.IdentitySwizzle1, **kwargs): """ Test GEMM Operation based on configuration """ if "data_type" in kwargs.keys(): data_type = kwargs["data_type"] else: if mixed or math_inst.element_a == cutlass.bfloat16: data_type = [ math_inst.element_a, math_inst.element_b, math_inst.element_accumulator, math_inst.element_accumulator ] else: data_type = [ math_inst.element_a, math_inst.element_b, math_inst.element_a, math_inst.element_accumulator ] tile_description = TileDescription( tiling[0], tiling[1], tiling[2], math_inst ) A = TensorDescription( data_type[0], layout[0], alignment[0] ) B = TensorDescription( data_type[1], layout[1], alignment[1] ) C = TensorDescription( data_type[2], layout[2], alignment[2] ) element_epilogue = data_type[3] if epilogue_functor is None: epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, element_epilogue) if gemm_kind == GemmKind.Universal: operation = GemmOperationUniversal( arch=arch, tile_description=tile_description, A=A, B=B, C=C, epilogue_functor=epilogue_functor, swizzling_functor=swizzling_functor ) if A.layout in [cutlass.ColumnMajorInterleaved32, cutlass.RowMajorInterleaved32]: return test_all_gemm(operation, "interleaved") else: return test_all_gemm(operation, "universal") elif gemm_kind == GemmKind.Grouped: operation = GemmOperationGrouped( arch, tile_description, A, B, C, epilogue_functor, swizzling_functor, precompute_mode=kwargs["precompute_mode"] ) testbed = TestbedGrouped(operation=operation) return testbed.run(24) else: raise NotImplementedError("the gemm kind is not implemented") def TestConv2dOperator(math_inst, alignment, tiling, arch, stride_supports=[StrideSupport.Strided, StrideSupport.Strided, StrideSupport.Strided], epilogue_functor=None, swizzling_functor=cutlass.IdentitySwizzle1, interleaved=False, **kwargs): """ Test Conv2d Operation based on configurations """ mixeds = [False, True, False] conv_kinds = [cutlass.conv.Operator.fprop, cutlass.conv.Operator.dgrad, cutlass.conv.Operator.wgrad] results = [] default_swizzling_functor = swizzling_functor if "layout" in kwargs.keys(): layout = kwargs["layout"] else: layout = (cutlass.TensorNHWC, cutlass.TensorNHWC, cutlass.TensorNHWC) for mixed, conv_kind, stride_support in zip(mixeds, conv_kinds, stride_supports): if "data_type" in kwargs.keys(): data_type = kwargs["data_type"] else: if mixed or math_inst.element_a == cutlass.bfloat16: data_type = [ math_inst.element_a, math_inst.element_b, math_inst.element_accumulator, math_inst.element_accumulator ] else: data_type = [ math_inst.element_a, math_inst.element_b, math_inst.element_a, math_inst.element_accumulator ] # skip Int8 Conv Backward if data_type[0] == cutlass.int8 and conv_kind in [cutlass.conv.Operator.dgrad, cutlass.conv.Operator.wgrad]: continue A = TensorDescription( element=data_type[0], layout=layout[0], alignment=alignment[0]) B = TensorDescription( element=data_type[1], layout=layout[1], alignment=alignment[1]) C = TensorDescription( element=data_type[2], layout=layout[2], alignment=alignment[2]) tile_description = TileDescription( threadblock_shape=tiling[0], stages=tiling[1], warp_count=tiling[2], math_instruction=math_inst ) if conv_kind == cutlass.conv.Operator.dgrad and stride_support == StrideSupport.Strided: swizzling_functor = cutlass.StridedDgradIdentitySwizzle1 else: swizzling_functor = default_swizzling_functor if epilogue_functor is None: epilogue_functor_ = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, data_type[3]) operation = Conv2dOperation( conv_kind=conv_kind, iterator_algorithm=cutlass.conv.IteratorAlgorithm.optimized, arch=arch, tile_description=tile_description, A=A, B=B, C=C, stride_support=stride_support, epilogue_functor=epilogue_functor_, swizzling_functor=swizzling_functor ) results.append(test_all_conv2d(operation, interleaved=interleaved)) return results class Test_SM80(unittest.TestCase): def test_SM80_TensorOp_16816(self): math_instructions = [ MathInstruction( [16, 8, 16], cutlass.float16, cutlass.float16, cutlass.float32, cutlass.OpClass.TensorOp, MathOperation.multiply_add ), MathInstruction( [16, 8, 16], cutlass.float16, cutlass.float16, cutlass.float16, cutlass.OpClass.TensorOp, MathOperation.multiply_add ), MathInstruction( [16, 8, 16], cutlass.bfloat16, cutlass.bfloat16, cutlass.float32, cutlass.OpClass.TensorOp, MathOperation.multiply_add ) ] layouts = [ (cutlass.RowMajor, cutlass.RowMajor, cutlass.RowMajor), (cutlass.ColumnMajor, cutlass.RowMajor, cutlass.RowMajor), (cutlass.RowMajor, cutlass.ColumnMajor, cutlass.RowMajor) ] alignments = [ (8, 8, 8), (4, 8, 8), (8, 4, 8) ] tilings = [ ([256, 128, 32], 3, [4, 2, 1]), ([64, 256, 32], 4, [1, 4, 1]), ([128, 64, 64], 3, [2, 2, 1]) ] for math_inst, layout, alignment, tiling in zip(math_instructions, layouts, alignments, tilings): self.assertTrue(TestGemmOperator(GemmKind.Universal, math_inst, layout, alignment, tiling, 80, False)) self.assertTrue(TestGemmOperator(GemmKind.Grouped, math_inst, layout, alignment, tiling, 80, True, precompute_mode=SchedulerMode.Host)) stride_supports = [StrideSupport.Strided, StrideSupport.Strided, StrideSupport.Strided] results = TestConv2dOperator(math_inst, alignment, tiling, 80, stride_supports=stride_supports) for res in results: self.assertTrue(res) def test_SM80_TensorOp_1688(self): # tf32 is not supported by most of python environment. Skip the test self.assertTrue(True) def test_SM80_TensorOp_1688_fast_math(self): math_instructions = [ MathInstruction( [16, 8, 8], cutlass.tfloat32, cutlass.tfloat32, cutlass.float32, cutlass.OpClass.TensorOp, MathOperation.multiply_add ), MathInstruction( [16, 8, 8], cutlass.float16, cutlass.float16, cutlass.float32, cutlass.OpClass.TensorOp, MathOperation.multiply_add_fast_f16 ), MathInstruction( [16, 8, 8], cutlass.bfloat16, cutlass.bfloat16, cutlass.float32, cutlass.OpClass.TensorOp, MathOperation.multiply_add_fast_bf16 ), MathInstruction( [16, 8, 8], cutlass.float32, cutlass.float32, cutlass.float32, cutlass.OpClass.TensorOp, MathOperation.multiply_add_fast_f32 ) ] layouts = [ (cutlass.RowMajor, cutlass.RowMajor, cutlass.ColumnMajor), (cutlass.RowMajor, cutlass.ColumnMajor, cutlass.ColumnMajor), (cutlass.ColumnMajor, cutlass.RowMajor, cutlass.ColumnMajor), (cutlass.ColumnMajor, cutlass.ColumnMajor, cutlass.RowMajor) ] alignments = [ (4, 4, 4), (4, 2, 4), (2, 4, 4), (2, 2, 4) ] tilings = [ ([128, 256, 16], 3, [4, 2, 1]), ([64, 256, 16], 4, [1, 4, 1]), ([128, 64, 32], 3, [2, 2, 1]), ([256, 64, 32], 3, [4, 2, 1]) ] data_type = [ cutlass.float32, cutlass.float32, cutlass.float32, cutlass.float32 ] for math_inst, layout, alignment, tiling in zip(math_instructions, layouts, alignments, tilings): self.assertTrue( TestGemmOperator( GemmKind.Universal, math_inst, layout, alignment, tiling, 80, False, data_type=data_type)) self.assertTrue( TestGemmOperator( GemmKind.Grouped, math_inst, layout, alignment, tiling, 80, True, precompute_mode=SchedulerMode.Device, data_type=data_type)) stride_supports = [StrideSupport.Unity, StrideSupport.Strided, StrideSupport.Unity] results = TestConv2dOperator(math_inst, alignment, tiling, 80, stride_supports=stride_supports, data_type=data_type) for res in results: self.assertTrue(res) def test_SM80_TensorOp_884(self): math_inst = MathInstruction( [8, 8, 4], cutlass.float64, cutlass.float64, cutlass.float64, cutlass.OpClass.TensorOp, MathOperation.multiply_add ) layout = (cutlass.ColumnMajor, cutlass.ColumnMajor, cutlass.ColumnMajor) alignment = (1, 1, 1) tiling = ([64, 256, 16], 3, [2, 4, 1]) data_type = [cutlass.float64, cutlass.float64, cutlass.float64, cutlass.float64] self.assertTrue(TestGemmOperator(GemmKind.Universal, math_inst, layout, alignment, tiling, 80, False, data_type=data_type)) self.assertTrue(TestGemmOperator(GemmKind.Grouped, math_inst, layout, alignment, tiling, 80, True, precompute_mode=SchedulerMode.Device, data_type=data_type)) stride_supports = [StrideSupport.Unity, StrideSupport.Strided, StrideSupport.Unity] results = TestConv2dOperator(math_inst, alignment, tiling, 80, stride_supports=stride_supports, data_type=data_type) for res in results: self.assertTrue(res) def test_SM80_TensorOp_16832_TN(self): math_inst = MathInstruction( [16, 8, 32], cutlass.int8, cutlass.int8, cutlass.int32, cutlass.OpClass.TensorOp, MathOperation.multiply_add_saturate ) layout = (cutlass.RowMajor, cutlass.ColumnMajor, cutlass.ColumnMajor) alignment = (16, 16, 4) alignment_mixed = (16, 16, 16) tiling = ([128, 256, 64], 3, [2, 4, 1]) data_type = [cutlass.int8, cutlass.int8, cutlass.int32, cutlass.int32] data_type_mixed = [cutlass.int8, cutlass.int8, cutlass.int8, cutlass.float32] self.assertTrue(TestGemmOperator(GemmKind.Universal, math_inst, layout, alignment, tiling, 80, False, data_type=data_type)) self.assertTrue(TestGemmOperator(GemmKind.Grouped, math_inst, layout, alignment_mixed, tiling, 80, True, precompute_mode=SchedulerMode.Device, data_type=data_type_mixed)) stride_supports = [StrideSupport.Strided, StrideSupport.Strided, StrideSupport.Strided] results = TestConv2dOperator(math_inst, alignment, tiling, 80, stride_supports=stride_supports, data_type=data_type) for res in results: self.assertTrue(res) def test_SM80_Simt_f32(self): math_inst = MathInstruction( [1, 1, 1], cutlass.float32, cutlass.float32, cutlass.float32, cutlass.OpClass.Simt, MathOperation.multiply_add ) layout = (cutlass.RowMajor, cutlass.RowMajor, cutlass.RowMajor) alignment = (1, 1, 1) tiling = ([128, 256, 8], 4, [2, 4, 1]) data_type = [cutlass.float32, cutlass.float32, cutlass.float32, cutlass.float32] self.assertTrue(TestGemmOperator(GemmKind.Universal, math_inst, layout, alignment, tiling, 80, False, data_type=data_type)) self.assertTrue(TestGemmOperator(GemmKind.Grouped, math_inst, layout, alignment, tiling, 80, True, precompute_mode=SchedulerMode.Host, data_type=data_type)) stride_supports = [StrideSupport.Strided, StrideSupport.Strided, StrideSupport.Strided] results = TestConv2dOperator(math_inst, alignment, tiling, 80, stride_supports=stride_supports, data_type=data_type) for res in results: self.assertTrue(res) def test_SM80_Simt_f64(self): math_inst = MathInstruction( [1, 1, 1], cutlass.float64, cutlass.float64, cutlass.float64, cutlass.OpClass.Simt, MathOperation.multiply_add ) layout = (cutlass.RowMajor, cutlass.RowMajor, cutlass.ColumnMajor) alignment = (1, 1, 1) tiling = ([64, 128, 8], 5, [2, 2, 1]) data_type = [cutlass.float64, cutlass.float64, cutlass.float64, cutlass.float64] self.assertTrue(TestGemmOperator(GemmKind.Universal, math_inst, layout, alignment, tiling, 80, False, data_type=data_type)) self.assertTrue(TestGemmOperator(GemmKind.Grouped, math_inst, layout, alignment, tiling, 80, True, precompute_mode=SchedulerMode.Device, data_type=data_type)) stride_supports = [StrideSupport.Strided, StrideSupport.Strided, StrideSupport.Strided] results = TestConv2dOperator(math_inst, alignment, tiling, 80, stride_supports=stride_supports, data_type=data_type) for res in results: self.assertTrue(res) def test_SM80_TensorOp_16832_Interleaved(self): math_inst = MathInstruction( [16, 8, 32], cutlass.int8, cutlass.int8, cutlass.int32, cutlass.OpClass.TensorOp, MathOperation.multiply_add_saturate ) layout = (cutlass.ColumnMajorInterleaved32, cutlass.RowMajorInterleaved32, cutlass.ColumnMajorInterleaved32) alignment_mixed = (16, 16, 8) tiling = ([256, 64, 64], 4, [4, 1, 1]) data_type_mixed = [cutlass.int8, cutlass.int8, cutlass.int8, cutlass.float32] epilogue_functor = FastLinearCombinationClamp( data_type_mixed[2], alignment_mixed[2] ) self.assertTrue(TestGemmOperator(GemmKind.Universal, math_inst, layout, alignment_mixed, tiling, 80, False, data_type=data_type_mixed, epilogue_functor=epilogue_functor)) stride_supports = [StrideSupport.Strided, StrideSupport.Strided, StrideSupport.Strided] layout = [cutlass.TensorNC32HW32, cutlass.TensorC32RSK32, cutlass.TensorNC32HW32] results = TestConv2dOperator(math_inst, alignment_mixed, tiling, 80, stride_supports=stride_supports, data_type=data_type_mixed, layout=layout, interleaved=True) for res in results: self.assertTrue(res) def SM80_SparseTensorOp_16832(self): pass def SM80_PlanarComplexTensorOp_16816(self): pass def SM80_SparseTensorOp_16816_fast_math(self): pass def SM80_TensorOp_1688_complex(self): pass def SM80_TensorOp_1688_fast_fp32_math_complex(self): pass def SM80_TensorOp_1688_rank_k(self): pass def SM80_TensorOp_1688_rank_k_complex(self): pass def SM80_TensorOp_1688_trmm(self): pass def SM80_TensorOp_1688_trmm_complex(self): pass def SM80_TensorOp_1688_symm(self): pass def SM80_TensorOp_1688_symm_complex(self): pass def SM80_TensorOp_884_complex(self): pass def SM80_TensorOp_884_complex_gaussian(self): pass def SM80_TensorOp_884_rank_k(self): pass def SM80_TensorOp_884_rank_k_complex(self): pass def SM80_TensorOp_884_rank_k_complex_gaussian(self): pass def SM80_TensorOp_884_trmm(self): pass def SM80_TensorOp_884_trmm_complex(self): pass def SM80_TensorOp_884_trmm_complex_gaussian(self): pass def SM80_TensorOp_884_symm(self): pass def SM80_TensorOp_884_symm_complex(self): pass def SM80_TensorOp_884_symm_complex_gaussian(self): pass def SM80_SparseTensorOp_16864_TN(self): pass def SM80_TensorOp_16864_TN(self): pass def SM80_SparseTensorOp_168128_TN(self): pass def SM80_TensorOp_16864_Interleaved(self): pass def SM80_TensorOp_168256(self): pass def SM80_Simt_complex(self): pass def argumentParser(): parser = argparse.ArgumentParser(description="Entrypoint for PyCutlass testing on Ampere architecture.") parser.add_argument("-j", "--junit_path", help="The absolute path to the directory for generating a junit xml report", default="") return parser.parse_args() if __name__ == '__main__': pycutlass.get_memory_pool(2**20, 2**34) pycutlass.compiler.nvcc() args = argumentParser() if args.junit_path: unittest.main(argv=[''], testRunner=xmlrunner.XMLTestRunner(output=args.junit_path)) else: unittest.main(argv=[''])
################################################################################################# # # Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: BSD-3-Clause # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # 3. Neither the name of the copyright holder nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ################################################################################################# """ Test cases for frontends """ import pycutlass import unittest from pycutlass import * from pycutlass.utils.device import device_cc class Test_Frontend(unittest.TestCase): def setUp(self) -> None: # # define the cutlass operator # cc = device_cc() math_inst = MathInstruction( [1, 1, 1], cutlass.float32, cutlass.float32, cutlass.float32, cutlass.OpClass.Simt, MathOperation.multiply_add ) stages = 2 tile_description = TileDescription( [128, 128, 8], stages, [2, 4, 1], math_inst ) A = TensorDescription( cutlass.float32, cutlass.RowMajor, 1 ) B = TensorDescription( cutlass.float32, cutlass.RowMajor, 1 ) C = TensorDescription( cutlass.float32, cutlass.RowMajor, 1 ) epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, cutlass.float32) self.operation = GemmOperationUniversal( arch=cc, tile_description=tile_description, A=A, B=B, C=C, epilogue_functor=epilogue_functor, swizzling_functor=cutlass.IdentitySwizzle1 ) pycutlass.compiler.add_module([self.operation,]) def test_torch_frontend(self): try: import torch except: self.assertTrue(False, "Unable to import torch") problem_size = cutlass.gemm.GemmCoord(512, 256, 128) tensor_A = torch.ceil(torch.empty(size=(problem_size.m(), problem_size.k()), dtype=torch.float32, device="cuda").uniform_(-8.5, 7.5)) tensor_B = torch.ceil(torch.empty(size=(problem_size.k(), problem_size.n()), dtype=torch.float32, device="cuda").uniform_(-8.5, 7.5)) tensor_C = torch.ceil(torch.empty(size=(problem_size.m(), problem_size.n()), dtype=torch.float32, device="cuda").uniform_(-8.5, 7.5)) tensor_D = torch.empty_like(tensor_C) alpha = 1.0 beta = 0.0 arguments = GemmArguments( operation=self.operation, problem_size=problem_size, A=tensor_A, B=tensor_B, C=tensor_C, D=tensor_D, output_op=self.operation.epilogue_type(alpha, beta), gemm_mode=cutlass.gemm.Mode.Gemm, split_k_splices=1 ) self.operation.run(arguments) arguments.sync() tensor_D_ref = alpha * tensor_A @ tensor_B + beta * tensor_C self.assertTrue(torch.equal(tensor_D, tensor_D_ref)) def test_cupy_frontend(self): try: import cupy as cp except: self.assertTrue(False, "Unable to import cupy") cp.cuda.set_allocator(rmm.rmm_cupy_allocator) problem_size = cutlass.gemm.GemmCoord(512, 256, 128) tensor_A = cp.ceil(cp.random.uniform(low=-8.5, high=7.5, size=(problem_size.m(), problem_size.k()), dtype=cp.float32)) tensor_B = cp.ceil(cp.random.uniform(low=-8.5, high=7.5, size=(problem_size.k(), problem_size.n()), dtype=cp.float32)) tensor_C = cp.ceil(cp.random.uniform(low=-8.5, high=7.5, size=(problem_size.m(), problem_size.n()), dtype=cp.float32)) tensor_D = cp.ones_like(tensor_C) alpha = 1.0 beta = 1.0 tensor_D_ref = alpha * tensor_A @ tensor_B + beta * tensor_C arguments = GemmArguments( operation=self.operation, problem_size=problem_size, A=tensor_A, B=tensor_B, C=tensor_C, D=tensor_D, output_op=self.operation.epilogue_type(alpha, beta), gemm_mode=cutlass.gemm.Mode.Gemm, split_k_splices=1 ) self.operation.run(arguments) arguments.sync() self.assertTrue(cp.array_equal(tensor_D, tensor_D_ref)) if __name__ == '__main__': pycutlass.get_memory_pool(2**32, 2**32) unittest.main()
################################################################################################# # # Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: BSD-3-Clause # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # 3. Neither the name of the copyright holder nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ################################################################################################# # test/unit/conv/device/conv2d_fprop_implicit_gemm_tf32nhwc_tf32nhwc_f32nhwc_tensor_op_f32_sm80.cu import pycutlass from pycutlass import * from pycutlass.test import * from pycutlass.utils.device import device_cc import unittest @unittest.skipIf(device_cc() < 80, "Device compute capability is insufficient for SM80 tests.") class Conv2dDgradImplicitGemmTF32nhwcTF32nhwcTF32nhwcTensorOpF32SM80(unittest.TestCase): def test_SM80_Device_Conv2d_Dgrad_Analytic_ImplicitGemm_tf32nhwc_tf32nhwc_f32nhwc_tensor_op_f32(self): math_inst = MathInstruction( instruction_shape=[16, 8, 8], element_a=cutlass.float32, element_b=cutlass.float32, element_accumulator=cutlass.float32, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add ) A = TensorDescription( element=math_inst.element_a, layout=cutlass.TensorNHWC, alignment=4) B = TensorDescription( element=math_inst.element_b, layout=cutlass.TensorNHWC, alignment=4) C = TensorDescription( element=cutlass.float32, layout=cutlass.TensorNHWC, alignment=8) tile_description = TileDescription( threadblock_shape=[128, 128, 16], stages=3, warp_count=[2, 2, 1], math_instruction=math_inst ) epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, cutlass.float32) operation = Conv2dOperation( conv_kind=cutlass.conv.Operator.dgrad, iterator_algorithm=cutlass.conv.IteratorAlgorithm.analytic, arch=80, tile_description=tile_description, A=A, B=B, C=C, stride_support=StrideSupport.Unity, epilogue_functor=epilogue_functor, swizzling_functor=cutlass.IdentitySwizzle1 ) self.assertTrue(test_all_conv2d(operation)) def test_SM80_Device_Conv2d_Dgrad_Optimized_ImplicitGemm_tf32nhwc_tf32nhwc_f32nhwc_tensor_op_f32(self): math_inst = MathInstruction( instruction_shape=[16, 8, 8], element_a=cutlass.float32, element_b=cutlass.float32, element_accumulator=cutlass.float32, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add ) A = TensorDescription( element=math_inst.element_a, layout=cutlass.TensorNHWC, alignment=4) B = TensorDescription( element=math_inst.element_b, layout=cutlass.TensorNHWC, alignment=4) C = TensorDescription( element=cutlass.float32, layout=cutlass.TensorNHWC, alignment=8) tile_description = TileDescription( threadblock_shape=[128, 128, 16], stages=3, warp_count=[2, 2, 1], math_instruction=math_inst ) epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, cutlass.float32) operation = Conv2dOperation( conv_kind=cutlass.conv.Operator.dgrad, iterator_algorithm=cutlass.conv.IteratorAlgorithm.optimized, arch=80, tile_description=tile_description, A=A, B=B, C=C, stride_support=StrideSupport.Unity, epilogue_functor=epilogue_functor, swizzling_functor=cutlass.IdentitySwizzle1 ) self.assertTrue(test_all_conv2d(operation)) if __name__ == '__main__': pycutlass.get_memory_pool(2**26, 2**26) unittest.main()
################################################################################################# # # Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: BSD-3-Clause # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # 3. Neither the name of the copyright holder nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ################################################################################################# # test/unit/conv/device/conv2d_fprop_implicit_gemm_f16nhwc_f16nhwc_f32nhwc_tensor_op_f32_sm80.cu import pycutlass from pycutlass import * from pycutlass.test import * from pycutlass.utils.device import device_cc import unittest @unittest.skipIf(device_cc() < 80, "Device compute capability is insufficient for SM80 tests.") class Conv2dDgradImplicitGemmF16nhwcF16nhwcF32nhwcTensorOpF32SM80(unittest.TestCase): def test_SM80_Device_Conv2d_Dgrad_Optimized_ImplicitGemm_f16nhwc_f16nhwc_f32nhwc_tensor_op_f32_unity_stride_stage3(self): math_inst = MathInstruction( instruction_shape=[16, 8, 16], element_a=cutlass.float16, element_b=cutlass.float16, element_accumulator=cutlass.float32, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add ) A = TensorDescription( element=math_inst.element_a, layout=cutlass.TensorNHWC, alignment=8) B = TensorDescription( element=math_inst.element_b, layout=cutlass.TensorNHWC, alignment=8) C = TensorDescription( element=cutlass.float32, layout=cutlass.TensorNHWC, alignment=4) tile_description = TileDescription( threadblock_shape=[128, 128, 32], stages=3, warp_count=[2, 2, 1], math_instruction=math_inst ) epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, cutlass.float32) operation = Conv2dOperation( conv_kind=cutlass.conv.Operator.dgrad, iterator_algorithm=cutlass.conv.IteratorAlgorithm.optimized, arch=80, tile_description=tile_description, A=A, B=B, C=C, stride_support=StrideSupport.Unity, epilogue_functor=epilogue_functor, swizzling_functor=cutlass.IdentitySwizzle1 ) self.assertTrue(test_all_conv2d(operation)) def test_SM80_Device_Conv2d_Dgrad_Optimized_ImplicitGemm_f16nhwc_f16nhwc_f32nhwc_tensor_op_f32_unity_stride_stage4(self): math_inst = MathInstruction( instruction_shape=[16, 8, 16], element_a=cutlass.float16, element_b=cutlass.float16, element_accumulator=cutlass.float32, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add ) A = TensorDescription( element=math_inst.element_a, layout=cutlass.TensorNHWC, alignment=8) B = TensorDescription( element=math_inst.element_b, layout=cutlass.TensorNHWC, alignment=8) C = TensorDescription( element=cutlass.float32, layout=cutlass.TensorNHWC, alignment=4) tile_description = TileDescription( threadblock_shape=[128, 128, 32], stages=4, warp_count=[2, 2, 1], math_instruction=math_inst ) epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, cutlass.float32) operation = Conv2dOperation( conv_kind=cutlass.conv.Operator.dgrad, iterator_algorithm=cutlass.conv.IteratorAlgorithm.optimized, arch=80, tile_description=tile_description, A=A, B=B, C=C, stride_support=StrideSupport.Unity, epilogue_functor=epilogue_functor, swizzling_functor=cutlass.IdentitySwizzle1 ) self.assertTrue(test_all_conv2d(operation)) def test_SM80_Device_Conv2d_Dgrad_Optimized_ImplicitGemm_f16nhwc_f16nhwc_f32nhwc_tensor_op_f32_unity_stride_stage3_64(self): math_inst = MathInstruction( instruction_shape=[16, 8, 16], element_a=cutlass.float16, element_b=cutlass.float16, element_accumulator=cutlass.float32, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add ) A = TensorDescription( element=math_inst.element_a, layout=cutlass.TensorNHWC, alignment=8) B = TensorDescription( element=math_inst.element_b, layout=cutlass.TensorNHWC, alignment=8) C = TensorDescription( element=cutlass.float32, layout=cutlass.TensorNHWC, alignment=4) tile_description = TileDescription( threadblock_shape=[128, 128, 64], stages=3, warp_count=[2, 2, 1], math_instruction=math_inst ) epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, cutlass.float32) operation = Conv2dOperation( conv_kind=cutlass.conv.Operator.dgrad, iterator_algorithm=cutlass.conv.IteratorAlgorithm.optimized, arch=80, tile_description=tile_description, A=A, B=B, C=C, stride_support=StrideSupport.Unity, epilogue_functor=epilogue_functor, swizzling_functor=cutlass.IdentitySwizzle1 ) self.assertTrue(test_all_conv2d(operation)) def test_SM80_Device_Conv2d_Dgrad_Optimized_ImplicitGemm_f16nhwc_f16nhwc_f32nhwc_tensor_op_f32_unity_stride_stage4_64(self): math_inst = MathInstruction( instruction_shape=[16, 8, 16], element_a=cutlass.float16, element_b=cutlass.float16, element_accumulator=cutlass.float32, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add ) A = TensorDescription( element=math_inst.element_a, layout=cutlass.TensorNHWC, alignment=8) B = TensorDescription( element=math_inst.element_b, layout=cutlass.TensorNHWC, alignment=8) C = TensorDescription( element=cutlass.float32, layout=cutlass.TensorNHWC, alignment=4) tile_description = TileDescription( threadblock_shape=[128, 128, 64], stages=4, warp_count=[2, 2, 1], math_instruction=math_inst ) epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, cutlass.float32) operation = Conv2dOperation( conv_kind=cutlass.conv.Operator.dgrad, iterator_algorithm=cutlass.conv.IteratorAlgorithm.optimized, arch=80, tile_description=tile_description, A=A, B=B, C=C, stride_support=StrideSupport.Unity, epilogue_functor=epilogue_functor, swizzling_functor=cutlass.IdentitySwizzle1 ) self.assertTrue(test_all_conv2d(operation)) if __name__ == '__main__': pycutlass.get_memory_pool(2**26, 2**26) unittest.main()
################################################################################################# # # Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: BSD-3-Clause # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # 3. Neither the name of the copyright holder nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ################################################################################################# # test/unit/conv/device/conv2d_dgrad_implicit_gemm_f16nhwc_f16nhwc_f16nhwc_tensor_op_f16_sm80.cu from pycutlass.conv2d_operation import * from pycutlass import * from pycutlass.test import * from pycutlass.utils.device import device_cc import unittest @unittest.skipIf(device_cc() < 80, "Device compute capability is insufficient for SM80 tests.") class Conv2dDgradImplicitGemmF16nhwcF16nhwcF16nhwcTensorOpF16SM80(unittest.TestCase): def test_SM80_Device_Conv2d_Dgrad_Analytic_ImplicitGemm_f16nhwc_f16nhwc_f16nhwc_tensor_op_f16(self): math_inst = MathInstruction( instruction_shape=[16, 8, 16], element_a=cutlass.float16, element_b=cutlass.float16, element_accumulator=cutlass.float16, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add ) A = TensorDescription( element=math_inst.element_a, layout=cutlass.TensorNHWC, alignment=8) B = TensorDescription( element=math_inst.element_b, layout=cutlass.TensorNHWC, alignment=8) C = TensorDescription( element=cutlass.float16, layout=cutlass.TensorNHWC, alignment=8) tile_description = TileDescription( threadblock_shape=[128, 128, 64], stages=3, warp_count=[2, 2, 1], math_instruction=math_inst ) epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, cutlass.float16) operation = Conv2dOperation( conv_kind=cutlass.conv.Operator.dgrad, iterator_algorithm=cutlass.conv.IteratorAlgorithm.analytic, arch=80, tile_description=tile_description, A=A, B=B, C=C, stride_support=StrideSupport.Unity, epilogue_functor=epilogue_functor, swizzling_functor=cutlass.IdentitySwizzle1 ) self.assertTrue(test_all_conv2d(operation)) def test_SM80_Device_Conv2d_Dgrad_Optimized_ImplicitGemm_f16nhwc_f16nhwc_f16nhwc_tensor_op_f16(self): math_inst = MathInstruction( instruction_shape=[16, 8, 16], element_a=cutlass.float16, element_b=cutlass.float16, element_accumulator=cutlass.float16, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add ) A = TensorDescription( element=math_inst.element_a, layout=cutlass.TensorNHWC, alignment=8) B = TensorDescription( element=math_inst.element_b, layout=cutlass.TensorNHWC, alignment=8) C = TensorDescription( element=cutlass.float16, layout=cutlass.TensorNHWC, alignment=8) tile_description = TileDescription( threadblock_shape=[128, 128, 64], stages=3, warp_count=[2, 2, 1], math_instruction=math_inst ) epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, cutlass.float16) operation = Conv2dOperation( conv_kind=cutlass.conv.Operator.dgrad, iterator_algorithm=cutlass.conv.IteratorAlgorithm.optimized, arch=80, tile_description=tile_description, A=A, B=B, C=C, stride_support=StrideSupport.Unity, epilogue_functor=epilogue_functor, swizzling_functor=cutlass.IdentitySwizzle1 ) self.assertTrue(test_all_conv2d(operation)) def test_SM80_Device_Conv2d_Dgrad_Analytic_ImplicitGemm_f16nhwc_f16nhwc_f16nhwc_tensor_op_f16_align4(self): math_inst = MathInstruction( instruction_shape=[16, 8, 16], element_a=cutlass.float16, element_b=cutlass.float16, element_accumulator=cutlass.float16, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add ) A = TensorDescription( element=math_inst.element_a, layout=cutlass.TensorNHWC, alignment=4) B = TensorDescription( element=math_inst.element_b, layout=cutlass.TensorNHWC, alignment=4) C = TensorDescription( element=cutlass.float16, layout=cutlass.TensorNHWC, alignment=4) tile_description = TileDescription( threadblock_shape=[128, 128, 64], stages=3, warp_count=[2, 2, 1], math_instruction=math_inst ) epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, cutlass.float16) operation = Conv2dOperation( conv_kind=cutlass.conv.Operator.dgrad, iterator_algorithm=cutlass.conv.IteratorAlgorithm.analytic, arch=80, tile_description=tile_description, A=A, B=B, C=C, stride_support=StrideSupport.Unity, epilogue_functor=epilogue_functor, swizzling_functor=cutlass.IdentitySwizzle1 ) problem_sizes = [ cutlass.conv.Conv2dProblemSize( cutlass.Tensor4DCoord(1, 4, 4, 12), cutlass.Tensor4DCoord(8, 3, 3, 12), cutlass.Tensor4DCoord(0, 0, 0, 0), cutlass.MatrixCoord(3, 3), cutlass.MatrixCoord(1, 1), cutlass.conv.Mode.cross_correlation, 1, 1 ), ] self.assertTrue(test_all_conv2d(operation, problem_sizes)) def test_SM80_Device_Conv2d_Dgrad_Optimized_ImplicitGemm_f16nhwc_f16nhwc_f16nhwc_tensor_op_f16_align4(self): math_inst = MathInstruction( instruction_shape=[16, 8, 16], element_a=cutlass.float16, element_b=cutlass.float16, element_accumulator=cutlass.float16, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add ) A = TensorDescription( element=math_inst.element_a, layout=cutlass.TensorNHWC, alignment=4) B = TensorDescription( element=math_inst.element_b, layout=cutlass.TensorNHWC, alignment=4) C = TensorDescription( element=cutlass.float16, layout=cutlass.TensorNHWC, alignment=4) tile_description = TileDescription( threadblock_shape=[128, 128, 64], stages=3, warp_count=[2, 2, 1], math_instruction=math_inst ) epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, cutlass.float16) operation = Conv2dOperation( conv_kind=cutlass.conv.Operator.dgrad, iterator_algorithm=cutlass.conv.IteratorAlgorithm.optimized, arch=80, tile_description=tile_description, A=A, B=B, C=C, stride_support=StrideSupport.Unity, epilogue_functor=epilogue_functor, swizzling_functor=cutlass.IdentitySwizzle1 ) problem_sizes = [ cutlass.conv.Conv2dProblemSize( cutlass.Tensor4DCoord(1, 4, 4, 12), cutlass.Tensor4DCoord(8, 3, 3, 12), cutlass.Tensor4DCoord(0, 0, 0, 0), cutlass.MatrixCoord(3, 3), cutlass.MatrixCoord(1, 1), cutlass.conv.Mode.cross_correlation, 1, 1 ), ] self.assertTrue(test_all_conv2d(operation, problem_sizes)) if __name__ == '__main__': pycutlass.get_memory_pool(2**26, 2**26) unittest.main()
################################################################################################# # # Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: BSD-3-Clause # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # 3. Neither the name of the copyright holder nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ################################################################################################# # test/unit/conv/device/conv2d_strided_dgrad_implicit_gemm_f16nhwc_f16nhwc_f32nhwc_tensor_op_f32_sm80.cu import pycutlass from pycutlass import * from pycutlass.test import * from pycutlass.utils.device import device_cc import unittest @unittest.skipIf(device_cc() < 80, "Device compute capability is insufficient for SM80 tests.") class Conv2dStridedDgradImplicitGemmF16NHWCF16NHWCF32NHWCTensorOpF32SM80(unittest.TestCase): def test_SM80_Device_Conv2d_Strided_Dgrad_Analytic_ImplicitGemm_f16nhwc_f16nhwc_f32nhwc_tensor_op_f32_128x128_32x3_64x64x32(self): math_inst = MathInstruction( instruction_shape=[16, 8, 16], element_a=cutlass.float16, element_b=cutlass.float16, element_accumulator=cutlass.float32, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add ) A = TensorDescription( element=math_inst.element_a, layout=cutlass.TensorNHWC, alignment=8) B = TensorDescription( element=math_inst.element_b, layout=cutlass.TensorNHWC, alignment=8) C = TensorDescription( element=cutlass.float32, layout=cutlass.TensorNHWC, alignment=4) tile_description = TileDescription( threadblock_shape=[128, 128, 32], stages=3, warp_count=[2, 2, 1], math_instruction=math_inst ) epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, cutlass.float32) operation = Conv2dOperation( conv_kind=cutlass.conv.Operator.dgrad, iterator_algorithm=cutlass.conv.IteratorAlgorithm.analytic, arch=80, tile_description=tile_description, A=A, B=B, C=C, stride_support=StrideSupport.Strided, epilogue_functor=epilogue_functor, swizzling_functor=cutlass.StridedDgradIdentitySwizzle1 ) self.assertTrue(test_all_conv2d(operation)) def test_SM80_Device_Conv2d_Strided_Dgrad_Analytic_ImplicitGemm_f16nhwc_f16nhwc_f32nhwc_tensor_op_f32_128x256_64x3_64x64x64(self): math_inst = MathInstruction( instruction_shape=[16, 8, 16], element_a=cutlass.float16, element_b=cutlass.float16, element_accumulator=cutlass.float32, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add ) A = TensorDescription( element=math_inst.element_a, layout=cutlass.TensorNHWC, alignment=8) B = TensorDescription( element=math_inst.element_b, layout=cutlass.TensorNHWC, alignment=8) C = TensorDescription( element=cutlass.float32, layout=cutlass.TensorNHWC, alignment=4) tile_description = TileDescription( threadblock_shape=[128, 256, 64], stages=3, warp_count=[2, 4, 1], math_instruction=math_inst ) epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, cutlass.float32) operation = Conv2dOperation( conv_kind=cutlass.conv.Operator.dgrad, iterator_algorithm=cutlass.conv.IteratorAlgorithm.analytic, arch=80, tile_description=tile_description, A=A, B=B, C=C, stride_support=StrideSupport.Strided, epilogue_functor=epilogue_functor, swizzling_functor=cutlass.StridedDgradIdentitySwizzle1 ) self.assertTrue(test_all_conv2d(operation)) def test_SM80_Device_Conv2d_Strided_Dgrad_Analytic_ImplicitGemm_f16nhwc_f16nhwc_f32nhwc_tensor_op_f32_align4_128x128_32x3_64x64x32(self): math_inst = MathInstruction( instruction_shape=[16, 8, 16], element_a=cutlass.float16, element_b=cutlass.float16, element_accumulator=cutlass.float32, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add ) A = TensorDescription( element=math_inst.element_a, layout=cutlass.TensorNHWC, alignment=4) B = TensorDescription( element=math_inst.element_b, layout=cutlass.TensorNHWC, alignment=4) C = TensorDescription( element=cutlass.float32, layout=cutlass.TensorNHWC, alignment=4) tile_description = TileDescription( threadblock_shape=[128, 128, 32], stages=3, warp_count=[2, 2, 1], math_instruction=math_inst ) epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, cutlass.float32) operation = Conv2dOperation( conv_kind=cutlass.conv.Operator.dgrad, iterator_algorithm=cutlass.conv.IteratorAlgorithm.analytic, arch=80, tile_description=tile_description, A=A, B=B, C=C, stride_support=StrideSupport.Strided, epilogue_functor=epilogue_functor, swizzling_functor=cutlass.StridedDgradIdentitySwizzle1 ) problem_sizes = [ cutlass.conv.Conv2dProblemSize( cutlass.Tensor4DCoord(1, 4, 4, 12), cutlass.Tensor4DCoord(8, 3, 3, 12), cutlass.Tensor4DCoord(0, 0, 0, 0), cutlass.MatrixCoord(3, 3), cutlass.MatrixCoord(1, 1), cutlass.conv.Mode.cross_correlation, 1, 1 ), ] self.assertTrue(test_all_conv2d(operation, problem_sizes)) def test_SM80_Device_Conv2d_Strided_Dgrad_Optimized_ImplicitGemm_f16nhwc_f16nhwc_f32nhwc_tensor_op_f32_128x128_32x3_64x64x32(self): math_inst = MathInstruction( instruction_shape=[16, 8, 16], element_a=cutlass.float16, element_b=cutlass.float16, element_accumulator=cutlass.float32, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add ) A = TensorDescription( element=math_inst.element_a, layout=cutlass.TensorNHWC, alignment=8) B = TensorDescription( element=math_inst.element_b, layout=cutlass.TensorNHWC, alignment=8) C = TensorDescription( element=cutlass.float32, layout=cutlass.TensorNHWC, alignment=4) tile_description = TileDescription( threadblock_shape=[128, 128, 32], stages=3, warp_count=[2, 2, 1], math_instruction=math_inst ) epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, cutlass.float32) operation = Conv2dOperation( conv_kind=cutlass.conv.Operator.dgrad, iterator_algorithm=cutlass.conv.IteratorAlgorithm.optimized, arch=80, tile_description=tile_description, A=A, B=B, C=C, stride_support=StrideSupport.Strided, epilogue_functor=epilogue_functor, swizzling_functor=cutlass.StridedDgradIdentitySwizzle1 ) self.assertTrue(test_all_conv2d(operation)) def test_SM80_Device_Conv2d_Strided_Dgrad_Optimized_ImplicitGemm_f16nhwc_f16nhwc_f32nhwc_tensor_op_f32_128x128_32x3_64x64x32_align4(self): math_inst = MathInstruction( instruction_shape=[16, 8, 16], element_a=cutlass.float16, element_b=cutlass.float16, element_accumulator=cutlass.float32, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add ) A = TensorDescription( element=math_inst.element_a, layout=cutlass.TensorNHWC, alignment=4) B = TensorDescription( element=math_inst.element_b, layout=cutlass.TensorNHWC, alignment=4) C = TensorDescription( element=cutlass.float32, layout=cutlass.TensorNHWC, alignment=4) tile_description = TileDescription( threadblock_shape=[128, 128, 32], stages=3, warp_count=[2, 2, 1], math_instruction=math_inst ) epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, cutlass.float32) operation = Conv2dOperation( conv_kind=cutlass.conv.Operator.dgrad, iterator_algorithm=cutlass.conv.IteratorAlgorithm.optimized, arch=80, tile_description=tile_description, A=A, B=B, C=C, stride_support=StrideSupport.Strided, epilogue_functor=epilogue_functor, swizzling_functor=cutlass.StridedDgradIdentitySwizzle1 ) problem_sizes = [ cutlass.conv.Conv2dProblemSize( cutlass.Tensor4DCoord(1, 56, 56, 12), cutlass.Tensor4DCoord(8, 1, 1, 12), cutlass.Tensor4DCoord(0, 0, 0, 0), cutlass.MatrixCoord(2, 2), cutlass.MatrixCoord(1, 1), cutlass.conv.Mode.cross_correlation, 1, 1 ), cutlass.conv.Conv2dProblemSize( cutlass.Tensor4DCoord(1, 55, 55, 12), cutlass.Tensor4DCoord(8, 1, 1, 12), cutlass.Tensor4DCoord(0, 0, 0, 0), cutlass.MatrixCoord(2, 2), cutlass.MatrixCoord(1, 1), cutlass.conv.Mode.cross_correlation, 1, 1 ), ] self.assertTrue(test_all_conv2d(operation, problem_sizes)) if __name__ == '__main__': pycutlass.get_memory_pool(2**26, 2**26) unittest.main()
################################################################################################# # # Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: BSD-3-Clause # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # 3. Neither the name of the copyright holder nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ################################################################################################# # test/unit/conv/device/conv2d_fprop_few_channels_f16nhwc_f16nhwc_f16nhwc_tensor_op_f32_sm80.cu import pycutlass from pycutlass.test import * from pycutlass.utils.device import device_cc import unittest @unittest.skipIf(device_cc() < 80, "Device compute capability is insufficient for SM80 tests.") def conv2d_few_channel_problemsizes(channels): problem_sizes = [ cutlass.conv.Conv2dProblemSize( cutlass.Tensor4DCoord(1, 8, 8, channels), cutlass.Tensor4DCoord(16, 3, 3, channels), cutlass.Tensor4DCoord(1, 1, 1, 1), cutlass.MatrixCoord(2, 2), cutlass.MatrixCoord(1, 1), cutlass.conv.Mode.cross_correlation, 1, 1 ), cutlass.conv.Conv2dProblemSize( cutlass.Tensor4DCoord(1, 16, 16, channels), cutlass.Tensor4DCoord(16, 3, 3, channels), cutlass.Tensor4DCoord(1, 1, 1, 1), cutlass.MatrixCoord(2, 2), cutlass.MatrixCoord(1, 1), cutlass.conv.Mode.cross_correlation, 1, 1 ), cutlass.conv.Conv2dProblemSize( cutlass.Tensor4DCoord(1, 16, 16, channels), cutlass.Tensor4DCoord(16, 7, 7, channels), cutlass.Tensor4DCoord(1, 1, 1, 1), cutlass.MatrixCoord(1, 1), cutlass.MatrixCoord(1, 1), cutlass.conv.Mode.cross_correlation, 1, 1 ), cutlass.conv.Conv2dProblemSize( cutlass.Tensor4DCoord(1, 224, 224, channels), cutlass.Tensor4DCoord(32, 7, 7, channels), cutlass.Tensor4DCoord(1, 1, 1, 1), cutlass.MatrixCoord(1, 1), cutlass.MatrixCoord(1, 1), cutlass.conv.Mode.cross_correlation, 1, 1 ), cutlass.conv.Conv2dProblemSize( cutlass.Tensor4DCoord(1, 224, 224, channels), cutlass.Tensor4DCoord(64, 7, 7, channels), cutlass.Tensor4DCoord(1, 1, 1, 1), cutlass.MatrixCoord(2, 2), cutlass.MatrixCoord(1, 1), cutlass.conv.Mode.cross_correlation, 1, 1 ), cutlass.conv.Conv2dProblemSize( cutlass.Tensor4DCoord(1, 224, 224, channels), cutlass.Tensor4DCoord(64, 5, 5, channels), cutlass.Tensor4DCoord(1, 1, 1, 1), cutlass.MatrixCoord(1, 1), cutlass.MatrixCoord(1, 1), cutlass.conv.Mode.cross_correlation, 1, 1 ), cutlass.conv.Conv2dProblemSize( cutlass.Tensor4DCoord(1, 224, 224, channels), cutlass.Tensor4DCoord(64, 5, 5, channels), cutlass.Tensor4DCoord(1, 1, 1, 1), cutlass.MatrixCoord(2, 2), cutlass.MatrixCoord(1, 1), cutlass.conv.Mode.cross_correlation, 1, 1 ), ] return problem_sizes class Conv2dFpropFewChannelsF16NHWCF16NHWCF16HNWCTensorOpF32SM80(unittest.TestCase): def test_SM80_Device_Conv2d_Fprop_Few_Channels_ImplicitGemm_f16nhwc_f16nhwc_f16nhwc_tensor_op_f32_channels_2(self): math_inst = MathInstruction( instruction_shape=[16, 8, 16], element_a=cutlass.float16, element_b=cutlass.float16, element_accumulator=cutlass.float32, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add ) A = TensorDescription( element=math_inst.element_a, layout=cutlass.TensorNHWC, alignment=2) B = TensorDescription( element=math_inst.element_b, layout=cutlass.TensorNHWC, alignment=2) C = TensorDescription( element=cutlass.float16, layout=cutlass.TensorNHWC, alignment=8) tile_description = TileDescription( threadblock_shape=[128, 128, 64], stages=3, warp_count=[2, 2, 1], math_instruction=math_inst ) epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, cutlass.float32) operation = Conv2dOperation( conv_kind=cutlass.conv.Operator.fprop, iterator_algorithm=cutlass.conv.IteratorAlgorithm.few_channels, arch=80, tile_description=tile_description, A=A, B=B, C=C, stride_support=StrideSupport.Strided, epilogue_functor=epilogue_functor, swizzling_functor=cutlass.IdentitySwizzle1 ) self.assertTrue(test_all_conv2d(operation, conv2d_few_channel_problemsizes(2))) def test_SM80_Device_Conv2d_Fprop_Few_Channels_ImplicitGemm_f16nhwc_f16nhwc_f16nhwc_tensor_op_f32_channels_1(self): math_inst = MathInstruction( instruction_shape=[16, 8, 8], element_a=cutlass.float16, element_b=cutlass.float16, element_accumulator=cutlass.float32, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add ) A = TensorDescription( element=math_inst.element_a, layout=cutlass.TensorNHWC, alignment=1) B = TensorDescription( element=math_inst.element_b, layout=cutlass.TensorNHWC, alignment=1) C = TensorDescription( element=cutlass.float16, layout=cutlass.TensorNHWC, alignment=8) tile_description = TileDescription( threadblock_shape=[128, 128, 32], stages=2, warp_count=[2, 2, 1], math_instruction=math_inst ) epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, cutlass.float32) operation = Conv2dOperation( conv_kind=cutlass.conv.Operator.fprop, iterator_algorithm=cutlass.conv.IteratorAlgorithm.few_channels, arch=80, tile_description=tile_description, A=A, B=B, C=C, stride_support=StrideSupport.Strided, epilogue_functor=epilogue_functor, swizzling_functor=cutlass.IdentitySwizzle1 ) self.assertTrue(test_all_conv2d(operation, conv2d_few_channel_problemsizes(1))) if __name__ == '__main__': pycutlass.get_memory_pool(2**26, 2**26) unittest.main()
################################################################################################# # # Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: BSD-3-Clause # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # 3. Neither the name of the copyright holder nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ################################################################################################# # test/unit/conv/device/conv2d_wgrad_implicit_gemm_f16nhwc_f16nhwc_f16nhwc_tensor_op_f16_sm80.cu import pycutlass from pycutlass import * from pycutlass.test import * from pycutlass.utils.device import device_cc import unittest @unittest.skipIf(device_cc() < 80, "Device compute capability is insufficient for SM80 tests.") class Conv2dWgradImplicitGemmF16nhwcF16nhwcF32nhwcTensorOpF32SM80(unittest.TestCase): def test_Device_Conv2d_Wgrad_Analytic_ImplicitGemm_f16nhwc_f16nhwc_f32nhwc_tensor_op_f32(self): math_inst = MathInstruction( instruction_shape=[16, 8, 8], element_a=cutlass.float16, element_b=cutlass.float16, element_accumulator=cutlass.float32, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add ) A = TensorDescription( element=math_inst.element_a, layout=cutlass.TensorNHWC, alignment=8) B = TensorDescription( element=math_inst.element_b, layout=cutlass.TensorNHWC, alignment=8) C = TensorDescription( element=cutlass.float32, layout=cutlass.TensorNHWC, alignment=4) tile_description = TileDescription( threadblock_shape=[128, 128, 16], stages=3, warp_count=[2, 2, 1], math_instruction=math_inst ) epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, cutlass.float32) operation = Conv2dOperation( conv_kind=cutlass.conv.Operator.wgrad, iterator_algorithm=cutlass.conv.IteratorAlgorithm.analytic, arch=80, tile_description=tile_description, A=A, B=B, C=C, stride_support=StrideSupport.Strided, epilogue_functor=epilogue_functor, swizzling_functor=cutlass.IdentitySwizzle1 ) self.assertTrue(test_all_conv2d(operation)) def test_SM80_Device_Conv2d_Wgrad_Optimized_ImplicitGemm_f16nhwc_f16nhwc_f32nhwc_tensor_op_f32(self): math_inst = MathInstruction( instruction_shape=[16, 8, 8], element_a=cutlass.float16, element_b=cutlass.float16, element_accumulator=cutlass.float32, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add ) A = TensorDescription( element=math_inst.element_a, layout=cutlass.TensorNHWC, alignment=8) B = TensorDescription( element=math_inst.element_b, layout=cutlass.TensorNHWC, alignment=8) C = TensorDescription( element=cutlass.float32, layout=cutlass.TensorNHWC, alignment=4) tile_description = TileDescription( threadblock_shape=[128, 128, 16], stages=3, warp_count=[2, 2, 1], math_instruction=math_inst ) epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, cutlass.float32) operation = Conv2dOperation( conv_kind=cutlass.conv.Operator.wgrad, iterator_algorithm=cutlass.conv.IteratorAlgorithm.optimized, arch=80, tile_description=tile_description, A=A, B=B, C=C, stride_support=StrideSupport.Strided, epilogue_functor=epilogue_functor, swizzling_functor=cutlass.IdentitySwizzle1 ) self.assertTrue(test_all_conv2d(operation)) def test_SM80_Device_Conv2d_Wgrad_Optimized_ImplicitGemm_f16nhwc_f16nhwc_f32nhwc_tensor_op_f32_64x256_32x4_64x64x32(self): math_inst = MathInstruction( instruction_shape=[16, 8, 16], element_a=cutlass.float16, element_b=cutlass.float16, element_accumulator=cutlass.float32, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add ) A = TensorDescription( element=math_inst.element_a, layout=cutlass.TensorNHWC, alignment=8) B = TensorDescription( element=math_inst.element_b, layout=cutlass.TensorNHWC, alignment=8) C = TensorDescription( element=cutlass.float32, layout=cutlass.TensorNHWC, alignment=4) tile_description = TileDescription( threadblock_shape=[64, 256, 32], stages=3, warp_count=[1, 4, 1], math_instruction=math_inst ) epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, cutlass.float32) operation = Conv2dOperation( conv_kind=cutlass.conv.Operator.wgrad, iterator_algorithm=cutlass.conv.IteratorAlgorithm.optimized, arch=80, tile_description=tile_description, A=A, B=B, C=C, stride_support=StrideSupport.Strided, epilogue_functor=epilogue_functor, swizzling_functor=cutlass.IdentitySwizzle1 ) self.assertTrue(test_all_conv2d(operation)) def test_Device_Conv2d_Wgrad_Analytic_ImplicitGemm_f16nhwc_f16nhwc_f32nhwc_tensor_op_f32_align4(self): math_inst = MathInstruction( instruction_shape=[16, 8, 8], element_a=cutlass.float16, element_b=cutlass.float16, element_accumulator=cutlass.float32, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add ) A = TensorDescription( element=math_inst.element_a, layout=cutlass.TensorNHWC, alignment=4) B = TensorDescription( element=math_inst.element_b, layout=cutlass.TensorNHWC, alignment=4) C = TensorDescription( element=cutlass.float32, layout=cutlass.TensorNHWC, alignment=4) tile_description = TileDescription( threadblock_shape=[128, 128, 16], stages=3, warp_count=[2, 2, 1], math_instruction=math_inst ) epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, cutlass.float32) operation = Conv2dOperation( conv_kind=cutlass.conv.Operator.wgrad, iterator_algorithm=cutlass.conv.IteratorAlgorithm.analytic, arch=80, tile_description=tile_description, A=A, B=B, C=C, stride_support=StrideSupport.Strided, epilogue_functor=epilogue_functor, swizzling_functor=cutlass.IdentitySwizzle1 ) problem_sizes = [ cutlass.conv.Conv2dProblemSize( cutlass.Tensor4DCoord(1, 4, 4, 12), cutlass.Tensor4DCoord(8, 3, 3, 12), cutlass.Tensor4DCoord(0, 0, 0, 0), cutlass.MatrixCoord(3, 3), cutlass.MatrixCoord(1, 1), cutlass.conv.Mode.cross_correlation, 1, 1 ), ] self.assertTrue(test_all_conv2d(operation, problem_sizes)) def test_Device_Conv2d_Wgrad_Optimized_ImplicitGemm_f16nhwc_f16nhwc_f32nhwc_tensor_op_f32_align4(self): math_inst = MathInstruction( instruction_shape=[16, 8, 8], element_a=cutlass.float16, element_b=cutlass.float16, element_accumulator=cutlass.float32, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add ) A = TensorDescription( element=math_inst.element_a, layout=cutlass.TensorNHWC, alignment=4) B = TensorDescription( element=math_inst.element_b, layout=cutlass.TensorNHWC, alignment=4) C = TensorDescription( element=cutlass.float32, layout=cutlass.TensorNHWC, alignment=4) tile_description = TileDescription( threadblock_shape=[128, 128, 16], stages=3, warp_count=[2, 2, 1], math_instruction=math_inst ) epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, cutlass.float32) operation = Conv2dOperation( conv_kind=cutlass.conv.Operator.wgrad, iterator_algorithm=cutlass.conv.IteratorAlgorithm.optimized, arch=80, tile_description=tile_description, A=A, B=B, C=C, stride_support=StrideSupport.Strided, epilogue_functor=epilogue_functor, swizzling_functor=cutlass.IdentitySwizzle1 ) problem_sizes = [ cutlass.conv.Conv2dProblemSize( cutlass.Tensor4DCoord(1, 4, 4, 12), cutlass.Tensor4DCoord(8, 3, 3, 12), cutlass.Tensor4DCoord(0, 0, 0, 0), cutlass.MatrixCoord(3, 3), cutlass.MatrixCoord(1, 1), cutlass.conv.Mode.cross_correlation, 1, 1 ), ] self.assertTrue(test_all_conv2d(operation, problem_sizes)) if __name__ == '__main__': pycutlass.get_memory_pool(2**26, 2**26) unittest.main()
################################################################################################# # # Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: BSD-3-Clause # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # 3. Neither the name of the copyright holder nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ################################################################################################# # test/unit/conv/device/conv2d_dgrad_implicit_gemm_f32nhwc_f32nhwc_f32nhwc_simt_f32_sm80.cu import pycutlass from pycutlass.conv2d_operation import * from pycutlass import * from pycutlass.test import * from pycutlass.utils.device import device_cc import unittest @unittest.skipIf(device_cc() < 80, "Device compute capability is insufficient for SM80 tests.") class Conv2dDgradImplicitGemmF32nhwcF32nhwcF32nhwcSimtF32SM80(unittest.TestCase): def test_SM80_Device_Conv2d_Fprop_Analytic_ImplicitGemm_f32nhwc_f32nhwc_f32nhwc_simt_f32(self): math_inst = MathInstruction( instruction_shape=[1, 1, 1], element_a=cutlass.float32, element_b=cutlass.float32, element_accumulator=cutlass.float32, opcode_class=cutlass.OpClass.Simt, math_operation=MathOperation.multiply_add ) A = TensorDescription( element=math_inst.element_a, layout=cutlass.TensorNHWC, alignment=4) B = TensorDescription( element=math_inst.element_b, layout=cutlass.TensorNHWC, alignment=4) C = TensorDescription( element=cutlass.float32, layout=cutlass.TensorNHWC, alignment=1) tile_description = TileDescription( threadblock_shape=[128, 128, 8], stages=4, warp_count=[4, 2, 1], math_instruction=math_inst ) epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, cutlass.float32) operation = Conv2dOperation( conv_kind=cutlass.conv.Operator.dgrad, iterator_algorithm=cutlass.conv.IteratorAlgorithm.analytic, arch=80, tile_description=tile_description, A=A, B=B, C=C, stride_support=StrideSupport.Unity, epilogue_functor=epilogue_functor, swizzling_functor=cutlass.IdentitySwizzle1 ) self.assertTrue(test_all_conv2d(operation)) def test_SM80_Device_Conv2d_Dgrad_Optimized_ImplicitGemm_f32nhwc_f32nhwc_f32nhwc_simt_f32(self): math_inst = MathInstruction( instruction_shape=[1, 1, 1], element_a=cutlass.float32, element_b=cutlass.float32, element_accumulator=cutlass.float32, opcode_class=cutlass.OpClass.Simt, math_operation=MathOperation.multiply_add ) A = TensorDescription( element=math_inst.element_a, layout=cutlass.TensorNHWC, alignment=4) B = TensorDescription( element=math_inst.element_b, layout=cutlass.TensorNHWC, alignment=4) C = TensorDescription( element=cutlass.float32, layout=cutlass.TensorNHWC, alignment=1) tile_description = TileDescription( threadblock_shape=[128, 128, 8], stages=4, warp_count=[2, 4, 1], math_instruction=math_inst ) epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, cutlass.float32) operation = Conv2dOperation( conv_kind=cutlass.conv.Operator.dgrad, iterator_algorithm=cutlass.conv.IteratorAlgorithm.optimized, arch=80, tile_description=tile_description, A=A, B=B, C=C, stride_support=StrideSupport.Unity, epilogue_functor=epilogue_functor, swizzling_functor=cutlass.IdentitySwizzle1 ) self.assertTrue(test_all_conv2d(operation)) if __name__ == '__main__': pycutlass.get_memory_pool(2**26, 2**26) unittest.main()
################################################################################################# # # Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: BSD-3-Clause # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # 3. Neither the name of the copyright holder nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ################################################################################################# # test/unit/conv/device/conv2d_fprop_implicit_gemm_f32nhwc_f32nhwc_f32nhwc_simt_f32_sm80.cu import pycutlass from pycutlass.conv2d_operation import * from pycutlass import * from pycutlass.test import * from pycutlass.utils.device import device_cc import unittest @unittest.skipIf(device_cc() < 80, "Device compute capability is insufficient for SM80 tests.") class Conv2dFpropImplicitGemmF32nhwcF32nhwcF32nhwcSimtF32SM80(unittest.TestCase): def test_SM80_Device_Conv2d_Fprop_Analytic_ImplicitGemm_f32nhwc_f32nhwc_f32nhwc_simt_f32(self): math_inst = MathInstruction( instruction_shape=[1, 1, 1], element_a=cutlass.float32, element_b=cutlass.float32, element_accumulator=cutlass.float32, opcode_class=cutlass.OpClass.Simt, math_operation=MathOperation.multiply_add ) A = TensorDescription( element=math_inst.element_a, layout=cutlass.TensorNHWC, alignment=4) B = TensorDescription( element=math_inst.element_b, layout=cutlass.TensorNHWC, alignment=4) C = TensorDescription( element=cutlass.float32, layout=cutlass.TensorNHWC, alignment=1) tile_description = TileDescription( threadblock_shape=[128, 128, 8], stages=4, warp_count=[4, 2, 1], math_instruction=math_inst ) epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, cutlass.float32) operation = Conv2dOperation( conv_kind=cutlass.conv.Operator.fprop, iterator_algorithm=cutlass.conv.IteratorAlgorithm.analytic, arch=80, tile_description=tile_description, A=A, B=B, C=C, stride_support=StrideSupport.Strided, epilogue_functor=epilogue_functor, swizzling_functor=cutlass.IdentitySwizzle2 ) self.assertTrue(test_all_conv2d(operation)) def test_SM80_Device_Conv2d_Fprop_Optimized_ImplicitGemm_f32nhwc_f32nhwc_f32nhwc_simt_f32(self): math_inst = MathInstruction( instruction_shape=[1, 1, 1], element_a=cutlass.float32, element_b=cutlass.float32, element_accumulator=cutlass.float32, opcode_class=cutlass.OpClass.Simt, math_operation=MathOperation.multiply_add ) A = TensorDescription( element=math_inst.element_a, layout=cutlass.TensorNHWC, alignment=4) B = TensorDescription( element=math_inst.element_b, layout=cutlass.TensorNHWC, alignment=4) C = TensorDescription( element=cutlass.float32, layout=cutlass.TensorNHWC, alignment=1) tile_description = TileDescription( threadblock_shape=[128, 128, 8], stages=4, warp_count=[2, 4, 1], math_instruction=math_inst ) epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, cutlass.float32) operation = Conv2dOperation( conv_kind=cutlass.conv.Operator.fprop, iterator_algorithm=cutlass.conv.IteratorAlgorithm.optimized, arch=80, tile_description=tile_description, A=A, B=B, C=C, stride_support=StrideSupport.Strided, epilogue_functor=epilogue_functor, swizzling_functor=cutlass.IdentitySwizzle1 ) self.assertTrue(test_all_conv2d(operation)) if __name__ == '__main__': pycutlass.get_memory_pool(2**26, 2**26) unittest.main()
################################################################################################# # # Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: BSD-3-Clause # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # 3. Neither the name of the copyright holder nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ################################################################################################# # test/unit/conv/device/conv2d_wgrad_implicit_gemm_f16nhwc_f16nhwc_f16nhwc_tensor_op_f16_sm80.cu import pycutlass from pycutlass import * from pycutlass.test import * from pycutlass.utils.device import device_cc import unittest @unittest.skipIf(device_cc() < 80, "Device compute capability is insufficient for SM80 tests.") class Conv2dWgradImplicitGemmF16nhwcF16nhwcF16nhwcTensorOpF16SM80(unittest.TestCase): def test_Device_Conv2d_Wgrad_Analytic_ImplicitGemm_f16nhwc_f16nhwc_f16nhwc_tensor_op_f16(self): math_inst = MathInstruction( instruction_shape=[16, 8, 16], element_a=cutlass.float16, element_b=cutlass.float16, element_accumulator=cutlass.float16, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add ) A = TensorDescription( element=math_inst.element_a, layout=cutlass.TensorNHWC, alignment=8) B = TensorDescription( element=math_inst.element_b, layout=cutlass.TensorNHWC, alignment=8) C = TensorDescription( element=cutlass.float16, layout=cutlass.TensorNHWC, alignment=8) tile_description = TileDescription( threadblock_shape=[128, 128, 64], stages=3, warp_count=[2, 2, 1], math_instruction=math_inst ) epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, cutlass.float16 ) operation = Conv2dOperation( conv_kind=cutlass.conv.Operator.wgrad, iterator_algorithm=cutlass.conv.IteratorAlgorithm.analytic, arch=80, tile_description=tile_description, A=A, B=B, C=C, stride_support=StrideSupport.Strided, epilogue_functor=epilogue_functor, swizzling_functor=cutlass.IdentitySwizzle1 ) self.assertTrue(test_all_conv2d(operation)) def test_Device_Conv2d_Wgrad_Optimized_ImplicitGemm_f16nhwc_f16nhwc_f16nhwc_tensor_op_f16(self): math_inst = MathInstruction( instruction_shape=[16, 8, 16], element_a=cutlass.float16, element_b=cutlass.float16, element_accumulator=cutlass.float16, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add ) A = TensorDescription( element=math_inst.element_a, layout=cutlass.TensorNHWC, alignment=8) B = TensorDescription( element=math_inst.element_b, layout=cutlass.TensorNHWC, alignment=8) C = TensorDescription( element=cutlass.float16, layout=cutlass.TensorNHWC, alignment=8) tile_description = TileDescription( threadblock_shape=[128, 128, 64], stages=3, warp_count=[2, 2, 1], math_instruction=math_inst ) epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, cutlass.float16 ) operation = Conv2dOperation( conv_kind=cutlass.conv.Operator.wgrad, iterator_algorithm=cutlass.conv.IteratorAlgorithm.optimized, arch=80, tile_description=tile_description, A=A, B=B, C=C, stride_support=StrideSupport.Strided, epilogue_functor=epilogue_functor, swizzling_functor=cutlass.IdentitySwizzle1 ) self.assertTrue(test_all_conv2d(operation)) if __name__ == '__main__': pycutlass.get_memory_pool(2**26, 2**26) unittest.main()
################################################################################################# # # Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: BSD-3-Clause # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # 3. Neither the name of the copyright holder nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ################################################################################################# # test/unit/conv/device/conv2d_fprop_fixed_channels_f16nhwc_f16nhwc_f16nhwc_tensor_op_f32_sm80.cu import pycutlass from pycutlass.test import * from pycutlass.utils.device import device_cc import unittest @unittest.skipIf(device_cc() < 80, "Device compute capability is insufficient for SM80 tests.") def conv2d_fixed_channel_problemsizes(channels): problem_sizes = [ cutlass.conv.Conv2dProblemSize( cutlass.Tensor4DCoord(1, 8, 8, channels), cutlass.Tensor4DCoord(16, 3, 3, channels), cutlass.Tensor4DCoord(1, 1, 1, 1), cutlass.MatrixCoord(2, 2), cutlass.MatrixCoord(1, 1), cutlass.conv.Mode.cross_correlation, 1, 1 ), cutlass.conv.Conv2dProblemSize( cutlass.Tensor4DCoord(1, 224, 224, channels), cutlass.Tensor4DCoord(32, 7, 7, channels), cutlass.Tensor4DCoord(1, 1, 1, 1), cutlass.MatrixCoord(1, 1), cutlass.MatrixCoord(1, 1), cutlass.conv.Mode.cross_correlation, 1, 1 ), cutlass.conv.Conv2dProblemSize( cutlass.Tensor4DCoord(1, 224, 224, channels), cutlass.Tensor4DCoord(64, 7, 7, channels), cutlass.Tensor4DCoord(1, 1, 1, 1), cutlass.MatrixCoord(2, 2), cutlass.MatrixCoord(1, 1), cutlass.conv.Mode.cross_correlation, 1, 1 ), cutlass.conv.Conv2dProblemSize( cutlass.Tensor4DCoord(1, 224, 224, channels), cutlass.Tensor4DCoord(64, 5, 5, channels), cutlass.Tensor4DCoord(1, 1, 1, 1), cutlass.MatrixCoord(1, 1), cutlass.MatrixCoord(1, 1), cutlass.conv.Mode.cross_correlation, 1, 1 ), cutlass.conv.Conv2dProblemSize( cutlass.Tensor4DCoord(1, 224, 224, channels), cutlass.Tensor4DCoord(64, 5, 5, channels), cutlass.Tensor4DCoord(1, 1, 1, 1), cutlass.MatrixCoord(2, 2), cutlass.MatrixCoord(1, 1), cutlass.conv.Mode.cross_correlation, 1, 1 ), ] return problem_sizes class Conv2dFpropFixedChannelsF16NHWCF16NHWCF16HNWCTensorOpF32SM80(unittest.TestCase): def test_SM80_Device_Conv2d_Fprop_Fixed_Channels_ImplicitGemm_f16nhwc_f16nhwc_f16nhwc_tensor_op_f32_channels_8(self): math_inst = MathInstruction( instruction_shape=[16, 8, 16], element_a=cutlass.float16, element_b=cutlass.float16, element_accumulator=cutlass.float32, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add ) A = TensorDescription( element=math_inst.element_a, layout=cutlass.TensorNHWC, alignment=8) B = TensorDescription( element=math_inst.element_b, layout=cutlass.TensorNHWC, alignment=8) C = TensorDescription( element=cutlass.float16, layout=cutlass.TensorNHWC, alignment=8) tile_description = TileDescription( threadblock_shape=[128, 128, 64], stages=3, warp_count=[2, 2, 1], math_instruction=math_inst ) epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, cutlass.float32) operation = Conv2dOperation( conv_kind=cutlass.conv.Operator.fprop, iterator_algorithm=cutlass.conv.IteratorAlgorithm.fixed_channels, arch=80, tile_description=tile_description, A=A, B=B, C=C, stride_support=StrideSupport.Strided, epilogue_functor=epilogue_functor, swizzling_functor=cutlass.IdentitySwizzle1 ) self.assertTrue(test_all_conv2d(operation, conv2d_fixed_channel_problemsizes(8))) def test_SM80_Device_Conv2d_Fprop_Fixed_Channels_ImplicitGemm_f16nhwc_f16nhwc_f16nhwc_tensor_op_f32_channels_4(self): math_inst = MathInstruction( instruction_shape=[16, 8, 16], element_a=cutlass.float16, element_b=cutlass.float16, element_accumulator=cutlass.float32, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add ) A = TensorDescription( element=math_inst.element_a, layout=cutlass.TensorNHWC, alignment=4) B = TensorDescription( element=math_inst.element_b, layout=cutlass.TensorNHWC, alignment=4) C = TensorDescription( element=cutlass.float16, layout=cutlass.TensorNHWC, alignment=8) tile_description = TileDescription( threadblock_shape=[128, 128, 64], stages=3, warp_count=[2, 2, 1], math_instruction=math_inst ) epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, cutlass.float32) operation = Conv2dOperation( conv_kind=cutlass.conv.Operator.fprop, iterator_algorithm=cutlass.conv.IteratorAlgorithm.fixed_channels, arch=80, tile_description=tile_description, A=A, B=B, C=C, stride_support=StrideSupport.Strided, epilogue_functor=epilogue_functor, swizzling_functor=cutlass.IdentitySwizzle1 ) self.assertTrue(test_all_conv2d(operation, conv2d_fixed_channel_problemsizes(4))) def test_SM80_Device_Conv2d_Fprop_Fixed_Channels_ImplicitGemm_f16nhwc_f16nhwc_f16nhwc_tensor_op_f32_channels_2(self): math_inst = MathInstruction( instruction_shape=[16, 8, 16], element_a=cutlass.float16, element_b=cutlass.float16, element_accumulator=cutlass.float32, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add ) A = TensorDescription( element=math_inst.element_a, layout=cutlass.TensorNHWC, alignment=2) B = TensorDescription( element=math_inst.element_b, layout=cutlass.TensorNHWC, alignment=2) C = TensorDescription( element=cutlass.float16, layout=cutlass.TensorNHWC, alignment=8) tile_description = TileDescription( threadblock_shape=[128, 128, 64], stages=3, warp_count=[2, 2, 1], math_instruction=math_inst ) epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, cutlass.float32) operation = Conv2dOperation( conv_kind=cutlass.conv.Operator.fprop, iterator_algorithm=cutlass.conv.IteratorAlgorithm.fixed_channels, arch=80, tile_description=tile_description, A=A, B=B, C=C, stride_support=StrideSupport.Strided, epilogue_functor=epilogue_functor, swizzling_functor=cutlass.IdentitySwizzle1 ) self.assertTrue(test_all_conv2d(operation, conv2d_fixed_channel_problemsizes(2))) if __name__ == '__main__': pycutlass.get_memory_pool(2**26, 2**26) unittest.main()
################################################################################################# # # Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: BSD-3-Clause # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # 3. Neither the name of the copyright holder nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ################################################################################################# # test/unit/conv/device/conv2d_fprop_implicit_gemm_tf32nhwc_tf32nhwc_f32nhwc_tensor_op_f32_sm80.cu import pycutlass from pycutlass import * from pycutlass.test import * from pycutlass.utils.device import device_cc import unittest @unittest.skipIf(device_cc() < 80, "Device compute capability is insufficient for SM80 tests.") class Conv2dFpropImplicitGemmTF32nhwcTF32nhwcTF32nhwcTensorOpF32SM80(unittest.TestCase): def test_SM80_Device_Conv2d_Fprop_Analytic_ImplicitGemm_tf32nhwc_tf32nhwc_f32nhwc_tensor_op_f32(self): math_inst = MathInstruction( instruction_shape=[16, 8, 8], element_a=cutlass.float32, element_b=cutlass.float32, element_accumulator=cutlass.float32, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add ) A = TensorDescription( element=math_inst.element_a, layout=cutlass.TensorNHWC, alignment=4) B = TensorDescription( element=math_inst.element_b, layout=cutlass.TensorNHWC, alignment=4) C = TensorDescription( element=cutlass.float32, layout=cutlass.TensorNHWC, alignment=8) tile_description = TileDescription( threadblock_shape=[128, 128, 16], stages=3, warp_count=[2, 2, 1], math_instruction=math_inst ) epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, cutlass.float32) operation = Conv2dOperation( conv_kind=cutlass.conv.Operator.fprop, iterator_algorithm=cutlass.conv.IteratorAlgorithm.analytic, arch=80, tile_description=tile_description, A=A, B=B, C=C, stride_support=StrideSupport.Strided, epilogue_functor=epilogue_functor, swizzling_functor=cutlass.IdentitySwizzle1 ) self.assertTrue(test_all_conv2d(operation)) def test_SM80_Device_Conv2d_Fprop_Optimized_ImplicitGemm_tf32nhwc_tf32nhwc_f32nhwc_tensor_op_f32_align2(self): math_inst = MathInstruction( instruction_shape=[16, 8, 8], element_a=cutlass.float32, element_b=cutlass.float32, element_accumulator=cutlass.float32, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add ) A = TensorDescription( element=math_inst.element_a, layout=cutlass.TensorNHWC, alignment=2) B = TensorDescription( element=math_inst.element_b, layout=cutlass.TensorNHWC, alignment=2) C = TensorDescription( element=cutlass.float32, layout=cutlass.TensorNHWC, alignment=8) tile_description = TileDescription( threadblock_shape=[128, 128, 16], stages=3, warp_count=[2, 2, 1], math_instruction=math_inst ) epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, cutlass.float32) operation = Conv2dOperation( conv_kind=cutlass.conv.Operator.fprop, iterator_algorithm=cutlass.conv.IteratorAlgorithm.optimized, arch=80, tile_description=tile_description, A=A, B=B, C=C, stride_support=StrideSupport.Strided, epilogue_functor=epilogue_functor, swizzling_functor=cutlass.IdentitySwizzle1 ) problem_sizes = [ cutlass.conv.Conv2dProblemSize( cutlass.Tensor4DCoord(1, 4, 4, 12), cutlass.Tensor4DCoord(8, 3, 3, 12), cutlass.Tensor4DCoord(0, 0, 0, 0), cutlass.MatrixCoord(3, 3), cutlass.MatrixCoord(1, 1), cutlass.conv.Mode.cross_correlation, 1, 1 ) ] self.assertTrue(test_all_conv2d(operation, problem_sizes)) if __name__ == '__main__': pycutlass.get_memory_pool(2**26, 2**26) unittest.main()
################################################################################################# # # Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: BSD-3-Clause # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # 3. Neither the name of the copyright holder nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ################################################################################################# # test/unit/conv/device/conv2d_fprop_implicit_gemm_f16nhwc_f16nhwc_f32nhwc_tensor_op_f32_sm80.cu import pycutlass from pycutlass import * from pycutlass.test import * from pycutlass.utils.device import device_cc import unittest @unittest.skipIf(device_cc() < 80, "Device compute capability is insufficient for SM80 tests.") class Conv2dFpropImplicitGemmF16nhwcF16nhwcF32nhwcTensorOpF32SM80(unittest.TestCase): def test_SM80_Device_Conv2d_Fprop_Analytic_ImplicitGemm_f16nhwc_f16nhwc_f32nhwc_tensor_op_f32(self): math_inst = MathInstruction( instruction_shape=[16, 8, 16], element_a=cutlass.float16, element_b=cutlass.float16, element_accumulator=cutlass.float32, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add ) A = TensorDescription( element=math_inst.element_a, layout=cutlass.TensorNHWC, alignment=8) B = TensorDescription( element=math_inst.element_b, layout=cutlass.TensorNHWC, alignment=8) C = TensorDescription( element=cutlass.float32, layout=cutlass.TensorNHWC, alignment=4) tile_description = TileDescription( threadblock_shape=[128, 128, 64], stages=3, warp_count=[2, 2, 1], math_instruction=math_inst ) epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, cutlass.float32) operation = Conv2dOperation( conv_kind=cutlass.conv.Operator.fprop, iterator_algorithm=cutlass.conv.IteratorAlgorithm.analytic, arch=80, tile_description=tile_description, A=A, B=B, C=C, stride_support=StrideSupport.Strided, epilogue_functor=epilogue_functor, swizzling_functor=cutlass.IdentitySwizzle1 ) self.assertTrue(test_all_conv2d(operation)) if __name__ == '__main__': pycutlass.get_memory_pool(2**26, 2**26) unittest.main()
################################################################################################# # # Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: BSD-3-Clause # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # 3. Neither the name of the copyright holder nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ################################################################################################# # test/unit/conv/device/conv2d_wgrad_implicit_gemm_f32nhwc_f32nhwc_f32nhwc_simt_f32_sm80.cu import pycutlass from pycutlass.conv2d_operation import * from pycutlass import * from pycutlass.test import * from pycutlass.utils.device import device_cc import unittest @unittest.skipIf(device_cc() < 80, "Device compute capability is insufficient for SM80 tests.") class Conv2dWgradImplicitGemmF32nhwcF32nhwcF32nhwcSimtF32SM80(unittest.TestCase): def test_SM80_Device_Conv2d_Wgrad_Analytic_ImplicitGemm_f32nhwc_f32nhwc_f32nhwc_simt_f32(self): math_inst = MathInstruction( instruction_shape=[1, 1, 1], element_a=cutlass.float32, element_b=cutlass.float32, element_accumulator=cutlass.float32, opcode_class=cutlass.OpClass.Simt, math_operation=MathOperation.multiply_add ) A = TensorDescription( element=math_inst.element_a, layout=cutlass.TensorNHWC, alignment=4) B = TensorDescription( element=math_inst.element_b, layout=cutlass.TensorNHWC, alignment=4) C = TensorDescription( element=cutlass.float32, layout=cutlass.TensorNHWC, alignment=1) tile_description = TileDescription( threadblock_shape=[128, 128, 8], stages=4, warp_count=[2, 4, 1], math_instruction=math_inst ) epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, cutlass.float32) operation = Conv2dOperation( conv_kind=cutlass.conv.Operator.wgrad, iterator_algorithm=cutlass.conv.IteratorAlgorithm.analytic, arch=80, tile_description=tile_description, A=A, B=B, C=C, stride_support=StrideSupport.Strided, epilogue_functor=epilogue_functor, swizzling_functor=cutlass.IdentitySwizzle1 ) self.assertTrue(test_all_conv2d(operation)) def test_SM80_Device_Conv2d_Wgrad_Optimized_ImplicitGemm_f32nhwc_f32nhwc_f32nhwc_simt_f32(self): math_inst = MathInstruction( instruction_shape=[1, 1, 1], element_a=cutlass.float32, element_b=cutlass.float32, element_accumulator=cutlass.float32, opcode_class=cutlass.OpClass.Simt, math_operation=MathOperation.multiply_add ) A = TensorDescription( element=math_inst.element_a, layout=cutlass.TensorNHWC, alignment=4) B = TensorDescription( element=math_inst.element_b, layout=cutlass.TensorNHWC, alignment=4) C = TensorDescription( element=cutlass.float32, layout=cutlass.TensorNHWC, alignment=1) tile_description = TileDescription( threadblock_shape=[128, 128, 8], stages=4, warp_count=[2, 4, 1], math_instruction=math_inst ) epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, cutlass.float32) operation = Conv2dOperation( conv_kind=cutlass.conv.Operator.wgrad, iterator_algorithm=cutlass.conv.IteratorAlgorithm.optimized, arch=80, tile_description=tile_description, A=A, B=B, C=C, stride_support=StrideSupport.Strided, epilogue_functor=epilogue_functor, swizzling_functor=cutlass.IdentitySwizzle1 ) self.assertTrue(test_all_conv2d(operation)) if __name__ == '__main__': pycutlass.get_memory_pool(2**26, 2**26) unittest.main()
################################################################################################# # # Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: BSD-3-Clause # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # 3. Neither the name of the copyright holder nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ################################################################################################# import pycutlass import unittest from pycutlass.memory_manager import * if __name__ == '__main__': pycutlass.get_memory_pool(2**32, 2**32) loader = unittest.TestLoader() tests = loader.discover('./', 'conv2d_*.py') testRunner = unittest.runner.TextTestRunner() testRunner.run(tests)
################################################################################################# # # Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: BSD-3-Clause # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # 3. Neither the name of the copyright holder nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ################################################################################################# # test/unit/conv/device/conv2d_wgrad_implicit_gemm_tf32nhwc_tf32nhwc_f32nhwc_tensor_op_f32_sm80.cu import pycutlass from pycutlass import * from pycutlass.test import * from pycutlass.utils.device import device_cc import unittest @unittest.skipIf(device_cc() < 80, "Device compute capability is insufficient for SM80 tests.") class Conv2dWgradImplicitGemmTF32nhwcTF32nhwcTF32nhwcTensorOpF32SM80(unittest.TestCase): def test_SM80_Device_Conv2d_Wgrad_Optimized_ImplicitGemm_tf32nhwc_tf32nhwc_f32nhwc_tensor_op_f32(self): math_inst = MathInstruction( instruction_shape=[16, 8, 8], element_a=cutlass.float32, element_b=cutlass.float32, element_accumulator=cutlass.float32, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add ) A = TensorDescription( element=math_inst.element_a, layout=cutlass.TensorNHWC, alignment=4) B = TensorDescription( element=math_inst.element_b, layout=cutlass.TensorNHWC, alignment=4) C = TensorDescription( element=cutlass.float32, layout=cutlass.TensorNHWC, alignment=8) tile_description = TileDescription( threadblock_shape=[128, 128, 16], stages=3, warp_count=[2, 2, 1], math_instruction=math_inst ) epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, cutlass.float32) operation = Conv2dOperation( conv_kind=cutlass.conv.Operator.wgrad, iterator_algorithm=cutlass.conv.IteratorAlgorithm.optimized, arch=80, tile_description=tile_description, A=A, B=B, C=C, stride_support=StrideSupport.Strided, epilogue_functor=epilogue_functor, swizzling_functor=cutlass.IdentitySwizzle1 ) self.assertTrue(test_all_conv2d(operation)) def test_SM80_Device_Conv2d_Wgrad_Optimized_ImplicitGemm_tf32nhwc_tf32nhwc_f32nhwc_tensor_op_f32_align1(self): math_inst = MathInstruction( instruction_shape=[16, 8, 8], element_a=cutlass.float32, element_b=cutlass.float32, element_accumulator=cutlass.float32, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add ) A = TensorDescription( element=math_inst.element_a, layout=cutlass.TensorNHWC, alignment=1) B = TensorDescription( element=math_inst.element_b, layout=cutlass.TensorNHWC, alignment=1) C = TensorDescription( element=cutlass.float32, layout=cutlass.TensorNHWC, alignment=4) tile_description = TileDescription( threadblock_shape=[128, 128, 32], stages=3, warp_count=[2, 2, 1], math_instruction=math_inst ) epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, cutlass.float32) operation = Conv2dOperation( conv_kind=cutlass.conv.Operator.wgrad, iterator_algorithm=cutlass.conv.IteratorAlgorithm.optimized, arch=80, tile_description=tile_description, A=A, B=B, C=C, stride_support=StrideSupport.Strided, epilogue_functor=epilogue_functor, swizzling_functor=cutlass.IdentitySwizzle1 ) problem_sizes = [ cutlass.conv.Conv2dProblemSize( cutlass.Tensor4DCoord(1, 8, 8, 1), cutlass.Tensor4DCoord(1, 3, 3, 1), cutlass.Tensor4DCoord(1, 1, 1, 1), cutlass.MatrixCoord(1, 1), cutlass.MatrixCoord(1, 1), cutlass.conv.Mode.cross_correlation, 1, 1 ), ] self.assertTrue(test_all_conv2d(operation, problem_sizes)) if __name__ == '__main__': pycutlass.get_memory_pool(2**26, 2**26) unittest.main()
################################################################################################# # # Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: BSD-3-Clause # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # 3. Neither the name of the copyright holder nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ################################################################################################# # test/unit/conv/device/conv2d_fprop_implicit_gemm_f16nhwc_f16nhwc_f16nhwc_tensor_op_f16_sm80.cu import pycutlass from pycutlass import * from pycutlass.test import * from pycutlass.utils.device import device_cc import unittest @unittest.skipIf(device_cc() < 80, "Device compute capability is insufficient for SM80 tests.") class Conv2dFpropImplicitGemmF16nhwcF16nhwcF16nhwcTensorOpF16SM80(unittest.TestCase): def test_SM80_Device_Conv2d_Fprop_Analytic_ImplicitGemm_f16nhwc_f16nhwc_f16nhwc_tensor_op_f16(self): math_inst = MathInstruction( instruction_shape=[16, 8, 16], element_a=cutlass.float16, element_b=cutlass.float16, element_accumulator=cutlass.float16, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add ) A = TensorDescription( element=math_inst.element_a, layout=cutlass.TensorNHWC, alignment=8) B = TensorDescription( element=math_inst.element_b, layout=cutlass.TensorNHWC, alignment=8) C = TensorDescription( element=cutlass.float16, layout=cutlass.TensorNHWC, alignment=8) tile_description = TileDescription( threadblock_shape=[128, 128, 64], stages=3, warp_count=[2, 2, 1], math_instruction=math_inst ) epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, cutlass.float16) operation = Conv2dOperation( conv_kind=cutlass.conv.Operator.fprop, iterator_algorithm=cutlass.conv.IteratorAlgorithm.analytic, arch=80, tile_description=tile_description, A=A, B=B, C=C, stride_support=StrideSupport.Strided, epilogue_functor=epilogue_functor, swizzling_functor=cutlass.IdentitySwizzle1 ) self.assertTrue(test_all_conv2d(operation)) def test_SM80_Device_Conv2d_Fprop_Optimized_ImplicitGemm_f16nhwc_f16nhwc_f16nhwc_tensor_op_f16(self): math_inst = MathInstruction( instruction_shape=[16, 8, 16], element_a=cutlass.float16, element_b=cutlass.float16, element_accumulator=cutlass.float16, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add ) A = TensorDescription( element=math_inst.element_a, layout=cutlass.TensorNHWC, alignment=8) B = TensorDescription( element=math_inst.element_b, layout=cutlass.TensorNHWC, alignment=8) C = TensorDescription( element=cutlass.float16, layout=cutlass.TensorNHWC, alignment=8) tile_description = TileDescription( threadblock_shape=[128, 128, 64], stages=3, warp_count=[2, 2, 1], math_instruction=math_inst ) epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, cutlass.float16) operation = Conv2dOperation( conv_kind=cutlass.conv.Operator.fprop, iterator_algorithm=cutlass.conv.IteratorAlgorithm.optimized, arch=80, tile_description=tile_description, A=A, B=B, C=C, stride_support=StrideSupport.Strided, epilogue_functor=epilogue_functor, swizzling_functor=cutlass.IdentitySwizzle1 ) self.assertTrue(test_all_conv2d(operation)) def test_SM80_Device_Conv2d_Fprop_Analytic_ImplicitGemm_f16nhwc_f16nhwc_f16nhwc_tensor_op_f16_align2(self): math_inst = MathInstruction( instruction_shape=[16, 8, 16], element_a=cutlass.float16, element_b=cutlass.float16, element_accumulator=cutlass.float16, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add ) A = TensorDescription( element=math_inst.element_a, layout=cutlass.TensorNHWC, alignment=2) B = TensorDescription( element=math_inst.element_b, layout=cutlass.TensorNHWC, alignment=2) C = TensorDescription( element=cutlass.float16, layout=cutlass.TensorNHWC, alignment=8) tile_description = TileDescription( threadblock_shape=[128, 128, 64], stages=3, warp_count=[2, 2, 1], math_instruction=math_inst ) epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, cutlass.float16) operation = Conv2dOperation( conv_kind=cutlass.conv.Operator.fprop, iterator_algorithm=cutlass.conv.IteratorAlgorithm.analytic, arch=80, tile_description=tile_description, A=A, B=B, C=C, stride_support=StrideSupport.Strided, epilogue_functor=epilogue_functor, swizzling_functor=cutlass.IdentitySwizzle1 ) problem_sizes = [ cutlass.conv.Conv2dProblemSize( cutlass.Tensor4DCoord(1, 4, 4, 12), cutlass.Tensor4DCoord(8, 3, 3, 12), cutlass.Tensor4DCoord(0, 0, 0, 0), cutlass.MatrixCoord(3, 3), cutlass.MatrixCoord(1, 1), cutlass.conv.Mode.cross_correlation, 1, 1 ), cutlass.conv.Conv2dProblemSize( cutlass.Tensor4DCoord(1, 4, 4, 14), cutlass.Tensor4DCoord(8, 3, 3, 14), cutlass.Tensor4DCoord(0, 0, 0, 0), cutlass.MatrixCoord(3, 3), cutlass.MatrixCoord(1, 1), cutlass.conv.Mode.cross_correlation, 1, 1 ), cutlass.conv.Conv2dProblemSize( cutlass.Tensor4DCoord(1, 23, 56, 98), cutlass.Tensor4DCoord(128, 3, 3, 98), cutlass.Tensor4DCoord(4, 0, 5, 0), cutlass.MatrixCoord(3, 3), cutlass.MatrixCoord(1, 1), cutlass.conv.Mode.cross_correlation, 1, 1 ), ] self.assertTrue(test_all_conv2d(operation, problem_sizes)) def test_SM80_Device_Conv2d_Fprop_Optimized_ImplicitGemm_f16nhwc_f16nhwc_f16nhwc_tensor_op_f16_align2(self): math_inst = MathInstruction( instruction_shape=[16, 8, 16], element_a=cutlass.float16, element_b=cutlass.float16, element_accumulator=cutlass.float16, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add ) A = TensorDescription( element=math_inst.element_a, layout=cutlass.TensorNHWC, alignment=2) B = TensorDescription( element=math_inst.element_b, layout=cutlass.TensorNHWC, alignment=2) C = TensorDescription( element=cutlass.float16, layout=cutlass.TensorNHWC, alignment=8) tile_description = TileDescription( threadblock_shape=[128, 128, 64], stages=3, warp_count=[2, 2, 1], math_instruction=math_inst ) epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, cutlass.float16) operation = Conv2dOperation( conv_kind=cutlass.conv.Operator.fprop, iterator_algorithm=cutlass.conv.IteratorAlgorithm.optimized, arch=80, tile_description=tile_description, A=A, B=B, C=C, stride_support=StrideSupport.Strided, epilogue_functor=epilogue_functor, swizzling_functor=cutlass.IdentitySwizzle1 ) problem_sizes = [ cutlass.conv.Conv2dProblemSize( cutlass.Tensor4DCoord(1, 4, 4, 12), cutlass.Tensor4DCoord(8, 3, 3, 12), cutlass.Tensor4DCoord(0, 0, 0, 0), cutlass.MatrixCoord(3, 3), cutlass.MatrixCoord(1, 1), cutlass.conv.Mode.cross_correlation, 1, 1 ), cutlass.conv.Conv2dProblemSize( cutlass.Tensor4DCoord(1, 4, 4, 14), cutlass.Tensor4DCoord(8, 3, 3, 14), cutlass.Tensor4DCoord(0, 0, 0, 0), cutlass.MatrixCoord(3, 3), cutlass.MatrixCoord(1, 1), cutlass.conv.Mode.cross_correlation, 1, 1 ), cutlass.conv.Conv2dProblemSize( cutlass.Tensor4DCoord(1, 23, 56, 98), cutlass.Tensor4DCoord(128, 3, 3, 98), cutlass.Tensor4DCoord(4, 0, 5, 0), cutlass.MatrixCoord(3, 3), cutlass.MatrixCoord(1, 1), cutlass.conv.Mode.cross_correlation, 1, 1 ), ] self.assertTrue(test_all_conv2d(operation, problem_sizes)) def test_SM80_Device_Conv2d_Fprop_Analytic_ImplicitGemm_f16nhwc_f16nhwc_f16nhwc_tensor_op_f16_align4(self): math_inst = MathInstruction( instruction_shape=[16, 8, 16], element_a=cutlass.float16, element_b=cutlass.float16, element_accumulator=cutlass.float16, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add ) A = TensorDescription( element=math_inst.element_a, layout=cutlass.TensorNHWC, alignment=4) B = TensorDescription( element=math_inst.element_b, layout=cutlass.TensorNHWC, alignment=4) C = TensorDescription( element=cutlass.float16, layout=cutlass.TensorNHWC, alignment=8) tile_description = TileDescription( threadblock_shape=[128, 128, 64], stages=3, warp_count=[2, 2, 1], math_instruction=math_inst ) epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, cutlass.float16) operation = Conv2dOperation( conv_kind=cutlass.conv.Operator.fprop, iterator_algorithm=cutlass.conv.IteratorAlgorithm.optimized, arch=80, tile_description=tile_description, A=A, B=B, C=C, stride_support=StrideSupport.Strided, epilogue_functor=epilogue_functor, swizzling_functor=cutlass.IdentitySwizzle1 ) problem_sizes = [ cutlass.conv.Conv2dProblemSize( cutlass.Tensor4DCoord(1, 4, 4, 12), cutlass.Tensor4DCoord(8, 3, 3, 12), cutlass.Tensor4DCoord(0, 0, 0, 0), cutlass.MatrixCoord(3, 3), cutlass.MatrixCoord(1, 1), cutlass.conv.Mode.cross_correlation, 1, 1 ), cutlass.conv.Conv2dProblemSize( cutlass.Tensor4DCoord(1, 4, 4, 28), cutlass.Tensor4DCoord(8, 3, 3, 28), cutlass.Tensor4DCoord(0, 0, 0, 0), cutlass.MatrixCoord(3, 3), cutlass.MatrixCoord(1, 1), cutlass.conv.Mode.cross_correlation, 1, 1 ), cutlass.conv.Conv2dProblemSize( cutlass.Tensor4DCoord(1, 23, 56, 100), cutlass.Tensor4DCoord(128, 3, 3, 100), cutlass.Tensor4DCoord(4, 0, 5, 0), cutlass.MatrixCoord(3, 3), cutlass.MatrixCoord(1, 1), cutlass.conv.Mode.cross_correlation, 1, 1 ), ] self.assertTrue(test_all_conv2d(operation, problem_sizes)) if __name__ == '__main__': pycutlass.get_memory_pool(2**26, 2**26) unittest.main()
################################################################################################# # # Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: BSD-3-Clause # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # 3. Neither the name of the copyright holder nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ################################################################################################# from functools import partial import pycutlass from pycutlass import * from pycutlass import library from pycutlass.test import * import unittest from pycutlass.test.utils import LayoutCombination, get_name from pycutlass.test.gemm_testbed import test_all_gemm from pycutlass.utils.device import device_cc # Partial specialization for naming tests name_fn = partial(get_name, element_a=cutlass.float16, element_b=cutlass.float16, arch=90) def add_test(cls, layouts, alignments, element_output, element_accumulator, element_epilogue, cluster_shape, threadblock_shape, stages, opclass, persistent=False): """ Create a test-running function with the given specification and set it as a method of `cls`. :param cls: class to which the generated method will be added :type cls: type :param layouts: indexable container of layouts of A, B, and C operands :param alignments: indexable container of alignments of A, B, and C operands :param element_output: data type of the output element :param element_accumulator: data type used in accumulation :param element_epilogue: data type used in computing the epilogue :param cluster_shape: indexable container of dimensions of threadblock cluster to be launched :param threadblock_shape: indexable container of dimensions of threadblock tiles :param stages: number of pipeline stages to use in the kernel :type stages: int :param opclass: class of operation being performed (e.g., SIMT, Tensor Core) :type opclass: cutlass.OpClass :param persistent: whether this is a persistent warp-specialized kernel :type persistent: bool """ def run(self): """ Dynamically-generated function that constructs a GEMM operation and verifies it against multiple test cases. """ element_A = cutlass.float16 element_B = cutlass.float16 inst_shape = [1, 1, 1] if opclass == cutlass.OpClass.Simt else None warp_count = [2, 2, 1] if opclass == cutlass.OpClass.Simt else None math_inst = MathInstruction( instruction_shape=inst_shape, element_a=element_A, element_b=element_B, element_accumulator=element_accumulator, opcode_class=opclass, math_operation=MathOperation.multiply_add ) tile_description = TileDescription( threadblock_shape=threadblock_shape, cluster_shape=cluster_shape, stages=stages, warp_count=warp_count, math_instruction=math_inst, persistent=persistent ) A = TensorDescription(element=element_A, layout=layouts[0], alignment=alignments[0]) B = TensorDescription(element=element_B, layout=layouts[1], alignment=alignments[1]) C = TensorDescription(element=element_output, layout=layouts[2], alignment=alignments[2]) epilogue_functor = LinearCombination(C.element, C.alignment, math_inst.element_accumulator, element_epilogue) swizzling_functor = cutlass.IdentitySwizzle1 operation = GemmOperationUniversal( arch=90, tile_description=tile_description, A=A, B=B, C=C, epilogue_functor=epilogue_functor, swizzling_functor=swizzling_functor) self.assertTrue(test_all_gemm(operation, "universal")) if persistent: suffix = "_persistent" else: suffix = "" name = name_fn(layouts, alignments, element_output, element_accumulator, element_epilogue, cluster_shape, threadblock_shape, stages, opclass=opclass, suffix=suffix) setattr(cls, name, run) return run @unittest.skipIf(device_cc() < 90, "Device compute capability is insufficient for SM90 tests.") class GemmF16Sm90(unittest.TestCase): """ Wrapper class to which tests will be added dynamically in __main__ """ pass add_test_tensorop = partial(add_test, opclass=cutlass.OpClass.TensorOp) add_test_simt = partial(add_test, opclass=cutlass.OpClass.Simt) # Tests with 1x1x1 clusters add_test_tensorop(GemmF16Sm90, LayoutCombination.NNN, [8, 8, 8], cutlass.float16, cutlass.float32, cutlass.float32, [1, 1, 1], [128, 128, 32], 3) add_test_tensorop(GemmF16Sm90, LayoutCombination.NNT, [8, 8, 8], cutlass.float16, cutlass.float32, cutlass.float32, [1, 1, 1], [128, 128, 32], None) add_test_tensorop(GemmF16Sm90, LayoutCombination.NTN, [8, 8, 8], cutlass.float16, cutlass.float32, cutlass.float32, [1, 1, 1], [128, 128, 32], None) add_test_tensorop(GemmF16Sm90, LayoutCombination.NTT, [8, 8, 8], cutlass.float16, cutlass.float32, cutlass.float32, [1, 1, 1], [128, 128, 32], None) add_test_tensorop(GemmF16Sm90, LayoutCombination.TNN, [8, 8, 8], cutlass.float16, cutlass.float32, cutlass.float32, [1, 1, 1], [128, 128, 32], None) add_test_tensorop(GemmF16Sm90, LayoutCombination.TNT, [8, 8, 8], cutlass.float16, cutlass.float32, cutlass.float32, [1, 1, 1], [128, 128, 32], None) add_test_tensorop(GemmF16Sm90, LayoutCombination.TTN, [8, 8, 8], cutlass.float16, cutlass.float32, cutlass.float32, [1, 1, 1], [128, 128, 32], None) add_test_tensorop(GemmF16Sm90, LayoutCombination.TTT, [8, 8, 8], cutlass.float16, cutlass.float32, cutlass.float32, [1, 1, 1], [128, 128, 32], None) add_test_tensorop(GemmF16Sm90, LayoutCombination.TNT, [8, 8, 8], cutlass.float16, cutlass.float32, cutlass.float32, [1, 1, 1], [64, 128, 32], None) add_test_tensorop(GemmF16Sm90, LayoutCombination.TNT, [8, 8, 8], cutlass.float16, cutlass.float32, cutlass.float32, [1, 1, 1], [128, 64, 32], None) add_test_tensorop(GemmF16Sm90, LayoutCombination.TNT, [8, 8, 8], cutlass.float16, cutlass.float32, cutlass.float32, [1, 1, 1], [64, 64, 64], None) add_test_tensorop(GemmF16Sm90, LayoutCombination.TNT, [4, 4, 8], cutlass.float16, cutlass.float32, cutlass.float32, [1, 1, 1], [128, 128, 32], None) add_test_tensorop(GemmF16Sm90, LayoutCombination.TNT, [4, 4, 8], cutlass.float16, cutlass.float16, cutlass.float16, [1, 1, 1], [128, 128, 32], None) add_test_tensorop(GemmF16Sm90, LayoutCombination.TNT, [8, 8, 8], cutlass.float16, cutlass.float16, cutlass.float16, [1, 1, 1], [128, 128, 32], None) add_test_tensorop(GemmF16Sm90, LayoutCombination.TNT, [8, 8, 8], cutlass.float16, cutlass.float32, cutlass.float32, [1, 1, 1], [64, 64, 64], 5) add_test_tensorop(GemmF16Sm90, LayoutCombination.TNT, [2, 2, 2], cutlass.float16, cutlass.float16, cutlass.float16, [1, 1, 1], [128, 128, 32], None) # Tests with different cluster shapes add_test_tensorop(GemmF16Sm90, LayoutCombination.TTN, [8, 8, 8], cutlass.float32, cutlass.float32, cutlass.float32, [2, 2, 1], [64, 128, 64], None) add_test_tensorop(GemmF16Sm90, LayoutCombination.TNN, [8, 8, 8], cutlass.float32, cutlass.float32, cutlass.float32, [2, 2, 1], [64, 128, 64], None) add_test_tensorop(GemmF16Sm90, LayoutCombination.NTN, [8, 8, 8], cutlass.float32, cutlass.float32, cutlass.float32, [2, 2, 1], [64, 128, 64], None) add_test_tensorop(GemmF16Sm90, LayoutCombination.NNN, [8, 8, 8], cutlass.float32, cutlass.float32, cutlass.float32, [2, 2, 1], [64, 128, 64], None) add_test_tensorop(GemmF16Sm90, LayoutCombination.TTN, [8, 8, 8], cutlass.float32, cutlass.float32, cutlass.float32, [1, 4, 1], [64, 128, 64], None) add_test_tensorop(GemmF16Sm90, LayoutCombination.TTN, [8, 8, 8], cutlass.float32, cutlass.float32, cutlass.float32, [2, 4, 1], [64, 128, 64], None) add_test_tensorop(GemmF16Sm90, LayoutCombination.TTN, [8, 8, 8], cutlass.float32, cutlass.float32, cutlass.float32, [4, 1, 1], [64, 128, 64], None) add_test_tensorop(GemmF16Sm90, LayoutCombination.TTN, [8, 8, 8], cutlass.float32, cutlass.float32, cutlass.float32, [4, 2, 1], [64, 128, 64], None) # Tests for persistent warp-specialized threadblocks add_test_tensorop(GemmF16Sm90, LayoutCombination.TTN, [8, 8, 8], cutlass.float32, cutlass.float32, cutlass.float32, [1, 1, 1], [64, 128, 64], None, persistent=True) add_test_tensorop(GemmF16Sm90, LayoutCombination.TTN, [8, 8, 8], cutlass.float32, cutlass.float32, cutlass.float32, [2, 1, 1], [64, 128, 64], None, persistent=True) add_test_tensorop(GemmF16Sm90, LayoutCombination.TTN, [8, 8, 8], cutlass.float32, cutlass.float32, cutlass.float32, [1, 1, 1], [128, 128, 64], None, persistent=True) add_test_tensorop(GemmF16Sm90, LayoutCombination.TTN, [8, 8, 8], cutlass.float32, cutlass.float32, cutlass.float32, [2, 1, 1], [128, 128, 64], None, persistent=True) add_test_tensorop(GemmF16Sm90, LayoutCombination.TTN, [8, 8, 8], cutlass.float32, cutlass.float32, cutlass.float32, [1, 2, 1], [64, 128, 64], None, persistent=True) add_test_tensorop(GemmF16Sm90, LayoutCombination.TTN, [8, 8, 8], cutlass.float32, cutlass.float32, cutlass.float32, [2, 2, 1], [64, 128, 64], None, persistent=True) add_test_tensorop(GemmF16Sm90, LayoutCombination.TTN, [8, 8, 8], cutlass.float32, cutlass.float32, cutlass.float32, [1, 4, 1], [64, 128, 64], None, persistent=True) add_test_tensorop(GemmF16Sm90, LayoutCombination.TTN, [8, 8, 8], cutlass.float32, cutlass.float32, cutlass.float32, [2, 4, 1], [64, 128, 64], None, persistent=True) add_test_tensorop(GemmF16Sm90, LayoutCombination.TTN, [8, 8, 8], cutlass.float32, cutlass.float32, cutlass.float32, [4, 1, 1], [64, 128, 64], None, persistent=True) add_test_tensorop(GemmF16Sm90, LayoutCombination.TTN, [8, 8, 8], cutlass.float32, cutlass.float32, cutlass.float32, [4, 4, 1], [64, 128, 64], None, persistent=True) # Tests using SIMT add_test_simt(GemmF16Sm90, LayoutCombination.NNN, [1, 1, 1], cutlass.float16, cutlass.float32, cutlass.float32, [1, 1, 1], [128, 128, 8], 2) add_test_simt(GemmF16Sm90, LayoutCombination.TNN, [1, 1, 1], cutlass.float16, cutlass.float32, cutlass.float32, [1, 1, 1], [64, 128, 8], 2) add_test_simt(GemmF16Sm90, LayoutCombination.NTN, [1, 1, 1], cutlass.float16, cutlass.float32, cutlass.float32, [1, 1, 1], [128, 64, 8], 2) add_test_simt(GemmF16Sm90, LayoutCombination.TTN, [1, 1, 1], cutlass.float16, cutlass.float32, cutlass.float32, [1, 1, 1], [64, 64, 8], 2) add_test_simt(GemmF16Sm90, LayoutCombination.NNT, [1, 1, 1], cutlass.float16, cutlass.float16, cutlass.float16, [1, 1, 1], [128, 128, 8], 2) if __name__ == '__main__': pycutlass.get_memory_pool(2**30, 2**30) unittest.main()
################################################################################################# # # Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: BSD-3-Clause # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # 3. Neither the name of the copyright holder nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ################################################################################################# from functools import partial import pycutlass from pycutlass import * from pycutlass import library from pycutlass.test import * import unittest from pycutlass.test.utils import LayoutCombination, get_name from pycutlass.test.gemm_testbed import test_all_gemm from pycutlass.utils.device import device_cc name_fn = partial(get_name, element_a=cutlass.bfloat16, element_b=cutlass.bfloat16, arch=90) def add_test(cls, layouts, alignments, element_output, element_accumulator, element_epilogue, cluster_shape, threadblock_shape, stages, opclass, persistent=False): """ Create a test-running function with the given specification and set it as a method of `cls`. :param cls: class to which the generated method will be added :type cls: type :param layouts: indexable container of layouts of A, B, and C operands :param alignments: indexable container of alignments of A, B, and C operands :param element_output: data type of the output element :param element_accumulator: data type used in accumulation :param element_epilogue: data type used in computing the epilogue :param cluster_shape: indexable container of dimensions of threadblock cluster to be launched :param threadblock_shape: indexable container of dimensions of threadblock tiles :param stages: number of pipeline stages to use in the kernel :type stages: int :param opclass: class of operation being performed (e.g., SIMT, Tensor Core) :type opclass: cutlass.OpClass :param persistent: whether this is a persistent warp-specialized kernel :type persistent: bool """ def run(self): """ Dynamically-generated function that constructs a GEMM operation and verifies it against multiple test cases. """ element_A = cutlass.bfloat16 element_B = cutlass.bfloat16 inst_shape = [1, 1, 1] if opclass == cutlass.OpClass.Simt else None warp_count = [2, 2, 1] if opclass == cutlass.OpClass.Simt else None math_inst = MathInstruction( instruction_shape=inst_shape, element_a=element_A, element_b=element_B, element_accumulator=element_accumulator, opcode_class=opclass, math_operation=MathOperation.multiply_add ) tile_description = TileDescription( threadblock_shape=threadblock_shape, cluster_shape=cluster_shape, stages=stages, warp_count=warp_count, math_instruction=math_inst, persistent=persistent ) A = TensorDescription(element=element_A, layout=layouts[0], alignment=alignments[0]) B = TensorDescription(element=element_B, layout=layouts[1], alignment=alignments[1]) C = TensorDescription(element=element_output, layout=layouts[2], alignment=alignments[2]) epilogue_functor = LinearCombination(C.element, C.alignment, math_inst.element_accumulator, element_epilogue) swizzling_functor = cutlass.IdentitySwizzle1 operation = GemmOperationUniversal( arch=90, tile_description=tile_description, A=A, B=B, C=C, epilogue_functor=epilogue_functor, swizzling_functor=swizzling_functor) self.assertTrue(test_all_gemm(operation, "universal")) if persistent: suffix = "_persistent" else: suffix = "" name = name_fn(layouts, alignments, element_output, element_accumulator, element_epilogue, cluster_shape, threadblock_shape, stages, opclass=opclass, suffix=suffix) setattr(cls, name, run) return run @unittest.skipIf(device_cc() < 90, "Device compute capability is insufficient for SM90 tests.") class GemmBF16Sm90(unittest.TestCase): """ Wrapper class to which tests will be added dynamically in __main__ """ pass add_test_tensorop = partial(add_test, opclass=cutlass.OpClass.TensorOp) add_test_simt = partial(add_test, opclass=cutlass.OpClass.Simt) add_test_tensorop(GemmBF16Sm90, LayoutCombination.NNN, [8, 8, 8], cutlass.bfloat16, cutlass.float32, cutlass.float32, [1, 1, 1], [128, 128, 32], 3) add_test_tensorop(GemmBF16Sm90, LayoutCombination.NNN, [4, 4, 8], cutlass.bfloat16, cutlass.float32, cutlass.float32, [1, 1, 1], [128, 128, 32], 5) add_test_tensorop(GemmBF16Sm90, LayoutCombination.TNN, [8, 8, 8], cutlass.bfloat16, cutlass.float32, cutlass.float32, [2, 1, 1], [128, 128, 32], None) add_test_tensorop(GemmBF16Sm90, LayoutCombination.TNN, [8, 8, 8], cutlass.bfloat16, cutlass.float32, cutlass.float32, [2, 1, 1], [128, 128, 32], None, persistent=True) add_test_simt(GemmBF16Sm90, LayoutCombination.NNN, [1, 1, 1], cutlass.bfloat16, cutlass.float32, cutlass.float32, [1, 1, 1], [128, 128, 8], 2) if __name__ == '__main__': pycutlass.get_memory_pool(2**30, 2**30) unittest.main()
################################################################################################# # # Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: BSD-3-Clause # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # 3. Neither the name of the copyright holder nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ################################################################################################# from functools import partial import pycutlass from pycutlass import * from pycutlass import library from pycutlass.test import * import unittest from pycutlass.test.utils import LayoutCombination, get_name from pycutlass.test.gemm_testbed import test_all_gemm from pycutlass.utils.device import device_cc name_fn = partial(get_name, element_a=cutlass.float64, element_b=cutlass.float64, arch=90) def add_test(cls, layouts, alignments, element_output, element_accumulator, element_epilogue, cluster_shape, threadblock_shape, stages, opclass): """ Create a test-running function with the given specification and set it as a method of `cls`. :param cls: class to which the generated method will be added :type cls: type :param layouts: indexable container of layouts of A, B, and C operands :param alignments: indexable container of alignments of A, B, and C operands :param element_output: data type of the output element :param element_accumulator: data type used in accumulation :param element_epilogue: data type used in computing the epilogue :param cluster_shape: indexable container of dimensions of threadblock cluster to be launched :param threadblock_shape: indexable container of dimensions of threadblock tiles :param stages: number of pipeline stages to use in the kernel :type stages: int :param opclass: class of operation being performed (e.g., SIMT, Tensor Core) :type opclass: cutlass.OpClass """ def run(self): """ Dynamically-generated function that constructs a GEMM operation and verifies it against multiple test cases. """ element_A = cutlass.float64 element_B = cutlass.float64 inst_shape = [1, 1, 1] if opclass == cutlass.OpClass.Simt else None warp_count = [2, 2, 1] if opclass == cutlass.OpClass.Simt else None math_inst = MathInstruction( instruction_shape=inst_shape, element_a=element_A, element_b=element_B, element_accumulator=element_accumulator, opcode_class=opclass, math_operation=MathOperation.multiply_add ) tile_description = TileDescription( threadblock_shape=threadblock_shape, cluster_shape=cluster_shape, stages=stages, warp_count=warp_count, math_instruction=math_inst ) A = TensorDescription(element=element_A, layout=layouts[0], alignment=alignments[0]) B = TensorDescription(element=element_B, layout=layouts[1], alignment=alignments[1]) C = TensorDescription(element=element_output, layout=layouts[2], alignment=alignments[2]) epilogue_functor = LinearCombination(C.element, C.alignment, math_inst.element_accumulator, element_epilogue) swizzling_functor = cutlass.IdentitySwizzle1 operation = GemmOperationUniversal( arch=90, tile_description=tile_description, A=A, B=B, C=C, epilogue_functor=epilogue_functor, swizzling_functor=swizzling_functor) self.assertTrue(test_all_gemm(operation, "universal")) name = name_fn(layouts, alignments, element_output, element_accumulator, element_epilogue, cluster_shape, threadblock_shape, stages, opclass=opclass) setattr(cls, name, run) return run @unittest.skipIf(device_cc() < 90, "Device compute capability is insufficient for SM90 tests.") class GemmF64Sm90(unittest.TestCase): """ Wrapper class to which tests will be added dynamically in __main__ """ pass add_test_simt = partial(add_test, opclass=cutlass.OpClass.Simt) add_test_simt(GemmF64Sm90, LayoutCombination.NNN, [1, 1, 1], cutlass.float64, cutlass.float64, cutlass.float64, [1, 1, 1], [64, 64, 32], 2) if __name__ == '__main__': pycutlass.get_memory_pool(2**30, 2**30) unittest.main()
################################################################################################# # # Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: BSD-3-Clause # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # 3. Neither the name of the copyright holder nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ################################################################################################# import pycutlass from pycutlass import * from pycutlass.test import * import unittest from pycutlass.test.gemm_testbed import test_all_gemm from pycutlass.utils.device import device_cc @unittest.skipIf(device_cc() < 80, "Device compute capability is insufficient for SM80 tests.") class GemmF64TensorOpSm80(unittest.TestCase): def test_SM80_Device_Gemm_f64n_f64t_f64t_tensor_op_f64_32x32x16_16x16x16(self): math_inst = MathInstruction( instruction_shape=[8, 8, 4], element_a=cutlass.float64, element_b=cutlass.float64, element_accumulator=cutlass.float64, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add ) tile_description = TileDescription( threadblock_shape=[32, 32, 16], stages=4, warp_count=[2, 2, 1], math_instruction=math_inst ) # alignment 1 restricted for double A = TensorDescription( element=cutlass.float64, layout=cutlass.ColumnMajor, alignment=1 ) B = TensorDescription( element=cutlass.float64, layout=cutlass.RowMajor, alignment=1 ) C = TensorDescription( element=cutlass.float64, layout=cutlass.RowMajor, alignment=1 ) element_epilogue = cutlass.float64 epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, element_epilogue) swizzling_functor = cutlass.IdentitySwizzle1 operation = GemmOperationUniversal( arch=80, tile_description=tile_description, A=A, B=B, C=C, epilogue_functor=epilogue_functor, swizzling_functor=swizzling_functor ) self.assertTrue(test_all_gemm(operation, "universal")) def test_SM80_Device_Gemm_f64t_f64n_f64t_tensor_op_f64_64x64x16_32x32x16(self): math_inst = MathInstruction( instruction_shape=[8, 8, 4], element_a=cutlass.float64, element_b=cutlass.float64, element_accumulator=cutlass.float64, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add ) tile_description = TileDescription( threadblock_shape=[64, 64, 16], stages=4, warp_count=[2, 2, 1], math_instruction=math_inst ) # alignment 1 restricted for double A = TensorDescription( element=cutlass.float64, layout=cutlass.RowMajor, alignment=1 ) B = TensorDescription( element=cutlass.float64, layout=cutlass.ColumnMajor, alignment=1 ) C = TensorDescription( element=cutlass.float64, layout=cutlass.RowMajor, alignment=1 ) element_epilogue = cutlass.float64 epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, element_epilogue) swizzling_functor = cutlass.IdentitySwizzle1 operation = GemmOperationUniversal( arch=80, tile_description=tile_description, A=A, B=B, C=C, epilogue_functor=epilogue_functor, swizzling_functor=swizzling_functor ) self.assertTrue(test_all_gemm(operation, "universal")) if __name__ == '__main__': pycutlass.get_memory_pool(2**30, 2**30) unittest.main()
################################################################################################# # # Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: BSD-3-Clause # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # 3. Neither the name of the copyright holder nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ################################################################################################# import pycutlass from pycutlass import * from pycutlass.test import * import unittest from pycutlass.test.gemm_testbed import test_all_gemm from pycutlass.utils.device import device_cc @unittest.skipIf(device_cc() < 80, "Device compute capability is insufficient for SM80 tests.") class GemmBF16TensorOpSm80(unittest.TestCase): def SM80_Device_Gemm_bf16n_bf16n_f32t_tensor_op_f32_64x128x64_32x64x64(self): math_inst = MathInstruction( instruction_shape=[16, 8, 16], element_a=cutlass.bfloat16, element_b=cutlass.bfloat16, element_accumulator=cutlass.float32, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add ) tile_description = TileDescription( threadblock_shape=[64, 128, 64], stages=4, warp_count=[2, 2, 1], math_instruction=math_inst ) A = TensorDescription( element=cutlass.bfloat16, layout=cutlass.ColumnMajor, alignment=8 ) B = TensorDescription( element=cutlass.bfloat16, layout=cutlass.ColumnMajor, alignment=8 ) C = TensorDescription( element=cutlass.float32, layout=cutlass.RowMajor, alignment=4 ) epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, cutlass.float32) swizzling_functor = cutlass.IdentitySwizzle1 operation = GemmOperationUniversal( arch=80, tile_description=tile_description, A=A, B=B, C=C, epilogue_functor=epilogue_functor, swizzling_functor=swizzling_functor ) self.assertTrue(test_all_gemm(operation, "universal")) def test_SM80_Device_Gemm_bf16t_bf16t_bf16t_tensor_op_f32_128x256x64_64x64x64(self): math_inst = MathInstruction( instruction_shape=[16, 8, 16], element_a=cutlass.bfloat16, element_b=cutlass.bfloat16, element_accumulator=cutlass.float32, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add ) tile_description = TileDescription( threadblock_shape=[64, 128, 32], stages=6, warp_count=[2, 2, 1], math_instruction=math_inst ) A = TensorDescription( element=cutlass.bfloat16, layout=cutlass.RowMajor, alignment=8 ) B = TensorDescription( element=cutlass.bfloat16, layout=cutlass.RowMajor, alignment=8 ) C = TensorDescription( element=cutlass.bfloat16, layout=cutlass.RowMajor, alignment=8 ) epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, cutlass.float32) swizzling_functor = cutlass.IdentitySwizzle1 operation = GemmOperationUniversal( arch=80, tile_description=tile_description, A=A, B=B, C=C, epilogue_functor=epilogue_functor, swizzling_functor=swizzling_functor ) self.assertTrue(test_all_gemm(operation, "multistage")) if __name__ == '__main__': pycutlass.get_memory_pool(2**30, 2**30) unittest.main()
################################################################################################# # # Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: BSD-3-Clause # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # 3. Neither the name of the copyright holder nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ################################################################################################# import pycutlass from pycutlass import * from pycutlass.test import * import unittest from pycutlass.test.gemm_testbed import test_all_gemm from pycutlass.utils.device import device_cc @unittest.skipIf(device_cc() < 80, "Device compute capability is insufficient for SM80 tests.") class GemmF16Sm80(unittest.TestCase): def test_SM80_Device_Gemm_f32t_f32n_f32t_tensor_op_bf16_f32_128x128x32_64x64x32(self): math_inst = MathInstruction( instruction_shape=[16, 8, 16], element_a=cutlass.float16, element_b=cutlass.float16, element_accumulator=cutlass.float32, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add ) tile_description = TileDescription( threadblock_shape=[128, 128, 32], stages=3, warp_count=[2, 2, 1], math_instruction=math_inst ) A = TensorDescription( element=cutlass.float16, layout=cutlass.ColumnMajor, alignment=8 ) B = TensorDescription( element=cutlass.float16, layout=cutlass.RowMajor, alignment=8 ) C = TensorDescription( element=cutlass.float32, layout=cutlass.ColumnMajor, alignment=4 ) element_epilogue = cutlass.float32 epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, element_epilogue) swizzling_functor = cutlass.BatchedIdentitySwizzle operation = GemmOperationUniversal( arch=80, tile_description=tile_description, A=A, B=B, C=C, epilogue_functor=epilogue_functor, swizzling_functor=swizzling_functor, direct_store=True ) self.assertTrue(test_all_gemm(operation, "universal")) def test_SM80_Device_Gemm_f16n_f16n_f16t_tensor_op_f32_128x128x64_64x64x64(self): math_inst = MathInstruction( instruction_shape=[16, 8, 16], element_a=cutlass.float16, element_b=cutlass.float16, element_accumulator=cutlass.float32, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add ) tile_description = TileDescription( threadblock_shape=[128, 128, 64], stages=3, warp_count=[2, 2, 1], math_instruction=math_inst ) A = TensorDescription( element=cutlass.float16, layout=cutlass.ColumnMajor, alignment=8 ) B = TensorDescription( element=cutlass.float16, layout=cutlass.ColumnMajor, alignment=8 ) C = TensorDescription( element=cutlass.float16, layout=cutlass.RowMajor, alignment=8 ) element_epilogue = cutlass.float32 epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, element_epilogue) swizzling_functor = cutlass.IdentitySwizzle1 operation = GemmOperationUniversal( arch=80, tile_description=tile_description, A=A, B=B, C=C, epilogue_functor=epilogue_functor, swizzling_functor=swizzling_functor ) self.assertTrue(test_all_gemm(operation, "universal")) def test_SM80_Device_Gemm_f16n_f16n_f32n_tensor_op_f32_128x256x64_64x64x64(self): math_inst = MathInstruction( instruction_shape=[16, 8, 16], element_a=cutlass.float16, element_b=cutlass.float16, element_accumulator=cutlass.float32, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add ) tile_description = TileDescription( threadblock_shape=[128, 256, 64], stages=3, warp_count=[2, 4, 1], math_instruction=math_inst ) A = TensorDescription( element=cutlass.float16, layout=cutlass.ColumnMajor, alignment=8 ) B = TensorDescription( element=cutlass.float16, layout=cutlass.ColumnMajor, alignment=8 ) C = TensorDescription( element=cutlass.float32, layout=cutlass.ColumnMajor, alignment=4 ) element_epilogue = cutlass.float32 epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, element_epilogue) swizzling_functor = cutlass.IdentitySwizzle1 operation = GemmOperationUniversal( arch=80, tile_description=tile_description, A=A, B=B, C=C, epilogue_functor=epilogue_functor, swizzling_functor=swizzling_functor ) self.assertTrue(test_all_gemm(operation, "universal")) def test_SM80_Device_Gemm_f16n_f16n_f32t_tensor_op_f32_256x128x64_64x64x64(self): math_inst = MathInstruction( instruction_shape=[16, 8, 16], element_a=cutlass.float16, element_b=cutlass.float16, element_accumulator=cutlass.float32, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add ) tile_description = TileDescription( threadblock_shape=[256, 128, 64], stages=3, warp_count=[4, 2, 1], math_instruction=math_inst ) A = TensorDescription( element=cutlass.float16, layout=cutlass.ColumnMajor, alignment=8 ) B = TensorDescription( element=cutlass.float16, layout=cutlass.ColumnMajor, alignment=8 ) C = TensorDescription( element=cutlass.float32, layout=cutlass.RowMajor, alignment=4 ) element_epilogue = cutlass.float32 epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, element_epilogue) swizzling_functor = cutlass.IdentitySwizzle1 operation = GemmOperationUniversal( arch=80, tile_description=tile_description, A=A, B=B, C=C, epilogue_functor=epilogue_functor, swizzling_functor=swizzling_functor ) self.assertTrue(test_all_gemm(operation, "universal")) def test_SM80_Device_Gemm_f16n_f16t_f16t_tensor_op_f16_sliced_k_128x64x64_64x64x32(self): math_inst = MathInstruction( instruction_shape=[16, 8, 16], element_a=cutlass.float16, element_b=cutlass.float16, element_accumulator=cutlass.float16, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add ) tile_description = TileDescription( threadblock_shape=[128, 64, 64], stages=3, warp_count=[2, 1, 1], math_instruction=math_inst ) A = TensorDescription( element=cutlass.float16, layout=cutlass.ColumnMajor, alignment=8 ) B = TensorDescription( element=cutlass.float16, layout=cutlass.RowMajor, alignment=8 ) C = TensorDescription( element=cutlass.float16, layout=cutlass.RowMajor, alignment=4 ) element_epilogue = cutlass.float16 epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, element_epilogue) swizzling_functor = cutlass.IdentitySwizzle1 operation = GemmOperationUniversal( arch=80, tile_description=tile_description, A=A, B=B, C=C, epilogue_functor=epilogue_functor, swizzling_functor=swizzling_functor ) self.assertTrue(test_all_gemm(operation, "universal")) def test_SM80_Device_GemmUniversal_f16n_f16t_f32t_tensor_op_f32_64x64x32_32x32x32(self): math_inst = MathInstruction( instruction_shape=[16, 8, 16], element_a=cutlass.float16, element_b=cutlass.float16, element_accumulator=cutlass.float16, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add ) tile_description = TileDescription( threadblock_shape=[64, 64, 32], stages=10, warp_count=[2, 2, 1], math_instruction=math_inst ) A = TensorDescription( element=cutlass.float16, layout=cutlass.ColumnMajor, alignment=8 ) B = TensorDescription( element=cutlass.float16, layout=cutlass.RowMajor, alignment=8 ) C = TensorDescription( element=cutlass.float16, layout=cutlass.RowMajor, alignment=4 ) element_epilogue = cutlass.float16 epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, element_epilogue) swizzling_functor = cutlass.IdentitySwizzle1 operation = GemmOperationUniversal( arch=80, tile_description=tile_description, A=A, B=B, C=C, epilogue_functor=epilogue_functor, swizzling_functor=swizzling_functor ) self.assertTrue(test_all_gemm(operation, "universal")) def test_SM80_Device_Gemm_f16n_f16t_f32t_tensor_op_f32_256x128x64_64x64x64(self): math_inst = MathInstruction( instruction_shape=[16, 8, 16], element_a=cutlass.float16, element_b=cutlass.float16, element_accumulator=cutlass.float32, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add ) tile_description = TileDescription( threadblock_shape=[256, 128, 64], stages=3, warp_count=[4, 2, 1], math_instruction=math_inst ) A = TensorDescription( element=cutlass.float16, layout=cutlass.ColumnMajor, alignment=8 ) B = TensorDescription( element=cutlass.float16, layout=cutlass.RowMajor, alignment=8 ) C = TensorDescription( element=cutlass.float16, layout=cutlass.RowMajor, alignment=8 ) element_epilogue = cutlass.float32 epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, element_epilogue) swizzling_functor = cutlass.IdentitySwizzle1 operation = GemmOperationUniversal( arch=80, tile_description=tile_description, A=A, B=B, C=C, epilogue_functor=epilogue_functor, swizzling_functor=swizzling_functor ) self.assertTrue(test_all_gemm(operation, "universal")) def test_test_SM80_Device_Gemm_f16t_f16n_f16t_tensor_op_f16_sliced_k_128x64x64_64x64x32(self): math_inst = MathInstruction( instruction_shape=[16, 8, 16], element_a=cutlass.float16, element_b=cutlass.float16, element_accumulator=cutlass.float32, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add ) tile_description = TileDescription( threadblock_shape=[128, 64, 64], stages=3, warp_count=[2, 1, 1], math_instruction=math_inst ) A = TensorDescription( element=cutlass.float16, layout=cutlass.RowMajor, alignment=8 ) B = TensorDescription( element=cutlass.float16, layout=cutlass.ColumnMajor, alignment=8 ) C = TensorDescription( element=cutlass.float16, layout=cutlass.RowMajor, alignment=4 ) element_epilogue = cutlass.float32 epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, element_epilogue) swizzling_functor = cutlass.IdentitySwizzle1 operation = GemmOperationUniversal( arch=80, tile_description=tile_description, A=A, B=B, C=C, epilogue_functor=epilogue_functor, swizzling_functor=swizzling_functor ) self.assertTrue(test_all_gemm(operation, "universal")) def test_SM80_Device_Gemm_f16t_f16t_f32n_tensor_op_f32_128x256x64_64x64x64(self): math_inst = MathInstruction( instruction_shape=[16, 8, 16], element_a=cutlass.float16, element_b=cutlass.float16, element_accumulator=cutlass.float32, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add ) tile_description = TileDescription( threadblock_shape=[128, 256, 64], stages=3, warp_count=[2, 4, 1], math_instruction=math_inst ) A = TensorDescription( element=cutlass.float16, layout=cutlass.RowMajor, alignment=8 ) B = TensorDescription( element=cutlass.float16, layout=cutlass.RowMajor, alignment=8 ) C = TensorDescription( element=cutlass.float16, layout=cutlass.ColumnMajor, alignment=8 ) element_epilogue = cutlass.float32 epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, element_epilogue) swizzling_functor = cutlass.IdentitySwizzle1 operation = GemmOperationUniversal( arch=80, tile_description=tile_description, A=A, B=B, C=C, epilogue_functor=epilogue_functor, swizzling_functor=swizzling_functor ) self.assertTrue(test_all_gemm(operation, "universal")) def test_SM80_Device_Gemm_f16t_f16t_f32t_tensor_op_f32_128x256x64_64x64x64(self): math_inst = MathInstruction( instruction_shape=[16, 8, 16], element_a=cutlass.float16, element_b=cutlass.float16, element_accumulator=cutlass.float32, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add ) tile_description = TileDescription( threadblock_shape=[128, 256, 64], stages=3, warp_count=[2, 4, 1], math_instruction=math_inst ) A = TensorDescription( element=cutlass.float16, layout=cutlass.ColumnMajor, alignment=8 ) B = TensorDescription( element=cutlass.float16, layout=cutlass.ColumnMajor, alignment=8 ) C = TensorDescription( element=cutlass.float32, layout=cutlass.ColumnMajor, alignment=4 ) element_epilogue = cutlass.float32 epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, element_epilogue) swizzling_functor = cutlass.IdentitySwizzle1 operation = GemmOperationUniversal( arch=80, tile_description=tile_description, A=A, B=B, C=C, epilogue_functor=epilogue_functor, swizzling_functor=swizzling_functor ) self.assertTrue(test_all_gemm(operation, "universal")) if __name__ == '__main__': pycutlass.get_memory_pool(2**30, 2**30) unittest.main()
################################################################################################# # # Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: BSD-3-Clause # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # 3. Neither the name of the copyright holder nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ################################################################################################# from functools import partial import pycutlass from pycutlass import * from pycutlass import library from pycutlass.test import * import unittest from pycutlass.test.utils import LayoutCombination, get_name from pycutlass.test.gemm_testbed import test_all_gemm from pycutlass.utils.device import device_cc name_fn = partial(get_name, element_a=cutlass.float16, element_b=cutlass.float16, arch=90) def add_test(cls, layouts, alignments, element_output, element_accumulator, element_epilogue, cluster_shape, threadblock_shape, stages, opclass, persistent=False): """ Create a test-running function with the given specification and set it as a method of `cls`. :param cls: class to which the generated method will be added :type cls: type :param layouts: indexable container of layouts of A, B, and C operands :param alignments: indexable container of alignments of A, B, and C operands :param element_output: data type of the output element :param element_accumulator: data type used in accumulation :param element_epilogue: data type used in computing the epilogue :param cluster_shape: indexable container of dimensions of threadblock cluster to be launched :param threadblock_shape: indexable container of dimensions of threadblock tiles :param stages: number of pipeline stages to use in the kernel :type stages: int :param opclass: class of operation being performed (e.g., SIMT, Tensor Core) :type opclass: cutlass.OpClass :param persistent: whether this is a persistent warp-specialized kernel :type persistent: bool """ def run(self): """ Dynamically-generated function that constructs a GEMM operation and verifies it against multiple test cases. """ element_A = cutlass.int8 element_B = cutlass.int8 inst_shape = [1, 1, 1] if opclass == cutlass.OpClass.Simt else None warp_count = [2, 2, 1] if opclass == cutlass.OpClass.Simt else None math_inst = MathInstruction( instruction_shape=inst_shape, element_a=element_A, element_b=element_B, element_accumulator=element_accumulator, opcode_class=opclass, math_operation=MathOperation.multiply_add ) tile_description = TileDescription( threadblock_shape=threadblock_shape, cluster_shape=cluster_shape, stages=stages, warp_count=warp_count, math_instruction=math_inst, persistent=persistent ) A = TensorDescription(element=element_A, layout=layouts[0], alignment=alignments[0]) B = TensorDescription(element=element_B, layout=layouts[1], alignment=alignments[1]) C = TensorDescription(element=element_output, layout=layouts[2], alignment=alignments[2]) if opclass == cutlass.OpClass.Simt: epilogue_functor_cls = LinearCombinationClamp else: epilogue_functor_cls = LinearCombination epilogue_functor = epilogue_functor_cls(C.element, C.alignment, math_inst.element_accumulator, element_epilogue) swizzling_functor = cutlass.IdentitySwizzle1 operation = GemmOperationUniversal( arch=90, tile_description=tile_description, A=A, B=B, C=C, epilogue_functor=epilogue_functor, swizzling_functor=swizzling_functor) self.assertTrue(test_all_gemm(operation, "universal")) if persistent: suffix = "_persistent" else: suffix = "" name = name_fn(layouts, alignments, element_output, element_accumulator, element_epilogue, cluster_shape, threadblock_shape, stages, opclass=opclass, suffix=suffix) setattr(cls, name, run) return run @unittest.skipIf(device_cc() < 90, "Device compute capability is insufficient for SM90 tests.") class GemmS8Sm90(unittest.TestCase): """ Wrapper class to which tests will be added dynamically in __main__ """ pass add_test_tensorop = partial(add_test, opclass=cutlass.OpClass.TensorOp) add_test_simt = partial(add_test, opclass=cutlass.OpClass.Simt) # Tests with 1x1x1 clusters add_test_tensorop(GemmS8Sm90, LayoutCombination.TNN, [16, 16, 16], cutlass.int8, cutlass.int32, cutlass.int32, [1, 1, 1], [128, 128, 128], 3) add_test_tensorop(GemmS8Sm90, LayoutCombination.TNT, [16, 16, 16], cutlass.int8, cutlass.int32, cutlass.int32, [1, 1, 1], [128, 128, 128], None) add_test_tensorop(GemmS8Sm90, LayoutCombination.TNT, [16, 16, 8], cutlass.int8, cutlass.int32, cutlass.int32, [1, 1, 1], [128, 128, 128], None) add_test_tensorop(GemmS8Sm90, LayoutCombination.TNT, [16, 16, 16], cutlass.int8, cutlass.int32, cutlass.int32, [1, 1, 1], [64, 128, 128], None) add_test_tensorop(GemmS8Sm90, LayoutCombination.TNT, [16, 16, 16], cutlass.int8, cutlass.int32, cutlass.int32, [1, 1, 1], [128, 64, 32], None) add_test_tensorop(GemmS8Sm90, LayoutCombination.TNT, [4, 4, 16], cutlass.int8, cutlass.int32, cutlass.int32, [1, 1, 1], [128, 128, 128], None) # Tests with different cluster shapes add_test_tensorop(GemmS8Sm90, LayoutCombination.TNT, [16, 16, 16], cutlass.int8, cutlass.int32, cutlass.int32, [2, 2, 1], [128, 128, 128], None) add_test_tensorop(GemmS8Sm90, LayoutCombination.TNT, [16, 16, 16], cutlass.int8, cutlass.int32, cutlass.int32, [1, 4, 1], [128, 128, 128], None) add_test_tensorop(GemmS8Sm90, LayoutCombination.TNT, [16, 16, 16], cutlass.int8, cutlass.int32, cutlass.int32, [4, 4, 1], [128, 128, 128], None) # Tests with persistent warp-specialized threadblocks add_test_tensorop(GemmS8Sm90, LayoutCombination.TNT, [16, 16, 16], cutlass.int8, cutlass.int32, cutlass.int32, [2, 1, 1], [128, 128, 128], None, persistent=True) # Tests for SIMT add_test_simt(GemmS8Sm90, LayoutCombination.TNN, [1, 1, 1], cutlass.int8, cutlass.int32, cutlass.int32, [1, 1, 1], [64, 32, 8], 2) if __name__ == '__main__': pycutlass.get_memory_pool(2**30, 2**30) unittest.main()
################################################################################################# # # Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: BSD-3-Clause # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # 3. Neither the name of the copyright holder nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ################################################################################################# import pycutlass from pycutlass import * from pycutlass.memory_manager import get_allocated_size from pycutlass.test import * import unittest from pycutlass.test.gemm_testbed import test_all_gemm from pycutlass.utils.device import device_cc @unittest.skipIf(device_cc() < 80, "Device compute capability is insufficient for SM80 tests.") class GemmF32nF32nF32nTensorOpF32Sm80(unittest.TestCase): def test_SM80_Device_Gemm_f32t_f32n_f32t_tensor_op_bf16_f32_128x128x32_64x64x32(self): math_inst = MathInstruction( instruction_shape=[16, 8, 8], element_a=cutlass.float32, element_b=cutlass.float32, element_accumulator=cutlass.float32, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add_fast_bf16 ) tile_description = TileDescription( threadblock_shape=[128, 128, 32], stages=3, warp_count=[2, 2, 1], math_instruction=math_inst ) A = TensorDescription( element=cutlass.float32, layout=cutlass.RowMajor, alignment=4 ) B = TensorDescription( element=cutlass.float32, layout=cutlass.ColumnMajor, alignment=4 ) C = TensorDescription( element=cutlass.float32, layout=cutlass.RowMajor, alignment=4 ) element_epilogue = cutlass.float32 epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, element_epilogue) swizzling_functor = cutlass.IdentitySwizzle1 operation = GemmOperationUniversal( arch=80, tile_description=tile_description, A=A, B=B, C=C, epilogue_functor=epilogue_functor, swizzling_functor=swizzling_functor ) self.assertTrue(test_all_gemm(operation, "universal")) def test_SM80_Device_Gemm_f32n_f32n_f32t_tensor_op_f32_128x128x32_64x64x32(self): math_inst = MathInstruction( instruction_shape=[16, 8, 8], element_a=cutlass.float32, element_b=cutlass.float32, element_accumulator=cutlass.float32, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add ) tile_description = TileDescription( threadblock_shape=[128, 128, 32], stages=3, warp_count=[2, 2, 1], math_instruction=math_inst ) A = TensorDescription( element=cutlass.float32, layout=cutlass.ColumnMajor, alignment=4 ) B = TensorDescription( element=cutlass.float32, layout=cutlass.ColumnMajor, alignment=4 ) C = TensorDescription( element=cutlass.float32, layout=cutlass.RowMajor, alignment=4 ) element_epilogue = cutlass.float32 epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, element_epilogue) swizzling_functor = cutlass.IdentitySwizzle1 operation = GemmOperationUniversal( arch=80, tile_description=tile_description, A=A, B=B, C=C, epilogue_functor=epilogue_functor, swizzling_functor=swizzling_functor ) self.assertTrue(test_all_gemm(operation, "universal")) def test_SM80_Device_Gemm_f32n_f32n_f32t_tensor_op_fast_accurate_f32_64x64x32_32x32x32(self): math_inst = MathInstruction( instruction_shape=[16, 8, 8], element_a=cutlass.float32, element_b=cutlass.float32, element_accumulator=cutlass.float32, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add_fast_f32 ) tile_description = TileDescription( threadblock_shape=[64, 64, 32], stages=3, warp_count=[2, 2, 1], math_instruction=math_inst ) A = TensorDescription( element=cutlass.float32, layout=cutlass.ColumnMajor, alignment=4 ) B = TensorDescription( element=cutlass.float32, layout=cutlass.ColumnMajor, alignment=4 ) C = TensorDescription( element=cutlass.float32, layout=cutlass.RowMajor, alignment=4 ) element_epilogue = cutlass.float32 epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, element_epilogue) swizzling_functor = cutlass.IdentitySwizzle1 operation = GemmOperationUniversal( arch=80, tile_description=tile_description, A=A, B=B, C=C, epilogue_functor=epilogue_functor, swizzling_functor=swizzling_functor ) self.assertTrue(test_all_gemm(operation, "universal")) if __name__ == '__main__': pycutlass.get_memory_pool(2**24, 2**24) pycutlass.compiler.load_from_cache() unittest.main()
################################################################################################# # # Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: BSD-3-Clause # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # 3. Neither the name of the copyright holder nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ################################################################################################# import pycutlass from pycutlass import * from pycutlass.epilogue import LinearCombinationClamp from pycutlass.test import * import unittest from pycutlass.test.gemm_testbed import test_all_gemm from pycutlass.utils.device import device_cc @unittest.skipIf(device_cc() < 80, "Device compute capability is insufficient for SM80 tests.") class GemmS8TensorOpF32Sm80(unittest.TestCase): def test_SM80_Device_Gemm_s8t_s8n_s8t_tensor_op_s32_64x64x64_32x32x64(self): math_inst = MathInstruction( instruction_shape=[16, 8, 32], element_a=cutlass.int8, element_b=cutlass.int8, element_accumulator=cutlass.int32, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add_saturate ) tile_description = TileDescription( threadblock_shape=[64, 64, 64], stages=6, warp_count=[2, 2, 1], math_instruction=math_inst ) A = TensorDescription( element=cutlass.int8, layout=cutlass.ColumnMajorInterleaved32, alignment=16 ) B = TensorDescription( element=cutlass.int8, layout=cutlass.RowMajorInterleaved32, alignment=16 ) C = TensorDescription( element=cutlass.int8, layout=cutlass.ColumnMajorInterleaved32, alignment=8 ) epilogue_functor = FastLinearCombinationClamp( C.element, C.alignment ) swizzling_functor = cutlass.IdentitySwizzle1 operation = GemmOperationUniversal( arch=80, tile_description=tile_description, A=A, B=B, C=C, epilogue_functor=epilogue_functor, swizzling_functor=swizzling_functor ) self.assertTrue(test_all_gemm(operation, "interleaved")) def test_SM80_Device_Gemm_s8t_s8n_s8t_tensor_op_s32_256x128x128_64x64x128(self): math_inst = MathInstruction( instruction_shape=[16, 8, 32], element_a=cutlass.int8, element_b=cutlass.int8, element_accumulator=cutlass.int32, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add ) tile_description = TileDescription( threadblock_shape=[128, 128, 128], stages=3, warp_count=[2, 2, 1], math_instruction=math_inst ) A = TensorDescription( element=cutlass.int8, layout=cutlass.RowMajor, alignment=16 ) B = TensorDescription( element=cutlass.int8, layout=cutlass.ColumnMajor, alignment=16 ) C = TensorDescription( element=cutlass.int8, layout=cutlass.RowMajor, alignment=16 ) epilogue_functor = FastLinearCombinationClamp( C.element, C.alignment ) swizzling_functor = cutlass.IdentitySwizzle1 operation = GemmOperationUniversal( arch=80, tile_description=tile_description, A=A, B=B, C=C, epilogue_functor=epilogue_functor, swizzling_functor=swizzling_functor ) self.assertTrue(test_all_gemm(operation, "multistage")) def test_SM80_Device_Gemm_s8t_s8n_s8n_tensor_op_s32_128x128x128_64x64x128(self): math_inst = MathInstruction( instruction_shape=[16, 8, 32], element_a=cutlass.int8, element_b=cutlass.int8, element_accumulator=cutlass.int32, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add ) tile_description = TileDescription( threadblock_shape=[128, 128, 128], stages=3, warp_count=[2, 2, 1], math_instruction=math_inst ) A = TensorDescription( element=cutlass.int8, layout=cutlass.RowMajor, alignment=16 ) B = TensorDescription( element=cutlass.int8, layout=cutlass.ColumnMajor, alignment=16 ) C = TensorDescription( element=cutlass.int8, layout=cutlass.ColumnMajor, alignment=16 ) epilogue_functor = FastLinearCombinationClamp( C.element, C.alignment ) swizzling_functor = cutlass.IdentitySwizzle1 operation = GemmOperationUniversal( arch=80, tile_description=tile_description, A=A, B=B, C=C, epilogue_functor=epilogue_functor, swizzling_functor=swizzling_functor ) self.assertTrue(test_all_gemm(operation, "multistage")) def test_SM80_Device_Gemm_s8t_s8n_s32n_tensor_op_s32_128x128x128_64x64x128(self): math_inst = MathInstruction( instruction_shape=[16, 8, 32], element_a=cutlass.int8, element_b=cutlass.int8, element_accumulator=cutlass.int32, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add ) tile_description = TileDescription( threadblock_shape=[128, 128, 128], stages=3, warp_count=[2, 2, 1], math_instruction=math_inst ) A = TensorDescription( element=cutlass.int8, layout=cutlass.RowMajor, alignment=16 ) B = TensorDescription( element=cutlass.int8, layout=cutlass.ColumnMajor, alignment=16 ) C = TensorDescription( element=cutlass.int32, layout=cutlass.ColumnMajor, alignment=4 ) element_epilogue = cutlass.int32 epilogue_functor = LinearCombinationClamp( C.element, C.alignment, math_inst.element_accumulator, element_epilogue ) swizzling_functor = cutlass.IdentitySwizzle1 operation = GemmOperationUniversal( arch=80, tile_description=tile_description, A=A, B=B, C=C, epilogue_functor=epilogue_functor, swizzling_functor=swizzling_functor ) self.assertTrue(test_all_gemm(operation, "multistage")) def test_SM80_Device_Gemm_s8t_s8n_s32t_tensor_op_s32_128x128x128_64x64x128(self): math_inst = MathInstruction( instruction_shape=[16, 8, 32], element_a=cutlass.int8, element_b=cutlass.int8, element_accumulator=cutlass.int32, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add ) tile_description = TileDescription( threadblock_shape=[128, 128, 128], stages=3, warp_count=[2, 2, 1], math_instruction=math_inst ) A = TensorDescription( element=cutlass.int8, layout=cutlass.RowMajor, alignment=16 ) B = TensorDescription( element=cutlass.int8, layout=cutlass.ColumnMajor, alignment=16 ) C = TensorDescription( element=cutlass.int32, layout=cutlass.RowMajor, alignment=4 ) element_epilogue = cutlass.int32 epilogue_functor = LinearCombinationClamp( C.element, C.alignment, math_inst.element_accumulator, element_epilogue ) swizzling_functor = cutlass.IdentitySwizzle1 operation = GemmOperationUniversal( arch=80, tile_description=tile_description, A=A, B=B, C=C, epilogue_functor=epilogue_functor, swizzling_functor=swizzling_functor ) self.assertTrue(test_all_gemm(operation, "multistage")) if __name__ == '__main__': pycutlass.get_memory_pool(2**30, 2**30) unittest.main()
################################################################################################# # # Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: BSD-3-Clause # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # 3. Neither the name of the copyright holder nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ################################################################################################# import pycutlass from pycutlass import * from pycutlass.test import * import unittest from pycutlass.test.gemm_grouped_testbed import TestbedGrouped from pycutlass.utils.device import device_cc @unittest.skipIf(device_cc() < 80, "Device compute capability is insufficient for SM80 tests.") class GemmGroupedSm80(unittest.TestCase): def test_SM80_Device_GemmGrouped_f16n_f16t_f32n_tensor_op_f32_128x128x32_64x64x32(self): math_inst = MathInstruction( instruction_shape=[16, 8, 16], element_a=cutlass.float16, element_b=cutlass.float16, element_accumulator=cutlass.float32, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add ) tile_description = TileDescription( threadblock_shape=[128, 128, 32], stages=3, warp_count=[2, 2, 1], math_instruction=math_inst ) A = TensorDescription( element=cutlass.float16, layout=cutlass.ColumnMajor, alignment=8 ) B = TensorDescription( element=cutlass.float16, layout=cutlass.ColumnMajor, alignment=8 ) C = TensorDescription( element=cutlass.float32, layout=cutlass.ColumnMajor, alignment=4 ) element_epilogue = cutlass.float32 epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, element_epilogue) swizzling_functor = cutlass.BatchedIdentitySwizzle for precompute_mode in [SchedulerMode.Device, SchedulerMode.Host]: operation = GemmOperationGrouped( 80, tile_description, A, B, C, epilogue_functor, swizzling_functor, precompute_mode=precompute_mode ) testbed = TestbedGrouped(operation=operation) self.assertTrue(testbed.run(24)) def test_SM80_Device_GemmGrouped_f64t_f64t_f64n_tensor_op_f64_64x64x16_32x32x16(self): math_inst = MathInstruction( instruction_shape=[8, 8, 4], element_a=cutlass.float64, element_b=cutlass.float64, element_accumulator=cutlass.float64, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add ) tile_description = TileDescription( threadblock_shape=[64, 64, 16], stages=4, warp_count=[2, 2, 1], math_instruction=math_inst ) A = TensorDescription( element=cutlass.float64, layout=cutlass.RowMajor, alignment=1 ) B = TensorDescription( element=cutlass.float64, layout=cutlass.RowMajor, alignment=1 ) C = TensorDescription( element=cutlass.float64, layout=cutlass.ColumnMajor, alignment=1 ) element_epilogue = cutlass.float64 epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, element_epilogue) swizzling_functor = cutlass.BatchedIdentitySwizzle for precompute_mode in [SchedulerMode.Device, SchedulerMode.Host]: operation = GemmOperationGrouped( 80, tile_description, A, B, C, epilogue_functor, swizzling_functor, precompute_mode=precompute_mode ) testbed = TestbedGrouped(operation=operation) self.assertTrue(testbed.run(24)) def test_SM80_Device_GemmGrouped_f32t_f32t_f32t_simt_f32_128x64x8_64x32x1(self): math_inst = MathInstruction( instruction_shape=[1, 1, 1], element_a=cutlass.float32, element_b=cutlass.float32, element_accumulator=cutlass.float32, opcode_class=cutlass.OpClass.Simt, math_operation=MathOperation.multiply_add ) tile_description = TileDescription( threadblock_shape=[128, 64, 8], stages=4, warp_count=[2, 2, 1], math_instruction=math_inst ) A = TensorDescription( element=cutlass.float32, layout=cutlass.RowMajor, alignment=1 ) B = TensorDescription( element=cutlass.float32, layout=cutlass.RowMajor, alignment=1 ) C = TensorDescription( element=cutlass.float32, layout=cutlass.RowMajor, alignment=1 ) element_epilogue = cutlass.float32 epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, element_epilogue) swizzling_functor = cutlass.BatchedIdentitySwizzle for precompute_mode in [SchedulerMode.Device, SchedulerMode.Host]: operation = GemmOperationGrouped( 80, tile_description, A, B, C, epilogue_functor, swizzling_functor, precompute_mode=precompute_mode ) testbed = TestbedGrouped(operation=operation) self.assertTrue(testbed.run(27)) def test_SM80_Device_GemmGrouped_f16n_f16t_f32n_tensor_op_f32_128x128x32_64x64x32_cache(self): math_inst = MathInstruction( instruction_shape=[16, 8, 16], element_a=cutlass.float16, element_b=cutlass.float16, element_accumulator=cutlass.float32, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add ) tile_description = TileDescription( threadblock_shape=[128, 128, 32], stages=3, warp_count=[2, 2, 1], math_instruction=math_inst ) A = TensorDescription( element=cutlass.float16, layout=cutlass.ColumnMajor, alignment=8 ) B = TensorDescription( element=cutlass.float16, layout=cutlass.ColumnMajor, alignment=8 ) C = TensorDescription( element=cutlass.float32, layout=cutlass.ColumnMajor, alignment=4 ) element_epilogue = cutlass.float32 epilogue_functor = LinearCombination( C.element, C.alignment, math_inst.element_accumulator, element_epilogue) swizzling_functor = cutlass.BatchedIdentitySwizzle for precompute_mode in [SchedulerMode.Device, SchedulerMode.Host]: operation = GemmOperationGrouped( 80, tile_description, A, B, C, epilogue_functor, swizzling_functor, precompute_mode=precompute_mode ) testbed = TestbedGrouped(operation=operation) self.assertTrue(testbed.run(5)) if __name__ == '__main__': pycutlass.get_memory_pool(2**30, 2**30) unittest.main()
################################################################################################# # # Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: BSD-3-Clause # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # 3. Neither the name of the copyright holder nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ################################################################################################# import pycutlass import unittest if __name__ == '__main__': pycutlass.get_memory_pool(2**30, 2**30) loader = unittest.TestLoader() tests = loader.discover('./', 'gemm_*.py') testRunner = unittest.runner.TextTestRunner() testRunner.run(tests)
################################################################################################# # # Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: BSD-3-Clause # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # 3. Neither the name of the copyright holder nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ################################################################################################# # Configuration file for the Sphinx documentation builder. # # This file only contains a selection of the most common options. For a full # list see the documentation: # https://www.sphinx-doc.org/en/master/usage/configuration.html # -- Path setup -------------------------------------------------------------- # If extensions (or modules to document with autodoc) are in another directory, # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. # # import os # import sys # sys.path.insert(0, os.path.abspath('.')) # -- Project information ----------------------------------------------------- project = 'PyCutlass' copyright = '2022, Zhaodong Chen; Andrew Kerr; Haicheng Wu; Szymon Migacz; Graham Markall' author = 'Zhaodong Chen; Andrew Kerr; Haicheng Wu; Szymon Migacz; Graham Markall' # -- General configuration --------------------------------------------------- # Add any Sphinx extension module names here, as strings. They can be # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom # ones. extensions = [ 'sphinx.ext.duration', 'sphinx.ext.doctest', 'sphinx.ext.autodoc', 'sphinx.ext.intersphinx', 'enum_tools.autoenum', 'sphinx.ext.autosummary', 'm2r2' ] source_suffix = [".rst", ".md"] autosummary_generate = True autosummary_imported_members = True # Add any paths that contain templates here, relative to this directory. templates_path = ['_templates'] # List of patterns, relative to source directory, that match files and # directories to ignore when looking for source files. # This pattern also affects html_static_path and html_extra_path. exclude_patterns = [] # -- Options for HTML output ------------------------------------------------- # The theme to use for HTML and HTML Help pages. See the documentation for # a list of builtin themes. # html_theme = 'bizstyle' # Add any paths that contain custom static files (such as style sheets) here, # relative to this directory. They are copied after the builtin static files, # so a file named "default.css" will overwrite the builtin "default.css". # html_static_path = ['_static']
################################################################################################# # # Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: BSD-3-Clause # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # 3. Neither the name of the copyright holder nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ################################################################################################# from pycutlass import * import pycutlass from pycutlass.epilogue import LinearCombination from pycutlass.test.conv2d_testbed import Conv2dLauncher if __name__ == "__main__": pycutlass.get_memory_pool(2**33, 2**33) pycutlass.compiler.nvcc() math_inst = MathInstruction( instruction_shape=[16, 8, 16], element_a=cutlass.float16, element_b=cutlass.float16, element_accumulator=cutlass.float32, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add ) A = TensorDescription( element=math_inst.element_a, layout=cutlass.TensorNHWC, alignment=8) B = TensorDescription( element=math_inst.element_b, layout=cutlass.TensorNHWC, alignment=8) C = TensorDescription( element=cutlass.float32, layout=cutlass.TensorNHWC, alignment=8) tile_description = TileDescription( threadblock_shape=[128, 128, 64], stages=4, warp_count=[2, 2, 1], math_instruction=math_inst ) epilogue_functor = LinearCombination(cutlass.float32, 4, cutlass.float32, cutlass.float32) operation = Conv2dOperation( conv_kind=cutlass.conv.Operator.fprop, iterator_algorithm=cutlass.conv.IteratorAlgorithm.optimized, arch=80, tile_description=tile_description, A=A, B=B, C=C, element_epilogue=cutlass.float32, stride_support=StrideSupport.Strided, epilogue_functor=epilogue_functor, swizzling_functor=cutlass.IdentitySwizzle1 ) profiler = Conv2dLauncher(operation, verification=False, profiling=True) python_runtime = profiler.run( problem_size = cutlass.conv.Conv2dProblemSize( cutlass.Tensor4DCoord(32, 224, 224, 128), cutlass.Tensor4DCoord(128, 3, 3, 128), cutlass.Tensor4DCoord(1, 1, 1, 1), cutlass.MatrixCoord(1, 1), cutlass.MatrixCoord(1, 1), cutlass.conv.Mode.cross_correlation, 1, 1 ), split_k_mode=cutlass.conv.SplitKMode.Serial ) cpp_runtime = profiler.run_cutlass_profiler( problem_size = cutlass.conv.Conv2dProblemSize( cutlass.Tensor4DCoord(32, 224, 224, 128), cutlass.Tensor4DCoord(128, 3, 3, 128), cutlass.Tensor4DCoord(1, 1, 1, 1), cutlass.MatrixCoord(1, 1), cutlass.MatrixCoord(1, 1), cutlass.conv.Mode.cross_correlation, 1, 1 ), split_k_mode=cutlass.conv.SplitKMode.Serial ) print(cpp_runtime / python_runtime)
################################################################################################# # # Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: BSD-3-Clause # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # 3. Neither the name of the copyright holder nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ################################################################################################# import pycutlass from pycutlass import * from pycutlass.test import * from pycutlass.test.gemm_testbed import GemmUniversalLauncher if __name__ == '__main__': pycutlass.get_memory_pool(2**32, 2**32) pycutlass.compiler.nvcc() math_inst = MathInstruction( instruction_shape=[16, 8, 16], element_a=cutlass.float16, element_b=cutlass.float16, element_accumulator=cutlass.float32, opcode_class=cutlass.OpClass.TensorOp, math_operation=MathOperation.multiply_add ) tile_description = TileDescription( threadblock_shape=[256, 128, 32], stages=3, warp_count=[4, 2, 1], math_instruction=math_inst ) A = TensorDescription( element=cutlass.float16, layout=cutlass.RowMajor, alignment=4 ) B = TensorDescription( element=cutlass.float16, layout=cutlass.RowMajor, alignment=4 ) C = TensorDescription( element=cutlass.float32, layout=cutlass.ColumnMajor, alignment=4 ) element_epilogue = cutlass.float32 epilogue_functor = LinearCombination(cutlass.float32, 4, cutlass.float32, cutlass.float32) swizzling_functor = cutlass.IdentitySwizzle1 operation = GemmOperationUniversal( arch=80, tile_description=tile_description, A=A, B=B, C=C, element_epilogue=element_epilogue, epilogue_functor=epilogue_functor, swizzling_functor=swizzling_functor ) profiler = GemmUniversalLauncher(operation, verification=False, profiling=True) python_runtime = profiler.run( mode=cutlass.gemm.Mode.Gemm, problem_size=cutlass.gemm.GemmCoord(4096, 4096, 4096) ) cpp_runtime = profiler.run_cutlass_profiler( mode=cutlass.gemm.Mode.Gemm, problem_size=cutlass.gemm.GemmCoord(4096, 4096, 4096), ) print(cpp_runtime / python_runtime)
################################################################################################# # # Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: BSD-3-Clause # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # 3. Neither the name of the copyright holder nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ################################################################################################# import ctypes from pycutlass.library import * class GemmCoord_(ctypes.Structure): _fields_ = [ ("m", ctypes.c_int), ("n", ctypes.c_int), ("k", ctypes.c_int) ] def __init__(self, gemm_coord) -> None: for field_name, _ in self._fields_: setattr(self, field_name, getattr(gemm_coord, field_name)()) class GemmCoordBatched_(ctypes.Structure): """ Wrapper around a GemmCoord that also contains batch count. This is used for encoding batched GEMM inputs to CUTLASS 3 GEMMs. """ _fields_ = [ ("m", ctypes.c_int), ("n", ctypes.c_int), ("k", ctypes.c_int), ("batch_count", ctypes.c_int) ] def __init__(self, gemm_coord, batch_count) -> None: for field_name, _ in self._fields_[:-1]: setattr(self, field_name, getattr(gemm_coord, field_name)()) setattr(self, "batch_count", batch_count) class MatrixCoord_(ctypes.Structure): _fields_ = [ ("row", ctypes.c_int), ("column", ctypes.c_int) ] class dim3_(ctypes.Structure): _fields_ = [ ("x", ctypes.c_int), ("y", ctypes.c_int), ("z", ctypes.c_int) ] class StrideBatched_(ctypes.Structure): """ CUTLASS 3.0 strides for operands contain one static dimension and two variable dimensions. The variable dimensions represent the stride along non-unit-stride dimension of the row/column major layout, and the batch stride. This structure encodes the two variable dimensions. """ _fields_ = [ ("major_stride", ctypes.c_int64), ("batch_stride", ctypes.c_int64) ] dtype2ctype = { cutlass.float16: ctypes.c_uint16, cutlass.float32: ctypes.c_float, cutlass.float64: ctypes.c_double, cutlass.int32: ctypes.c_int32 } def get_gemm_arguments_3x(epilogue_functor): _EpilogueOutputOpParams = epilogue_functor.epilogue_type class _GemmArguments(ctypes.Structure): _fields_ = [ ("mode", ctypes.c_int), ("problem_size", GemmCoordBatched_), ("ptr_A", ctypes.c_void_p), ("stride_A", StrideBatched_), ("ptr_B", ctypes.c_void_p), ("stride_B", StrideBatched_), ("ptr_C", ctypes.c_void_p), ("stride_C", StrideBatched_), ("ptr_D", ctypes.c_void_p), ("stride_D", StrideBatched_), ("epilogue", _EpilogueOutputOpParams), ] return _GemmArguments, _EpilogueOutputOpParams def get_gemm_arguments(epilogue_functor): _EpilogueOutputOpParams = epilogue_functor.epilogue_type class _GemmArguments(ctypes.Structure): _fields_ = [ # Arguments from UniversalArgumentsBase ("mode", ctypes.c_int), ("problem_size", GemmCoord_), ("batch_count", ctypes.c_int), ("batch_stride_D", ctypes.c_longlong), # Remaining arguments ("epilogue", _EpilogueOutputOpParams), ("ptr_A", ctypes.c_void_p), ("ptr_B", ctypes.c_void_p), ("ptr_C", ctypes.c_void_p), ("ptr_D", ctypes.c_void_p), ("batch_stride_A", ctypes.c_longlong), ("batch_stride_B", ctypes.c_longlong), ("batch_stride_C", ctypes.c_longlong), ("stride_a", ctypes.c_longlong), ("stride_b", ctypes.c_longlong), ("stride_c", ctypes.c_longlong), ("stride_d", ctypes.c_longlong), ("lda", ctypes.c_longlong), ("ldb", ctypes.c_longlong), ("ldc", ctypes.c_longlong), ("ldd", ctypes.c_longlong), ("ptr_gather_A_indices", ctypes.c_void_p), ("ptr_gather_B_indices", ctypes.c_void_p), ("ptr_scatter_D_indices", ctypes.c_void_p) ] return _GemmArguments, _EpilogueOutputOpParams ########################################################################################### # GEMM Grouped ########################################################################################### def get_gemm_grouped_arguments(epilogue_functor): _EpilogueOutputOpParams = epilogue_functor.epilogue_type class _GEMMGroupedArguments(ctypes.Structure): _fields_ = [ ("problem_sizes", ctypes.c_void_p), ("problem_count", ctypes.c_int), ("threadblock_count", ctypes.c_int), ("output_op", _EpilogueOutputOpParams), ("ptr_A", ctypes.c_void_p), ("ptr_B", ctypes.c_void_p), ("ptr_C", ctypes.c_void_p), ("ptr_D", ctypes.c_void_p), ("lda", ctypes.c_void_p), ("ldb", ctypes.c_void_p), ("ldc", ctypes.c_void_p), ("ldd", ctypes.c_void_p), ("host_problem_sizes", ctypes.c_void_p) ] return _GEMMGroupedArguments, _EpilogueOutputOpParams ############################################################################################ # Convolution2D ############################################################################################ class Conv2DProblemSize(ctypes.Structure): _fields_ = [ ("N", ctypes.c_int), ("H", ctypes.c_int), ("W", ctypes.c_int), ("C", ctypes.c_int), ("P", ctypes.c_int), ("Q", ctypes.c_int), ("K", ctypes.c_int), ("R", ctypes.c_int), ("S", ctypes.c_int), ("pad_h", ctypes.c_int), ("pad_w", ctypes.c_int), ("stride_h", ctypes.c_int), ("stride_w", ctypes.c_int), ("dilation_h", ctypes.c_int), ("dilation_w", ctypes.c_int), ("mode", ctypes.c_int), # kCrossCorrelation: 0, kConvolution: 1 ("split_k_slices", ctypes.c_int), ("groups", ctypes.c_int) ] def __init__(self, problem_size) -> None: for field_name, _ in self._fields_: setattr(self, field_name, getattr(problem_size, field_name)) class Layout4D(ctypes.Structure): _fields_ = [ ("stride", ctypes.c_int * 3) ] def __init__(self, tensor_ref): stride = tensor_ref.stride() setattr(self, "stride", (stride.at(0), stride.at(1), stride.at(2))) class TensorRef_(ctypes.Structure): _fields_ = [ ("ptr", ctypes.c_void_p), ("layout", Layout4D) ] def __init__(self, tensor_ref): setattr(self, "ptr", tensor_ref.data()) setattr(self, "layout", Layout4D(tensor_ref.layout())) class TensorRef2D_(ctypes.Structure): _fields_ = [ ("ptr", ctypes.c_void_p), ("stride", ctypes.c_int) ] def get_conv2d_arguments(epilogue_functor): _EpilogueOutputOpParams = epilogue_functor.epilogue_type class _Conv2dArguments(ctypes.Structure): _fields_ = [ ("problem_size", Conv2DProblemSize), # 0 ("ref_A", TensorRef_), # 72 ("ref_B", TensorRef_), # 96 ("ref_C", TensorRef_), # 120 ("ref_D", TensorRef_), # 144 ("output_op", _EpilogueOutputOpParams), # 168 ("split_k_mode", ctypes.c_int) # 192 ] return _Conv2dArguments, _EpilogueOutputOpParams ############################################################################################ # Reduction ############################################################################################ def get_reduction_params(epilogue_functor): _EpilogueOutputParams = epilogue_functor.epilogue_type class _ReductionParams(ctypes.Structure): _fields_ = [ ("problem_size", MatrixCoord_), ("partitions", ctypes.c_int), ("partition_stride", ctypes.c_longlong), ("workspace", TensorRef2D_), ("destination", TensorRef2D_), ("source", TensorRef2D_), ("output_op", _EpilogueOutputParams) ] return _ReductionParams, _EpilogueOutputParams
################################################################################################# # # Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: BSD-3-Clause # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # 3. Neither the name of the copyright holder nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ################################################################################################# import rmm import numpy as np class PoolMemoryManager: def __init__(self, init_pool_size: int, max_pool_size: int) -> None: self.pool = rmm.mr.PoolMemoryResource( rmm.mr.CudaMemoryResource(), initial_pool_size=init_pool_size, maximum_pool_size=max_pool_size ) self.mr = rmm.mr.TrackingResourceAdaptor(self.pool) rmm.mr.set_current_device_resource(self.mr) def get_allocated_size(self): return self.mr.get_allocated_bytes() def pool_size(self): return self.pool.pool_size() def todevice(host_data, dtype=np.float32): """ Pass the host_data to device memory """ if isinstance(host_data, list): return rmm.DeviceBuffer.to_device(np.array(host_data, dtype=dtype).tobytes()) elif isinstance(host_data, np.ndarray): return rmm.DeviceBuffer.to_device(host_data.tobytes()) def device_mem_alloc(size): return rmm.DeviceBuffer(size=size) def align_size(size, alignment=256): return ((size + alignment - 1) // alignment) * alignment def get_allocated_size(): device_resource = rmm.mr.get_current_device_resource() return device_resource.get_allocated_bytes()
################################################################################################# # # Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: BSD-3-Clause # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # 3. Neither the name of the copyright holder nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ################################################################################################# import pycutlass from pycutlass import * import cutlass from cuda import cuda from cuda import nvrtc import tempfile import os import ctypes # import json import sqlite3 IncludeTemplate = r'''#include "${include}" ''' # class CompilationOptions: ''' Compilation options. ''' # def __init__(self, flags, arch, include_paths=[]): self.includes = [] self.include_paths = include_paths self.flags = flags self.arch = arch def get_str(self): options = "" for flag in self.flags: options += " " + flag for incl in self.include_paths: options += ' --include-path=%s' % incl arch_flag = " -arch=sm_%d" % self.arch if self.arch == 90: arch_flag += 'a' options += arch_flag return options # def get(self): options = [] for flag in self.flags: options.append(bytes(str.encode(flag))) for incl in self.include_paths: options.append(bytes(str.encode('--include-path=%s' % incl))) arch_flag = " -arch=sm_%d" % self.arch if self.arch == 90: arch_flag += 'a' options.append(bytes(str.encode(arch_flag))) return options def convertToBinaryData(filename): with open(filename, 'rb') as file: blobData = file.read() return blobData def CDLLBin(host_binary): tempfile.tempdir = "./" temp_so = tempfile.NamedTemporaryFile( prefix='host_func', suffix='.so', delete=True) with open(temp_so.name, 'wb') as file: file.write(host_binary) host_lib = ctypes.CDLL(temp_so.name) return host_lib class ArtifactManager: """ Artifact manager """ def __init__(self) -> None: try: connection = sqlite3.connect("./compiled_cache.db") cursor = connection.cursor() sqlite_create_table_query = """CREATE TABLE compiled_operations(op_key TEXT NOT NULL UNIQUE, cubin BLOB NOT NULL, hostbin BLOB NOT NULL, op_name TEXT NOT NULL, op_attrs TEXT NOT NULL)""" cursor.execute(sqlite_create_table_query) connection.commit() cursor.close() except: pass self.nvcc() self.compiled_cache_device = cutlass.CompileCache() self.compiled_cache_host = cutlass.CompileCache() def nvrtc(self): self.backend = "nvrtc" self.default_compile_options = [ '-std=c++17', '-default-device' ] def nvcc(self): self.backend = "nvcc" self.default_compile_options = [ '-std=c++17', '--expt-relaxed-constexpr', '-Xcudafe --diag_suppress=esa_on_defaulted_function_ignored' ] def insert_operation(self, op_key, cubin, hostfile, op_name, op_attrs): connection = sqlite3.connect("./compiled_cache.db") cursor = connection.cursor() sqlite_insert_blob_query = """ INSERT OR IGNORE INTO compiled_operations (op_key, cubin, hostbin, op_name, op_attrs) VALUES (?, ?, ?, ?, ?)""" hostbin = convertToBinaryData(hostfile) data_tuple = (op_key, cubin, hostbin, op_name, json.dumps(op_attrs)) cursor.execute(sqlite_insert_blob_query, data_tuple) connection.commit() cursor.close() def load_operation(self, op_key, extra_funcs): connection = sqlite3.connect("./compiled_cache.db") cursor = connection.cursor() sqlite_fetch_blob_query = """SELECT * from compiled_operations where op_key = ?""" # try: cursor.execute(sqlite_fetch_blob_query, (op_key, )) record = cursor.fetchall() if len(record) == 0: return False for row in record: key, cubin_image, host_binary, operation_name, op_attr = row op_attr = json.loads(op_attr) err, module = cuda.cuModuleLoadData(cubin_image) if err != cuda.CUresult.CUDA_SUCCESS: raise RuntimeError('Cuda Error: {}'.format(err)) err, kernel = cuda.cuModuleGetFunction( module, bytes(str.encode(operation_name))) self.compiled_cache_device.insert(key, kernel) compiled_host_fns = {} host_lib = CDLLBin(host_binary) func_name = operation_name + '_get_params' func = getattr(host_lib, func_name) func.restype = ctypes.POINTER(ctypes.c_char * op_attr[0]) compiled_host_fns['get_args'] = func func_name = operation_name + '_shared_memory_size' func = getattr(host_lib, func_name) compiled_host_fns['shared_memory_capacity'] = func() for attr in op_attr: if isinstance(attr, str): func_name = operation_name + '_' + attr func = getattr(host_lib, func_name) # Set the return type of the function if attr in extra_funcs and extra_funcs[attr] != None: func.restype = extra_funcs[attr] compiled_host_fns[attr] = func self.compiled_cache_host.insert(key, compiled_host_fns) return True def emit_compile_(self, operation_list, compilation_options, requires_nvcc_hostlib_compilation): """ Compile a list of kernels and store them into database """ source_buffer_device = "" source_buffer_host = "" # 1. include includes = [] for operation in operation_list: for incl in operation.emitter.includes: if incl not in includes: includes.append(incl) includes_host = [ "builtin_types.h", "device_launch_parameters.h", "stddef.h"] + includes for incl in includes: source_buffer_device += SubstituteTemplate( IncludeTemplate, {'include': incl}) for incl in includes_host: if "/device/" not in incl: source_buffer_host += SubstituteTemplate( IncludeTemplate, {'include': incl}) # 2. Operations for operation in operation_list: source_buffer_device += operation.emit() source_buffer_host += operation.emit() values = { 'operation_name': operation.name(), 'operation_suffix': operation.emitter.operation_suffix } source_buffer_device += SubstituteTemplate( operation.KernelTemplate, values) source_buffer_host += SubstituteTemplate( operation.HostTemplate, values) if self.backend == "nvrtc": # 3. compile err, program = nvrtc.nvrtcCreateProgram( str.encode(source_buffer_device), bytes(str.encode("module.cu")), 0, [], []) if err != nvrtc.nvrtcResult.NVRTC_SUCCESS: raise RuntimeError('NVRTC Error: {}'.format(err)) # Compile program options = compilation_options.get() err, = nvrtc.nvrtcCompileProgram(program, len(options), options) if err != nvrtc.nvrtcResult.NVRTC_SUCCESS: error_string = 'NVRTC Error: {}\n'.format(err) # Get log from compilation err, logSize = nvrtc.nvrtcGetProgramLogSize(program) if err != nvrtc.nvrtcResult.NVRTC_SUCCESS: raise RuntimeError('NVRTC Error: {}'.format(err)) log = b' ' * logSize err, = nvrtc.nvrtcGetProgramLog(program, log) if err != nvrtc.nvrtcResult.NVRTC_SUCCESS: raise RuntimeError('NVRTC Error: {}'.format(err)) raise RuntimeError( error_string + log.decode() + source_buffer_device) # Get data from compilation err, dataSize = nvrtc.nvrtcGetCUBINSize(program) if err != nvrtc.nvrtcResult.NVRTC_SUCCESS: raise RuntimeError('NVRTC Error: {}'.format(err)) cubin_image = b' ' * dataSize err, = nvrtc.nvrtcGetCUBIN(program, cubin_image) if err != nvrtc.nvrtcResult.NVRTC_SUCCESS: raise RuntimeError('NVRTC Error: {}'.format(err)) else: # with nvcc backend # emit code tempfile.tempdir = "./" temp_cu = tempfile.NamedTemporaryFile( prefix='kernel', suffix='.cu', delete=True) temp_cubin = tempfile.NamedTemporaryFile( prefix='kernel', suffix='.cubin', delete=True) with open(temp_cu.name, 'w') as file: file.write(source_buffer_device) # compile with nvcc cuda_install_path = os.getenv('CUDA_INSTALL_PATH') assert cuda_install_path is not None, "Environment variable 'CUDA_INSTALL_PATH' is not defined." cmd_template = "${cuda_install_path}/bin/nvcc ${options} -cubin ${srcfile} -o ${tarfile}" values = { "cuda_install_path": cuda_install_path, "options": compilation_options.get_str(), "srcfile": temp_cu.name, "tarfile": temp_cubin.name } cmd = SubstituteTemplate(cmd_template, values) os.system(cmd) # load the cubin image with open(temp_cubin.name, 'rb') as file: cubin_image = file.read() # Set up the host-side library code if requires_nvcc_hostlib_compilation: cuda_install_path = os.getenv('CUDA_INSTALL_PATH') assert cuda_install_path is not None, "Environment variable 'CUDA_INSTALL_PATH' is not defined." cmd_template = "echo '%s'|${cuda_install_path}/bin/nvcc -x cu -Xcompiler=\"-fpermissive -w -fPIC\" ${options}" % source_buffer_host cmd = SubstituteTemplate( cmd_template, { "cuda_install_path": cuda_install_path, "options": compilation_options.get_str() }) else: options = compilation_options.get() cmd = "echo '%s'|g++ -x c++ -fpermissive -w -fPIC" % source_buffer_host filtered_opts = ['-default-device', '-Xcicc', '-Xllc', '--expt-relaxed-constexpr', '-Xcudafe --diag_suppress=esa_on_defaulted_function_ignored'] for opt in options: opt = opt.decode("utf-8") if opt not in filtered_opts and '-arch=sm_' not in opt: if '--include-path=' in opt: cmd += " " + opt.replace('--include-path=', '-I') else: cmd += " " + opt tempfile.tempdir = "./" temp = tempfile.NamedTemporaryFile( prefix='host_func', suffix='.so', delete=True) cmd += ' - -shared -o %s -lcudart -lcuda' % temp.name os.system(cmd) host_lib = ctypes.CDLL(temp.name) return cubin_image, host_lib, temp def add_module(self, operations, compile_options=None): """ Insert a new compiled device module """ if compile_options is None: cutlass_path = os.getenv('CUTLASS_PATH') assert cutlass_path is not None, "Environment variable 'CUTLASS_PATH' is not defined." cuda_install_path = os.getenv('CUDA_INSTALL_PATH') assert cuda_install_path is not None, "Environment variable 'CUDA_INSTALL_PATH' is not defined." include_paths = [ cuda_install_path + '/include', cutlass_path + '/include', cutlass_path + '/tools/util/include', cutlass_path + '/tools/library/scripts/pycutlass/src/cpp/include' ] if pycutlass.DEVICE_CC is not None: arch = pycutlass.DEVICE_CC else: # Find the maximum arch tag among the provided operations and compile for that target. # Since we are compiling to .cubin files, only one architecture may be specified. arch = max([op.arch for op in operations]) compile_options = CompilationOptions( self.default_compile_options, arch, include_paths) # save the cubin operation_key = [] operation_list = [] requires_nvcc_hostlib_compilation = False for operation in operations: # step 1: get kernel string as key key = operation.rt_module.emit() + operation.procedural_name() + self.backend # step 1: check if the operation is in cache compiled_kernel = self.compiled_cache_device.at(key) if compiled_kernel is None: hit = self.load_operation(key, getattr(operation.rt_module, 'extra_funcs', {})) if hit: compiled_kernel = self.compiled_cache_device.at(key) assert compiled_kernel is not None if compiled_kernel is not None: operation.rt_module.kernel = compiled_kernel compiled_host_fns = self.compiled_cache_host.at(key) assert compiled_host_fns is not None for key in compiled_host_fns.keys(): setattr(operation.rt_module, key, compiled_host_fns[key]) operation.rt_module.initialize() else: operation_list.append(operation.rt_module) operation_key.append(key) # Creating the Params structures for certain 3.0 kernels currently requires CUDA. For these cases, use NVCC to generate # the PyCUTLASS host-side library. Otherwise, g++ will be used. if isinstance(operation, pycutlass.gemm_operation.GemmOperationUniversal) and operation.api == pycutlass.library.ApiVersion.v3x: if self.backend == "nvrtc": raise RuntimeError('CUTLASS 3 kernels currently require NVCC for compilation.') requires_nvcc_hostlib_compilation = True if len(operation_list) > 0: cubin_image, host_lib, host_file = self.emit_compile_( operation_list, compile_options, requires_nvcc_hostlib_compilation) err, module = cuda.cuModuleLoadData(cubin_image) if err != cuda.CUresult.CUDA_SUCCESS: raise RuntimeError('Cuda Error: {}'.format(err)) operation_name = [] operation_attr = [] for operation, key in zip(operation_list, operation_key): # get device kernels err, operation.kernel = cuda.cuModuleGetFunction( module, bytes(str.encode(operation.name())) ) operation_name.append(operation.name()) self.compiled_cache_device.insert(key, operation.kernel) # get host functions compiled_host_fns = {} op_attr = [] # get param size func_name = operation.name() + '_get_param_size' func = getattr(host_lib, func_name) param_size = func() func_name = operation.name() + '_get_params' func = getattr(host_lib, func_name) func.argtype = operation.argtype func.restype = ctypes.POINTER(ctypes.c_char * param_size) setattr(operation, 'get_args', func) compiled_host_fns['get_args'] = func # set shared memory size func_name = operation.name() + '_shared_memory_size' func = getattr(host_lib, func_name) setattr(operation, 'shared_memory_capacity', func()) compiled_host_fns['shared_memory_capacity'] = func() # set the maximum dynamic shared size operation.initialize() # get extra functions op_attr.append(param_size) if hasattr(operation, "extra_funcs"): for suffix, ret_type in operation.extra_funcs.items(): func_name = operation.name() + '_' + suffix func = getattr(host_lib, func_name) if ret_type is not None: func.restype = ret_type setattr(operation, suffix, func) compiled_host_fns[suffix] = func op_attr.append(suffix) operation_attr.append(op_attr) self.compiled_cache_host.insert(key, compiled_host_fns) for key, operation_name, operation_attr in zip(operation_key, operation_name, operation_attr): self.insert_operation( key, cubin_image, host_file.name, operation_name, operation_attr)
################################################################################ # # Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: BSD-3-Clause # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # 3. Neither the name of the copyright holder nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ################################################################################ import enum import copy import numpy as np from typeguard import typechecked import cutlass from pycutlass import * import pycutlass.builder.collective_op_builder as collective_op_builder from cuda import cuda ################################################################################ # # Data structure modeling a GEMM operation # ################################################################################ def transpose_layout(layout: cutlass.layout): if layout == cutlass.ColumnMajor: return cutlass.RowMajor elif layout == cutlass.RowMajor: return cutlass.ColumnMajor else: raise ValueError("unsupported Layout {}".format(layout)) # @typechecked class GemmArguments2x(ArgumentBase): """ Argument wrapper for GEMM in CUTLASS 2. It encodes problem information and user-provide tensors into the kernel's argument :param operation: the GEMM operation to take the argument :type operation: :class:`pycutlass.GemmOperationUniversal` | :class:`pycutlass.GemmOperationGrouped` :param problem_size: GEMM problem size gemm(M, N, K) :type operation: :class:`cutlass.gemm.GemmCoord` :param A: tensor A :type A: cuda.CUdeviceptr | numpy.ndarray | torch.Tensor | cupy.ndarray :param B: tensor B :type B: cuda.CUdeviceptr | numpy.ndarray | torch.Tensor | cupy.ndarray :param C: tensor C :type C: cuda.CUdeviceptr | numpy.ndarray | torch.Tensor | cupy.ndarray :param D: tensor D :type D: cuda.CUdeviceptr | numpy.ndarray | torch.Tensor | cupy.ndarray :param gemm_mode: GEMM mode :type gemm_mode: :class:`cutlass.gemm.Mode` :param output_op: output operator, optional :type output_op: :class:`pycutlass.LinearCombinationFunctorArguments` """ def __init__( self, operation: 'GemmOperation', problem_size: 'cutlass.gemm.GemmCoord', A: 'Tensor', B: 'Tensor', C: 'Tensor', D: 'Tensor', gemm_mode: 'cutlass.gemm.Mode'=cutlass.gemm.Mode.Gemm, **kwargs): self.operation = operation self.layout_A: cutlass.layout = operation.A.layout self.layout_B: cutlass.layout = operation.B.layout self.layout_C: cutlass.layout = operation.C.layout self.element_A = operation.A.element self.element_B = operation.B.element self.element_C = operation.C.element if (operation.C.layout in [cutlass.RowMajorInterleaved32, cutlass.ColumnMajorInterleaved32]): # reorder tensor B for interleaved layout output B = self.reorder_tensor_B(B, problem_size) super().__init__(A, B, C, D, **kwargs) if operation.switched: self.problem_size = cutlass.gemm.GemmCoord( problem_size.n(), problem_size.m(), problem_size.k()) self.ptr_A, self.ptr_B = self.ptr_B, self.ptr_A else: self.problem_size = cutlass.gemm.GemmCoord( problem_size.m(), problem_size.n(), problem_size.k()) # if the number of elements in C = problem_size.n # C is treated as the bias if hasattr(self, "tensor_c_numel"): if (self.tensor_c_numel == self.problem_size.n() and self.problem_size.m() != 1): self.bias = True # get the leading dimension self.lda = operation.A.layout.packed(self.problem_size.mk()).stride() self.ldb = operation.B.layout.packed(self.problem_size.kn()).stride() self.ldc = operation.C.layout.packed(self.problem_size.mn()).stride() self.ldd = self.ldc # stride 0 trick if self.bias: self.ldc = 0 if 'output_op' in kwargs.keys() and \ gemm_mode != cutlass.gemm.Mode.GemmSplitKParallel: self.output_op = kwargs['output_op'] else: self.output_op = self.operation.epilogue_type(1.0, 0.0) # get number of slices on k dimension self.gemm_mode = gemm_mode if gemm_mode in [cutlass.gemm.Mode.Gemm, cutlass.gemm.Mode.GemmSplitKParallel]: if 'split_k_slices' in kwargs.keys(): self.batch_count = kwargs['split_k_slices'] else: self.batch_count = 1 self.split_k_slices = self.batch_count if gemm_mode in [cutlass.gemm.Mode.Batched, cutlass.gemm.Mode.Array]: if 'batch' in kwargs.keys(): self.batch_count = kwargs['batch'] else: self.batch_count = 1 self.batched_stride_A = self.problem_size.m() * self.problem_size.k() self.batched_stride_B = self.problem_size.n() * self.problem_size.k() self.batched_stride_C = self.problem_size.m() * self.problem_size.n() self.batched_stride_D = self.problem_size.m() * self.problem_size.n() if self.bias: self.batched_stride_C = self.problem_size.n() # support GEMM Mode Array if gemm_mode == cutlass.gemm.Mode.Array: self.ptr_A_array = [] self.ptr_B_array = [] self.ptr_C_array = [] self.ptr_D_array = [] ptr_A_addr = int(self.ptr_A) ptr_B_addr = int(self.ptr_B) ptr_C_addr = int(self.ptr_C) ptr_D_addr = int(self.ptr_D) stride_A = self.batched_stride_A * DataTypeSize[self.element_A] // 8 stride_B = self.batched_stride_B * DataTypeSize[self.element_B] // 8 stride_C = self.batched_stride_C * DataTypeSize[self.element_C] // 8 stride_D = self.batched_stride_D * DataTypeSize[self.element_C] // 8 for _ in range(self.batch_count): self.ptr_A_array.append(ptr_A_addr) self.ptr_B_array.append(ptr_B_addr) self.ptr_C_array.append(ptr_C_addr) self.ptr_D_array.append(ptr_D_addr) ptr_A_addr += stride_A ptr_B_addr += stride_B ptr_C_addr += stride_C ptr_D_addr += stride_D self.ptr_A_array_buffer = todevice(self.ptr_A_array, dtype=np.int64) self.ptr_B_array_buffer = todevice(self.ptr_B_array, dtype=np.int64) self.ptr_C_array_buffer = todevice(self.ptr_C_array, dtype=np.int64) self.ptr_D_array_buffer = todevice(self.ptr_D_array, dtype=np.int64) if isinstance(self.operation, GemmOperationUniversal): self.initialize() def reorder_tensor_B(self, tensor_B: 'np.ndarray', problem_size: 'cutlass.gemm.GemmCoord'): """ Reorder tensor_B for interleaved layout :param tensor_B: input tensor B :type tensor_B: numpy.ndarray :param problem_size: GEMM problem size :type problem_size: :class:`cutlass.gemm.GemmCoord` :return: reordered tensor B :rtype: numpy.ndarray """ reordered_tensor_B = np.empty_like(tensor_B) tensor_ref_B = self.get_tensor_ref( tensor_B, self.element_B, self.layout_B, problem_size, "b" ) reordered_tensor_ref_B = self.get_tensor_ref( reordered_tensor_B, self.element_B, self.layout_B, problem_size, "b" ) cutlass.gemm.host.reorder_column( tensor_ref_B, reordered_tensor_ref_B, problem_size) return reordered_tensor_B def get_tensor_ref( self, tensor, dtype, tensor_layout, problem_size, operand): if operand == "a": tensor_coord = problem_size.mk() elif operand == "b": tensor_coord = problem_size.kn() elif operand in ["c", "d"]: tensor_coord = problem_size.mn() else: raise ValueError("unknown operand: " + operand) layout = tensor_layout.packed(tensor_coord) return TensorRef(tensor, dtype, layout).tensor_ref def get_arguments(self): problem_size_ = GemmCoord_(self.problem_size) grid_tiled_shape_ = GemmCoord_( cutlass.gemm.GemmCoord( self.grid_tiled_shape.x, self.grid_tiled_shape.y, self.grid_tiled_shape.z ) ) if self.gemm_mode == cutlass.gemm.Mode.Array: arguments = self.operation.argument_type( # Arguments from UniversalArgumentsBase self.gemm_mode, problem_size_, self.batch_count, 0, # Remaining arguments self.output_op, int(self.ptr_A_array_buffer.ptr), int(self.ptr_B_array_buffer.ptr), int(self.ptr_C_array_buffer.ptr), int(self.ptr_D_array_buffer.ptr), 0, 0, 0, self.lda, self.ldb, self.ldc, self.ldd, self.lda, self.ldb, self.ldc, self.ldd, 0, 0, 0 ) else: arguments = self.operation.argument_type( # Arguments from UniversalArgumentsBase self.gemm_mode, problem_size_, self.batch_count, self.batched_stride_D, # Remaining arguments self.output_op, int(self.ptr_A), int(self.ptr_B), int(self.ptr_C), int(self.ptr_D), self.batched_stride_A, self.batched_stride_B, self.batched_stride_C, self.lda, self.ldb, self.ldc, self.ldd, self.lda, self.ldb, self.ldc, self.ldd, 0, 0, 0 ) self.arguments = arguments, grid_tiled_shape_, self.gemm_k_size def initialize(self): # get launch configuration launch_config = self.operation.rt_module.plan(self) # get the host and device workspace device_workspace_size = \ self.operation.rt_module.get_device_workspace_size(self) if device_workspace_size > 0: self.workspace_buffer = device_mem_alloc(device_workspace_size) workspace_ptr = self.workspace_buffer.ptr err, = cuda.cuMemsetD32( workspace_ptr, 0, device_workspace_size // 4) else: workspace_ptr = None device_workspace = 0 if (workspace_ptr is not None and self.gemm_mode == cutlass.gemm.Mode.GemmSplitKParallel): # in GEMM split-K parallel, the D pointer is redirected # to the workspace self.ptr_D = cuda.CUdeviceptr(workspace_ptr) elif (workspace_ptr is not None and self.gemm_mode == cutlass.gemm.Mode.Gemm): # in GEMM split-K serial device_workspace = workspace_ptr self.get_arguments() arguments, grid_tiled_shape, gemm_k_size = self.arguments res_arg = self.operation.rt_module.get_args( ctypes.byref(arguments), ctypes.c_void_p(int(device_workspace))) host_workspace = bytearray(res_arg.contents) device_workspace = None self.host_workspace = host_workspace self.device_workspace = device_workspace self.launch_config = launch_config class GemmArguments3x(GemmArguments2x): """ Argument wrapper for GEMM in CUTLASS 3. It encodes problem information and user-provide tensors into the kernel's argument :param operation: the GEMM operation to take the argument :type operation: :class:`pycutlass.GemmOperationUniversal` | :class:`pycutlass.GemmOperationGrouped` :param problem_size: GEMM problem size gemm(M, N, K) :type operation: :class:`cutlass.gemm.GemmCoord` :param A: tensor A :type A: cuda.CUdeviceptr | numpy.ndarray | torch.Tensor | cupy.ndarray :param B: tensor B :type B: cuda.CUdeviceptr | numpy.ndarray | torch.Tensor | cupy.ndarray :param C: tensor C :type C: cuda.CUdeviceptr | numpy.ndarray | torch.Tensor | cupy.ndarray :param D: tensor D :type D: cuda.CUdeviceptr | numpy.ndarray | torch.Tensor | cupy.ndarray :param gemm_mode: GEMM mode :type gemm_mode: :class:`cutlass.gemm.Mode` :param output_op: output operator, optional :type output_op: :class:`pycutlass.LinearCombinationFunctorArguments` """ def __init__( self, operation: 'GemmOperation', problem_size: 'cutlass.gemm.GemmCoord', A: 'Tensor', B: 'Tensor', C: 'Tensor', D: 'Tensor', gemm_mode: 'cutlass.gemm.Mode'=cutlass.gemm.Mode.Gemm, **kwargs): if gemm_mode not in [cutlass.gemm.Mode.Gemm, cutlass.gemm.Mode.Batched]: raise Exception("Unsupported GEMM mode {}.".format(gemm_mode)) super().__init__(operation, problem_size, A, B, C, D, gemm_mode, **kwargs) def get_arguments(self): problem_size_ = GemmCoordBatched_(self.problem_size, self.batch_count) if self.batch_count > 1: bsA = self.batched_stride_A bsB = self.batched_stride_B bsC = self.batched_stride_C bsD = self.batched_stride_D else: bsA = 0 bsB = 0 bsC = 0 bsD = 0 stride_A = StrideBatched_(self.lda, bsA) stride_B = StrideBatched_(self.ldb, bsB) stride_C = StrideBatched_(self.ldc, bsC) stride_D = StrideBatched_(self.ldd, bsD) self.arguments = self.operation.argument_type( self.gemm_mode, problem_size_, int(self.ptr_A), stride_A, int(self.ptr_B), stride_B, int(self.ptr_C), stride_C, int(self.ptr_D), stride_D, self.output_op, ) def initialize(self): # get the host and device workspace device_workspace_size = \ self.operation.rt_module.get_device_workspace_size(self) if device_workspace_size > 0: self.workspace_buffer = device_mem_alloc(device_workspace_size) workspace_ptr = self.workspace_buffer.ptr err, = cuda.cuMemsetD32( workspace_ptr, 0, device_workspace_size // 4) else: workspace_ptr = None device_workspace = 0 if (workspace_ptr is not None and self.gemm_mode == cutlass.gemm.Mode.GemmSplitKParallel): # in GEMM split-K parallel, the D pointer is redirected # to the workspace self.ptr_D = cuda.CUdeviceptr(workspace_ptr) elif (workspace_ptr is not None and self.gemm_mode == cutlass.gemm.Mode.Gemm): # in GEMM split-K serial device_workspace = workspace_ptr self.get_arguments() res_arg = self.operation.rt_module.get_args( ctypes.byref(self.arguments), ctypes.c_void_p(int(device_workspace))) host_workspace = bytearray(res_arg.contents) grid = self.operation.rt_module.get_grid_shape( ctypes.byref(self.arguments), ctypes.c_void_p(int(device_workspace))) block = self.operation.rt_module.get_block_shape() device_workspace = None self.host_workspace = host_workspace self.device_workspace = device_workspace self.launch_config = LaunchConfiguration([grid.x, grid.y, grid.z], [block.x, block.y, block.z], self.operation.rt_module.shared_memory_capacity) def GemmArguments(operation: 'GemmOperation', problem_size: 'cutlass.gemm.GemmCoord', A: 'Tensor', B: 'Tensor', C: 'Tensor', D: 'Tensor', gemm_mode: 'cutlass.gemm.Mode'=cutlass.gemm.Mode.Gemm, **kwargs): """ Argument wrapper for GEMM in CUTLASS 2 or 3. It returns either 2x arguments or 3x arguments depending on the `arch` field specified in `operation`. :param operation: the GEMM operation to take the argument :type operation: :class:`pycutlass.GemmOperationUniversal` | :class:`pycutlass.GemmOperationGrouped` :param problem_size: GEMM problem size gemm(M, N, K) :type operation: :class:`cutlass.gemm.GemmCoord` :param A: tensor A :type A: cuda.CUdeviceptr | numpy.ndarray | torch.Tensor | cupy.ndarray :param B: tensor B :type B: cuda.CUdeviceptr | numpy.ndarray | torch.Tensor | cupy.ndarray :param C: tensor C :type C: cuda.CUdeviceptr | numpy.ndarray | torch.Tensor | cupy.ndarray :param D: tensor D :type D: cuda.CUdeviceptr | numpy.ndarray | torch.Tensor | cupy.ndarray :param gemm_mode: GEMM mode :type gemm_mode: :class:`cutlass.gemm.Mode` :param output_op: output operator, optional :type output_op: :class:`pycutlass.LinearCombinationFunctorArguments` """ ArgClass = GemmArguments3x if operation.api == ApiVersion.v3x else GemmArguments2x return ArgClass(operation, problem_size, A, B, C, D, gemm_mode, **kwargs) class GemmGroupedArguments: """ Argument wrapper for GEMM Grouped. It encodes problem information and user-provide tensors into the kernel's argument :param operation: the GEMM Grouped operation to take the argument :type operation: :class:`pycutlass.GemmOperationGrouped` :param problem_size: list of GEMM problem size gemm(M, N, K) :type operation: list[:class:`cutlass.gemm.GemmCoord`] :param A: list of tensor A :type A: list[cuda.CUdeviceptr | numpy.ndarray | torch.Tensor | cupy.ndarray] :param B: list of tensor B :type B: list[cuda.CUdeviceptr | numpy.ndarray | torch.Tensor | cupy.ndarray] :param C: list of tensor C :type C: list[cuda.CUdeviceptr | numpy.ndarray | torch.Tensor | cupy.ndarray] :param D: list of tensor D :type D: list[cuda.CUdeviceptr | numpy.ndarray | torch.Tensor | cupy.ndarray] :param output_op: output operator, optional :type output_op: :class:`pycutlass.LinearCombinationFunctorArguments` """ def __init__( self, operation: 'GemmOperationGrouped', problem_sizes: 'list[cutlass.gemm.GemmCoord]', A: 'list[Tensor]', B: 'list[Tensor]', C: 'list[torch.Tensor]', D: 'list[Tensor]', **kwargs): # get number of problems in the group self.problem_count = len(problem_sizes) # check the input arguments assert len(A) == self.problem_count assert len(B) == self.problem_count assert len(C) == self.problem_count assert len(D) == self.problem_count problem_size_host = [] self.ptr_A_host = [] self.ptr_B_host = [] self.ptr_C_host = [] self.ptr_D_host = [] lda_host = [] ldb_host = [] ldc_host = [] ldd_host = [] self.partitions = 1 self.operation = operation # get the threadblock threadblock_shape = operation.tile_description.threadblock_shape self.threadblock_shape = cutlass.gemm.GemmCoord( threadblock_shape[0], threadblock_shape[1], threadblock_shape[2]) self.threadblock_swizzle = operation.swizzling_functor self.total_tiles = 0 self.gemm_arguments = [] # process the input arguments for idx, problem_size in enumerate(problem_sizes): M, N, K = problem_size.m(), problem_size.n(), problem_size.k() temp_argument = GemmArguments2x( operation=operation, problem_size=cutlass.gemm.GemmCoord(M, N, K), A=A[idx], B=B[idx], C=C[idx], D=D[idx], ) self.gemm_arguments.append(temp_argument) problem_size_host.append( [temp_argument.problem_size.m(), temp_argument.problem_size.n(), temp_argument.problem_size.k()] ) self.ptr_A_host.append(int(temp_argument.ptr_A)) lda_host.append(temp_argument.lda) self.ptr_B_host.append(int(temp_argument.ptr_B)) ldb_host.append(temp_argument.ldb) self.ptr_C_host.append(int(temp_argument.ptr_C)) ldc_host.append(temp_argument.ldc) self.ptr_D_host.append(int(temp_argument.ptr_D)) ldd_host.append(temp_argument.ldd) # get number of tiles grid = self.threadblock_swizzle.get_grid_shape( self.threadblock_swizzle.get_tiled_shape( temp_argument.problem_size, self.threadblock_shape, temp_argument.batch_count) ) self.total_tiles += grid.x * grid.y * grid.z self.problem_size_buffer = todevice(problem_size_host, np.int32) self.ptr_A_buffer = todevice(self.ptr_A_host, np.int64) self.ptr_B_buffer = todevice(self.ptr_B_host, np.int64) self.ptr_C_buffer = todevice(self.ptr_C_host, np.int64) self.ptr_D_buffer = todevice(self.ptr_D_host, np.int64) self.lda_buffer = todevice(lda_host, np.int64) self.ldb_buffer = todevice(ldb_host, np.int64) self.ldc_buffer = todevice(ldc_host, np.int64) self.ldd_buffer = todevice(ldd_host, np.int64) if 'output_op' in kwargs.keys(): self.alpha = kwargs['output_op'].alpha self.beta = kwargs['output_op'].beta else: self.alpha = 1.0 self.beta = 0.0 if 'output_op' in kwargs.keys(): self.output_op = kwargs['output_op'] else: self.output_op = self.operation.epilogue_type(1.0, 0.0) # get host problem size self.host_problem_size_ptr = np.array( problem_size_host, dtype=np.int32).__array_interface__['data'][0] self.arguments = self.get_arguments() self.initialize() def get_arguments(self): return self.operation.argument_type( self.problem_size_buffer.ptr, self.problem_count, self.total_tiles, self.output_op, self.ptr_A_buffer.ptr, self.ptr_B_buffer.ptr, self.ptr_C_buffer.ptr, self.ptr_D_buffer.ptr, self.lda_buffer.ptr, self.ldb_buffer.ptr, self.ldc_buffer.ptr, self.ldd_buffer.ptr, ctypes.c_void_p(int(self.host_problem_size_ptr)) ) def initialize(self): # get launch configuration launch_config = self.operation.rt_module.plan(self) # get the host and device workspace device_workspace_size = \ self.operation.rt_module.get_device_workspace_size(self) if device_workspace_size > 0: self.workspace_buffer = device_mem_alloc(device_workspace_size) workspace_ptr = self.workspace_buffer.ptr err, = cuda.cuMemsetD32( workspace_ptr, 0, device_workspace_size // 4) else: workspace_ptr = None if self.operation.precompute_mode == SchedulerMode.Host: device_workspace_ptr = self.operation.rt_module.host_precompute( self, self.operation.rt_module.get_workspace_size(self)) else: device_workspace_ptr = 0 result = self.operation.rt_module.get_args( ctypes.byref(self.arguments), self.total_tiles, ctypes.c_void_p(int(device_workspace_ptr)) ) host_workspace = bytearray(result.contents) device_workspace = None self.host_workspace = host_workspace self.device_workspace = device_workspace self.launch_config = launch_config def sync(self): err, = cudart.cudaDeviceSynchronize() if err != cuda.CUresult.CUDA_SUCCESS: raise RuntimeError("CUDA Error %s" % str(err)) for arg in self.gemm_arguments: arg.sync(stream_sync=False) ################################################################################ # Base class for GEMM runtime module ################################################################################ class GemmRTbase(ExecutableOperation): """ GemmRT manages the CUTLASS runtime components """ KernelTemplate = r''' extern "C" __global__ void ${operation_name}(${operation_name}${operation_suffix}::Params params) { // Dynamic shared memory base pointer extern __shared__ int SharedStorageBase[]; // Declare pointer to dynamic shared memory. ${operation_name}${operation_suffix}::SharedStorage *shared_storage = reinterpret_cast<${operation_name}${operation_suffix}::SharedStorage *>(SharedStorageBase); ${operation_name}${operation_suffix} op; op(params, *shared_storage); } ''' def __init__(self, operation: 'GemmOperation'): super().__init__(operation) self.operation = operation threadblock_shape = operation.tile_description.threadblock_shape self.threadblock_shape = cutlass.gemm.GemmCoord( threadblock_shape[0], threadblock_shape[1], threadblock_shape[2]) self.threadblock_swizzle = operation.swizzling_functor #: number of threads per threadblock self.threads: int = operation.tile_description.num_threads # def emit(self): return self.emitter.emit(self.operation) # def can_implement(self, configuration, arguments): raise NotImplementedError() # def get_host_workspace_size(self, arguments): raise NotImplementedError() # def get_device_workspace_size(self, arguments): return 0 # def initialize(self): err, = cuda.cuFuncSetAttribute( self.kernel, attrib=cuda.CUfunction_attribute.CU_FUNC_ATTRIBUTE_MAX_DYNAMIC_SHARED_SIZE_BYTES, value=self.shared_memory_capacity) if err != cuda.CUresult.CUDA_SUCCESS: raise RuntimeError('Cuda Error: {}'.format(err)) ################################################################################ # Runtime module for GEMM Universal ################################################################################ class GemmRTUniversal(GemmRTbase): """ GemmRTUniversal manages the CUTLASS runtime components """ HostTemplate = r''' extern "C" { // Get the size of params in bytes int ${operation_name}_get_param_size(){ return sizeof(${operation_name}${operation_suffix}::Params); } // Get the size of dynamic shared memory in bytes int ${operation_name}_shared_memory_size() { return int(sizeof(${operation_name}${operation_suffix}::SharedStorage)); } // Get the params as byte array char* ${operation_name}_get_params(${operation_name}_base::Arguments* argument, int* workspace){ ${operation_name}_base::Params* params; params = new ${operation_name}_base::Params(*argument, -1, // SM count. Only used for stream-K -1 // Occupancy. Only used for stream-K ); // Semaphore holds the pointer to the workspace in the Params struct params->semaphore = workspace; char *bytes = ((char*)(params)); char *output = new char[sizeof(${operation_name}_base::Params)]; for (unsigned int i = 0; i < sizeof(${operation_name}_base::Params); i ++) output[i] = bytes[i]; return output; } } ''' def __init__(self, operation: 'GemmOperation'): super(GemmRTUniversal, self).__init__(operation) self.emitter = EmitGemmUniversalInstance( '_type', operation.direct_store, operation.visitor) self.argument_type, self.epilogue_type = get_gemm_arguments(operation.epilogue_functor) self.argtype = [ ctypes.POINTER(self.argument_type), ctypes.POINTER(GemmCoord_), ctypes.c_int, ctypes.c_void_p ] def plan(self, arguments): grid = self.threadblock_swizzle.get_tiled_shape( arguments.problem_size, self.threadblock_shape, arguments.batch_count ) gemm_k_size = arguments.problem_size.k() if (arguments.gemm_mode in [cutlass.gemm.Mode.Gemm, cutlass.gemm.Mode.GemmSplitKParallel]): # alignk = max(max(128 // DataTypeSize[self.operation.A.element], 128 // DataTypeSize[self.operation.B.element]), 1) gemm_k_size = (((arguments.problem_size.k() + arguments.batch_count - 1) // arguments.batch_count + alignk - 1) // alignk) * alignk if gemm_k_size: grid_z = (arguments.problem_size.k() + gemm_k_size - 1) // gemm_k_size grid = cutlass.gemm.GemmCoord(grid.m(), grid.n(), grid_z) arguments.grid_tiled_shape = cutlass.dim3(grid.m(), grid.n(), grid.k()) grid = self.threadblock_swizzle.get_grid_shape(grid) arguments.gemm_k_size = gemm_k_size return LaunchConfiguration( [grid.x, grid.y, grid.z], [self.threads, 1, 1], self.shared_memory_capacity) # def get_device_workspace_size(self, arguments: GemmArguments): workspace_bytes = 0 if arguments.gemm_mode == cutlass.gemm.Mode.GemmSplitKParallel: workspace_bytes = (DataTypeSize[arguments.operation.C.element] * arguments.batched_stride_D * arguments.grid_tiled_shape.z // 8) elif (arguments.gemm_mode == cutlass.gemm.Mode.Gemm and arguments.split_k_slices > 1): # workspace_bytes = 4 * arguments.grid_tiled_shape.x * arguments.grid_tiled_shape.y return workspace_bytes ################################################################################ # Runtime module for GEMM Universal within CUTLASS 3 ################################################################################ class GemmRTUniversal3x(GemmRTUniversal): """ GemmRTUniversal manages the CUTLASS runtime components """ KernelTemplate = r''' using Operator = ${operation_name}${operation_suffix}; extern "C" __global__ __launch_bounds__(Operator::MaxThreadsPerBlock, Operator::MinBlocksPerMultiprocessor) void ${operation_name}(__grid_constant__ typename Operator::Params const params) { // Dynamic shared memory base pointer extern __shared__ char smem[]; // Declare pointer to dynamic shared memory. Operator op; op(params, smem); } ''' HostTemplate = r''' extern "C" { // Get the size of params in bytes int ${operation_name}_get_param_size(){ return sizeof(${operation_name}${operation_suffix}::Params); } // Get the size of dynamic shared memory in bytes int ${operation_name}_shared_memory_size() { return ${operation_name}${operation_suffix}::SharedStorageSize; } using GemmType = ${operation_name}_base; // Get the params as byte array char* ${operation_name}_get_params(GemmType::Arguments* argument, int* workspace){ GemmType::Params params = GemmType::to_underlying_arguments(*argument, workspace); char *bytes = ((char*)(&params)); char *output = new char[sizeof(GemmType::Params)]; for (unsigned int i = 0; i < sizeof(GemmType::Params); i ++) output[i] = bytes[i]; return output; } // Get the grid shape dim3 ${operation_name}_get_grid_shape(GemmType::Arguments* args, int* workspace) { auto tmp_params = GemmType::to_underlying_arguments(*args, workspace); return GemmType::get_grid_shape(tmp_params); } // Get the block shape dim3 ${operation_name}_get_block_shape() { return GemmType::get_block_shape(); } } ''' def __init__(self, operation: 'GemmOperation'): super(GemmRTUniversal3x, self).__init__(operation) self.extra_funcs = { 'get_grid_shape': dim3_, 'get_block_shape': dim3_ } self.emitter = EmitGemmUniversalInstance3x('_type') self.argument_type, self.epilogue_type = get_gemm_arguments_3x(operation.epilogue_functor) class EmitGemmUniversalInstance3x: ''' Responsible for emitting a CUTLASS 3 template definition''' def __init__(self, operation_suffix=''): self.operation_suffix = operation_suffix self.includes = [ "cutlass/cutlass.h", "cute/tensor.hpp", "cute/atom/mma_atom.hpp", "cutlass/numeric_types.h", "cutlass/gemm/kernel/gemm_universal.hpp", "cutlass/gemm/collective/collective_builder.hpp", "cutlass/epilogue/collective/default_epilogue.hpp", "cutlass/epilogue/thread/linear_combination.h" ] self.gemm_template = """ using namespace cute; ${collective_op} using EpilogueOp = cutlass::epilogue::collective::DefaultEpilogue< cutlass::gemm::TagToStrideC_t<${layout_c}>, cutlass::gemm::TagToStrideC_t<${layout_c}>, ${epilogue_functor} >; // Gemm operator ${operation_name} using ${operation_name}_base = cutlass::gemm::kernel::GemmUniversal< Shape<int,int,int,int>, CollectiveOp, EpilogueOp >; // Define named type struct ${operation_name}${operation_suffix} : public ${operation_name}_base { }; """ # def emit(self, operation): instance_layout_A, instance_layout_B, instance_layout_C = \ (operation.A.layout, operation.B.layout, operation.C.layout) # Support built-in epilogue functors or user-defined functions epilogue_functor = operation.epilogue_functor.emit() collective_op = collective_op_builder.build(operation) values = { 'operation_name': operation.procedural_name(), 'operation_suffix': self.operation_suffix, 'collective_op': collective_op, 'element_a': DataTypeTag[operation.A.element], 'layout_a': LayoutTag[instance_layout_A], 'element_b': DataTypeTag[operation.B.element], 'layout_b': LayoutTag[instance_layout_B], 'element_c': DataTypeTag[operation.C.element], 'layout_c': LayoutTag[instance_layout_C], 'epilogue_functor': epilogue_functor, 'element_output': DataTypeTag[operation.epilogue_functor.element_output], 'element_accumulator': DataTypeTag[operation.accumulator_type()], 'element_epilogue': DataTypeTag[operation.epilogue_functor.element_epilogue], 'epilogue_vector_length': str(operation.epilogue_functor.epilogue_vector_length), 'opcode_class': OpcodeClassTag[operation.tile_description.math_instruction.opcode_class], 'arch': "cutlass::arch::Sm%d" % operation.arch, 'threadblock_shape_m': str(operation.tile_description.threadblock_shape[0]), 'threadblock_shape_n': str(operation.tile_description.threadblock_shape[1]), 'threadblock_shape_k': str(operation.tile_description.threadblock_shape[2]), 'cluster_shape_m': str(operation.tile_description.cluster_shape[0]), 'cluster_shape_n': str(operation.tile_description.cluster_shape[1]), 'cluster_shape_k': str(operation.tile_description.cluster_shape[2]), 'align_a': str(operation.A.alignment), 'align_b': str(operation.B.alignment) } values['epilogue_functor'] = operation.epilogue_functor.emit() return SubstituteTemplate(self.gemm_template, values) ################################################################################################### # Runtime module for GEMM Grouped ################################################################################################### class GemmRTGrouped(GemmRTbase): """ GemmRTGrouped manages the CUTLASS runtime components """ HostTemplate = r''' extern "C" { // precompute scheduling information char * ${operation_name}_precompute(${operation_name}_base::Arguments const &args, int tile_count, size_t workspace_bytes) { char* host_workspace = new char[workspace_bytes]; ${operation_name}_base::ProblemVisitor::host_precompute( args.host_problem_sizes, args.problem_count, args.threadblock_count, (void*)host_workspace ); return host_workspace; } // Get the size of params in bytes int ${operation_name}_get_param_size(){ return sizeof(${operation_name}${operation_suffix}::Params); } // Get the size of dynamic shared memory in bytes int ${operation_name}_shared_memory_size() { return int(sizeof(${operation_name}${operation_suffix}::SharedStorage)); } // Get the params as byte array char* ${operation_name}_get_params(${operation_name}_base::Arguments* argument, int tile_count, void* workspace=nullptr){ ${operation_name}_base::Params* params; params = new ${operation_name}_base::Params(*argument, workspace, tile_count); char *bytes = ((char*)(params)); char *output = new char[sizeof(${operation_name}_base::Params)]; for (unsigned int i = 0; i < sizeof(${operation_name}_base::Params); i ++) output[i] = bytes[i]; return output; } } ''' def __init__(self, operation: 'GemmOperation'): super(GemmRTGrouped, self).__init__(operation) self.extra_funcs = {'precompute': None} self.emitter = EmitGemmGroupedInstance('_type') self.argument_type, self.epilogue_type = get_gemm_grouped_arguments(operation.epilogue_functor) self.argtype = [ctypes.POINTER(self.argument_type), ctypes.c_int, ctypes.c_void_p] def host_precompute(self, arguments, workspace_bytes): self.precompute.argtype = [ self.argtype[0], ctypes.c_int, ctypes.c_longlong] self.precompute.restype = ctypes.POINTER( ctypes.c_byte * workspace_bytes) problem_info = self.precompute(ctypes.byref( arguments.arguments), arguments.total_tiles, workspace_bytes) problem_info_array = bytearray(problem_info.contents) # copy to device memory return rmm.DeviceBuffer.to_device(problem_info_array).ptr def plan(self, arguments): return LaunchConfiguration( [arguments.total_tiles, 1, 1], [self.threads, 1, 1], self.shared_memory_capacity) def get_workspace_size(self, arguments): if self.operation.precompute_mode == SchedulerMode.Device: return 0 elif self.operation.precompute_mode == SchedulerMode.Host: total_tiles = arguments.total_tiles entries_per_block = 1 return 8 * entries_per_block * total_tiles # three int32_t ################################################################################ # Runtime module for GEMM Grouped ################################################################################ # class GemmOperationBase: """ CUTLASS GEMM operation """ # def __init__( self, gemm_kind, arch, tile_description: TileDescription, A: TensorDescription, B: TensorDescription, C: TensorDescription, epilogue_functor, swizzling_functor=cutlass.IdentitySwizzle1, api=False, **kwargs): #: operation kind self.operation_kind: OperationKind = OperationKind.Gemm #: compute capability self.arch: int = arch #: tile description object self.tile_description: TileDescription = tile_description #: gemm kind self.gemm_kind: GemmKind = gemm_kind self.api = api self.prefix = "3x" if self.api == ApiVersion.v3x else "" # use deep copy to avoid overwriting the original TensorDescription if self.api != ApiVersion.v3x and C.layout == cutlass.ColumnMajor: #: Operand A self.A: TensorDescription = copy.deepcopy(B) #: Operand B self.B: TensorDescription = copy.deepcopy(A) #: Operand C self.C: TensorDescription = copy.deepcopy(C) self.A.layout = transpose_layout(self.A.layout) self.B.layout = transpose_layout(self.B.layout) self.C.layout = transpose_layout(self.C.layout) self.switched = True else: #: Operand A self.A: TensorDescription = copy.deepcopy(A) #: Operand B self.B: TensorDescription = copy.deepcopy(B) #: Operand C self.C: TensorDescription = copy.deepcopy(C) self.switched = False self.epilogue_functor = epilogue_functor self.swizzling_functor = swizzling_functor() if "direct_store" in kwargs: self.direct_store = kwargs["direct_store"] else: self.direct_store = False if "visitor" in kwargs: self.visitor = kwargs["visitor"] else: self.visitor = False def run(self, arguments: GemmArguments) -> cuda.CUresult: """ Configure and launch the cuda kernel with input arguments """ err = self.rt_module.run( arguments.host_workspace, arguments.device_workspace, arguments.launch_config) if err != cuda.CUresult.CUDA_SUCCESS: raise RuntimeError('CUDA Error %s' % str(err)) return err def free(self): if hasattr(self, "workspace_buffer"): del self.workspace_buffer # def is_complex(self): complex_operators = [ MathOperation.multiply_add_complex, MathOperation.multiply_add_complex_gaussian, MathOperation.multiply_add_complex_fast_f32 ] return self.tile_description.math_instruction.math_operation in complex_operators # def is_planar_complex(self): return self.gemm_kind in (GemmKind.PlanarComplex, GemmKind.PlanarComplexArray) # def accumulator_type(self): accum = self.tile_description.math_instruction.element_accumulator if self.is_complex(): return get_complex_from_real(accum) return accum # def short_math_name(self): if self.tile_description.math_instruction.math_operation == MathOperation.multiply_add_complex_gaussian: return "g%s" % ShortDataTypeNames[self.accumulator_type()] return ShortDataTypeNames[self.accumulator_type()] # def core_name(self): ''' The basic operation kind is prefixed with a letter indicating the accumulation type. ''' inst_shape = '' inst_operation = '' intermediate_type = '' math_operations_map = { MathOperation.xor_popc: 'xor', } if self.tile_description.math_instruction.opcode_class == cutlass.OpClass.TensorOp or \ self.tile_description.math_instruction.opcode_class == cutlass.OpClass.WmmaTensorOp: math_op = self.tile_description.math_instruction.math_operation math_op_string = math_operations_map[math_op] if math_op in math_operations_map.keys( ) else '' if self.tile_description.math_instruction.instruction_shape is not None: inst_shape = "%dx%dx%d" % tuple( self.tile_description.math_instruction.instruction_shape) else: inst_shape = "Default" inst_shape += math_op_string if self.tile_description.math_instruction.element_a != self.A.element and \ self.tile_description.math_instruction.element_a != self.tile_description.math_instruction.element_accumulator: intermediate_type = DataTypeNames[self.tile_description.math_instruction.element_a] return "%s%s%s%s" % (self.short_math_name(), inst_shape, intermediate_type, GemmKindNames[self.gemm_kind]) # def extended_name(self): ''' Append data types if they differ from compute type. ''' if self.is_complex(): extended_name = "${core_name}" else: if self.C.element != self.tile_description.math_instruction.element_accumulator and \ self.A.element != self.tile_description.math_instruction.element_accumulator: extended_name = "${element_c}_${core_name}_${element_a}" elif self.C.element == self.tile_description.math_instruction.element_accumulator and \ self.A.element != self.tile_description.math_instruction.element_accumulator: extended_name = "${core_name}_${element_a}" else: extended_name = "${core_name}" extended_name = SubstituteTemplate(extended_name, { 'element_a': DataTypeNames[self.A.element], 'element_c': DataTypeNames[self.C.element], 'core_name': self.core_name() }) return extended_name # def extended_name_3x(self): '''Generates a string representing the MMA atom. Assumes accumulator type is C type.''' extended_name = "{core_name}_{element_a}_{element_b}_{element_acc}_{element_c}".format( element_a = DataTypeNames[self.A.element], element_b = DataTypeNames[self.B.element], element_acc = DataTypeNames[self.tile_description.math_instruction.element_accumulator], element_c = DataTypeNames[self.C.element], core_name = self.core_name()) return extended_name # def layout_name(self): if self.is_complex() or self.is_planar_complex(): return "%s%s" % ( ShortComplexLayoutNames[( self.A.layout, self.A.complex_transform)], ShortComplexLayoutNames[( self.B.layout, self.B.complex_transform)] ) return "%s%s" % (ShortLayoutTypeNames[self.A.layout], ShortLayoutTypeNames[self.B.layout]) # Generates a short string representing the ABC layout tags (e.g. ntn or tnn) def layout_name_3x(self): if self.is_complex() or self.is_planar_complex(): return "{}{}{}".format( ShortComplexLayoutNames[(self.A.layout, self.A.complex_transform)], ShortComplexLayoutNames[(self.B.layout, self.B.complex_transform)], ShortComplexLayoutNames[(self.C.layout, self.C.complex_transform)]) else: return "{}{}{}".format( ShortLayoutTypeNames[self.A.layout], ShortLayoutTypeNames[self.B.layout], ShortLayoutTypeNames[self.C.layout]) # def procedural_name(self): ''' The full procedural name indicates architecture, extended name, tile size, and layout. ''' opcode_class_name = OpcodeClassNames[self.tile_description.math_instruction.opcode_class] if self.api == ApiVersion.v3x and self.arch >= 90: kernel_name_template = "cutlass{p}_sm{ar}_{op}_{ex}_{tbm}x{tbn}x{tbk}_{cm}x{cn}x{ck}_{l}_{s}_align{al}" return kernel_name_template.format( p = self.prefix, ar = self.arch, op = opcode_class_name, ex = self.extended_name_3x(), tbm = self.tile_description.threadblock_shape[0], tbn = self.tile_description.threadblock_shape[1], tbk = self.tile_description.threadblock_shape[2], cm = self.tile_description.cluster_shape[0], cn = self.tile_description.cluster_shape[1], ck = self.tile_description.cluster_shape[2], l = self.tile_description.stages, s = self.layout_name_3x(), al = str(self.A.alignment)) else: threadblock = self.tile_description.procedural_name() return "cutlass{p}_sm{ar}_{op}_{ex}_{tb}_{l}_align{a}".format( p = self.prefix, ar = self.arch, op = opcode_class_name, ex = self.extended_name(), tb = threadblock, l = self.layout_name(), a = str(self.A.alignment)) # def configuration_name(self): ''' The full procedural name indicates architecture, extended name, tile size, and layout. ''' return self.procedural_name() class GemmOperationUniversal(GemmOperationBase): def __init__(self, arch, tile_description: TileDescription, A: TensorDescription, B, C, epilogue_functor, swizzling_functor=cutlass.IdentitySwizzle1, **kwargs): api = api_version(arch, tile_description.math_instruction.opcode_class, A.element) super(GemmOperationUniversal, self).__init__(GemmKind.Universal, arch, tile_description, A, B, C, epilogue_functor, swizzling_functor, api=api, **kwargs) if api == ApiVersion.v3x: self.rt_module = GemmRTUniversal3x(self) else: self.rt_module = GemmRTUniversal(self) self.argument_type = self.rt_module.argument_type self.epilogue_type = self.rt_module.epilogue_type class GemmOperationGrouped(GemmOperationBase): def __init__(self, arch, tile_description: TileDescription, A: TensorDescription, B, C, epilogue_functor, swizzling_functor=cutlass.IdentitySwizzle1, **kwargs): super(GemmOperationGrouped, self).__init__(GemmKind.Grouped, arch, tile_description, A, B, C, epilogue_functor, swizzling_functor, **kwargs) assert "precompute_mode" in kwargs.keys( ), "missing keyword argument 'precompute_mode'." self.precompute_mode = kwargs["precompute_mode"] self.rt_module = GemmRTGrouped(self) self.argument_type = self.rt_module.argument_type self.epilogue_type = self.rt_module.epilogue_type ################################################################################################### # # Emits single instances of a CUTLASS device-wide operator # ################################################################################################### # class EmitGemmUniversalInstance: ''' Responsible for emitting a CUTLASS template definition''' def __init__(self, operation_suffix='', direct_store=False, visitor=False): self.operation_suffix = operation_suffix self.direct_store = direct_store self.visitor = visitor self.includes = [ "cutlass/cutlass.h", "cutlass/numeric_types.h", "cutlass/arch/arch.h", "cutlass/arch/mma.h", "cutlass/layout/matrix.h", "cutlass/gemm/device/gemm.h", "cutlass/gemm/device/gemm_universal_adapter.h", "cutlass/gemm/kernel/default_gemm_universal.h", ] if self.visitor: self.includes += [ "gemm/gemm_universal_with_visitor.h", "epilogue/epilogue_visitor_with_layernorm.h", "epilogue/epilogue_visitor_generic.h" ] if self.direct_store: self.includes.append( "cutlass/epilogue/threadblock/default_epilogue_direct_store.h") self.gemm_template_interleaved = """ // Gemm operator ${operation_name} using ${operation_name}_base = typename cutlass::gemm::kernel::DefaultGemmUniversal< ${element_a}, ${layout_a}, ${transform_a}, ${align_a}, ${element_b}, ${layout_b}, ${transform_b}, ${align_b}, ${element_c}, ${layout_c}, ${element_accumulator}, ${opcode_class}, ${arch}, cutlass::gemm::GemmShape<${threadblock_shape_m}, ${threadblock_shape_n}, ${threadblock_shape_k}>, cutlass::gemm::GemmShape<${warp_shape_m}, ${warp_shape_n}, ${warp_shape_k}>, cutlass::gemm::GemmShape<${instruction_shape_m}, ${instruction_shape_n}, ${instruction_shape_k}>, ${epilogue_functor}, ${swizzling_functor}, ${stages}, ${math_operation} >::GemmKernel; // Define named type struct ${operation_name}${operation_suffix} : public ${operation_name}_base { }; """ self.gemm_template_direct_store = """ // Gemm operator ${operation_name} using ${operation_name}_default = typename cutlass::gemm::kernel::DefaultGemmUniversal< ${element_a}, ${layout_a}, ${transform_a}, ${align_a}, ${element_b}, ${layout_b}, ${transform_b}, ${align_b}, ${element_c}, ${layout_c}, ${element_accumulator}, ${opcode_class}, ${arch}, cutlass::gemm::GemmShape<${threadblock_shape_m}, ${threadblock_shape_n}, ${threadblock_shape_k}>, cutlass::gemm::GemmShape<${warp_shape_m}, ${warp_shape_n}, ${warp_shape_k}>, cutlass::gemm::GemmShape<${instruction_shape_m}, ${instruction_shape_n}, ${instruction_shape_k}>, ${epilogue_functor}, ${swizzling_functor}, ${stages}, ${math_operation} >::GemmKernel; using ${operation_name}_base = cutlass::gemm::kernel::GemmUniversal< ${operation_name}_default::Mma, cutlass::epilogue::threadblock::DefaultEpilogueDirectStore< ${operation_name}_default::Epilogue >::Epilogue, ${operation_name}_default::ThreadblockSwizzle >; // Define named type struct ${operation_name}${operation_suffix} : public ${operation_name}_base { }; """ self.gemm_template_visitor = """ // Gemm operator ${operation_name} using ${operation_name}_default = typename cutlass::gemm::kernel::DefaultGemmUniversal< ${element_a}, ${layout_a}, ${transform_a}, ${align_a}, ${element_b}, ${layout_b}, ${transform_b}, ${align_b}, ${element_c}, ${layout_c}, ${element_accumulator}, ${opcode_class}, ${arch}, cutlass::gemm::GemmShape<${threadblock_shape_m}, ${threadblock_shape_n}, ${threadblock_shape_k}>, cutlass::gemm::GemmShape<${warp_shape_m}, ${warp_shape_n}, ${warp_shape_k}>, cutlass::gemm::GemmShape<${instruction_shape_m}, ${instruction_shape_n}, ${instruction_shape_k}>, ${elementwise_epilogue_functor}, ${swizzling_functor}, ${stages}, ${math_operation} >::GemmKernel; ${epilogue_visitor} using ${operation_name}_Epilogue = typename cutlass::epilogue::threadblock::EpilogueWithVisitorFromExistingEpilogue< ${operation_name}_EpilogueVisitor, typename ${operation_name}_default::Epilogue>::Epilogue; using ${operation_name}_base = cutlass::gemm::kernel::GemmUniversalwithEpilogueVisitor< ${operation_name}_default::Mma, ${operation_name}_Epilogue, ${operation_name}_default::ThreadblockSwizzle >; // Define named type struct ${operation_name}${operation_suffix} : public ${operation_name}_base { }; """ # def instance_template(self): return """ ${compile_guard_start} manifest.append(new ${gemm_kind}< cutlass::gemm::device::GemmUniversalAdapter<${operation_name}> >("${operation_name}")); ${compile_guard_end} """ # def emit(self, operation): threadblock_shape = operation.tile_description.threadblock_shape warp_count = operation.tile_description.warp_count warp_shape = [threadblock_shape[idx] // warp_count[idx] for idx in range(3)] # transpose_layouts = { # cutlass.layout.ColumnMajorcutlass.layout.ColumnMajor, # cutlass.layout.RowMajorcutlass.layout.RowMajor # } # if operation.A.layout in transpose_layouts.keys() and \ # operation.B.layout in transpose_layouts.keys() and \ # operation.C.layout in transpose_layouts.keys(): # instance_layout_A = transpose_layouts[operation.A.layout] # instance_layout_B = transpose_layouts[operation.B.layout] # instance_layout_C = transpose_layouts[operation.C.layout] # gemm_template = self.gemm_template # else: instance_layout_A, instance_layout_B, instance_layout_C = \ (operation.A.layout, operation.B.layout, operation.C.layout) if self.direct_store: gemm_template = self.gemm_template_direct_store elif self.visitor: gemm_template = self.gemm_template_visitor else: gemm_template = self.gemm_template_interleaved # values = { 'operation_name': operation.procedural_name(), 'operation_suffix': self.operation_suffix, 'element_a': DataTypeTag[operation.A.element], 'layout_a': LayoutTag[instance_layout_A], 'element_b': DataTypeTag[operation.B.element], 'layout_b': LayoutTag[instance_layout_B], 'element_c': DataTypeTag[operation.C.element], 'layout_c': LayoutTag[instance_layout_C], 'element_accumulator': DataTypeTag[operation.accumulator_type()], 'opcode_class': OpcodeClassTag[operation.tile_description.math_instruction.opcode_class], 'arch': "cutlass::arch::Sm%d" % operation.arch, 'threadblock_shape_m': str(operation.tile_description.threadblock_shape[0]), 'threadblock_shape_n': str(operation.tile_description.threadblock_shape[1]), 'threadblock_shape_k': str(operation.tile_description.threadblock_shape[2]), 'warp_shape_m': str(warp_shape[0]), 'warp_shape_n': str(warp_shape[1]), 'warp_shape_k': str(warp_shape[2]), 'instruction_shape_m': str(operation.tile_description.math_instruction.instruction_shape[0]), 'instruction_shape_n': str(operation.tile_description.math_instruction.instruction_shape[1]), 'instruction_shape_k': str(operation.tile_description.math_instruction.instruction_shape[2]), 'swizzling_functor': operation.swizzling_functor.tag(), 'stages': str(operation.tile_description.stages), 'align_a': str(operation.A.alignment), 'align_b': str(operation.B.alignment), 'transform_a': ComplexTransformTag[operation.A.complex_transform], 'transform_b': ComplexTransformTag[operation.B.complex_transform], 'math_operation': MathOperationTag[operation.tile_description.math_instruction.math_operation] } if self.visitor: values['epilogue_visitor'] = operation.epilogue_functor.emit(operation) values['elementwise_epilogue_functor'] = operation.epilogue_functor.elementwise_functor.emit() else: values['epilogue_functor'] = operation.epilogue_functor.emit() return SubstituteTemplate(gemm_template, values) ################################################################################################### # class EmitGemmGroupedInstance: ''' Responsible for emitting a CUTLASS template definition''' def __init__(self, operation_suffix=''): self.operation_suffix = operation_suffix self.includes = [ "cutlass/cutlass.h", "cutlass/numeric_types.h", "cutlass/arch/arch.h", "cutlass/arch/mma.h", "cutlass/layout/matrix.h", "cutlass/gemm/kernel/gemm_grouped.h", "cutlass/gemm/kernel/default_gemm_grouped.h" ] self.gemm_template = """ // Gemm operator ${operation_name} using ${operation_name}_base = typename cutlass::gemm::kernel::DefaultGemmGrouped< ${element_a}, ${layout_a}, ${transform_a}, ${align_a}, ${element_b}, ${layout_b}, ${transform_b}, ${align_b}, ${element_c}, ${layout_c}, ${element_accumulator}, ${opcode_class}, ${arch}, cutlass::gemm::GemmShape<${threadblock_shape_m}, ${threadblock_shape_n}, ${threadblock_shape_k}>, cutlass::gemm::GemmShape<${warp_shape_m}, ${warp_shape_n}, ${warp_shape_k}>, cutlass::gemm::GemmShape<${instruction_shape_m}, ${instruction_shape_n}, ${instruction_shape_k}>, ${epilogue_functor}, ${swizzling_functor}, ${stages}, ${precompute_mode}, ${math_operation} >::GemmKernel; // Define named type struct ${operation_name}${operation_suffix} : public ${operation_name}_base { }; """ # def instance_template(self): return """ ${compile_guard_start} manifest.append(new ${gemm_kind}< cutlass::gemm::device::GemmGrouped<${operation_name}> >("${operation_name}")); ${compile_guard_end} """ # def emit(self, operation): threadblock_shape = operation.tile_description.threadblock_shape warp_count = operation.tile_description.warp_count warp_shape = [threadblock_shape[idx] // warp_count[idx] for idx in range(3)] instance_layout_A, instance_layout_B, instance_layout_C = \ (operation.A.layout, operation.B.layout, operation.C.layout) # # Support built-in epilogue functors or user-defined functions epilogue_functor = operation.epilogue_functor.emit() values = { 'operation_name': operation.procedural_name(), 'operation_suffix': self.operation_suffix, 'element_a': DataTypeTag[operation.A.element], 'layout_a': LayoutTag[instance_layout_A], 'element_b': DataTypeTag[operation.B.element], 'layout_b': LayoutTag[instance_layout_B], 'element_c': DataTypeTag[operation.C.element], 'layout_c': LayoutTag[instance_layout_C], 'element_accumulator': DataTypeTag[operation.accumulator_type()], 'opcode_class': OpcodeClassTag[operation.tile_description.math_instruction.opcode_class], 'arch': "cutlass::arch::Sm%d" % operation.arch, 'threadblock_shape_m': str(operation.tile_description.threadblock_shape[0]), 'threadblock_shape_n': str(operation.tile_description.threadblock_shape[1]), 'threadblock_shape_k': str(operation.tile_description.threadblock_shape[2]), 'warp_shape_m': str(warp_shape[0]), 'warp_shape_n': str(warp_shape[1]), 'warp_shape_k': str(warp_shape[2]), 'instruction_shape_m': str(operation.tile_description.math_instruction.instruction_shape[0]), 'instruction_shape_n': str(operation.tile_description.math_instruction.instruction_shape[1]), 'instruction_shape_k': str(operation.tile_description.math_instruction.instruction_shape[2]), 'epilogue_functor': epilogue_functor, 'swizzling_functor': operation.swizzling_functor.tag(), 'stages': str(operation.tile_description.stages), 'align_a': str(operation.A.alignment), 'align_b': str(operation.B.alignment), 'transform_a': ComplexTransformTag[operation.A.complex_transform], 'transform_b': ComplexTransformTag[operation.B.complex_transform], 'precompute_mode': SchedulerModeTag[operation.precompute_mode], 'math_operation': MathOperationTag[operation.tile_description.math_instruction.math_operation] } return SubstituteTemplate(self.gemm_template, values)
################################################################################################# # # Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: BSD-3-Clause # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # 3. Neither the name of the copyright holder nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ################################################################################################# import re ################################################################################################### import enum import cutlass import cute # The following block implements enum.auto() for Python 3.5 variants that don't include it such # as the default 3.5.2 on Ubuntu 16.04. # # https://codereview.stackexchange.com/questions/177309/reimplementing-pythons-enum-auto-for-compatibility try: from enum import auto as enum_auto except ImportError: __cutlass_library_auto_enum = 0 def enum_auto() -> int: global __cutlass_library_auto_enum i = __cutlass_library_auto_enum __cutlass_library_auto_enum += 1 return i ################################################################################################### # class GeneratorTarget(enum.Enum): Library = enum_auto() # GeneratorTargetNames = { GeneratorTarget.Library: 'library', } # ################################################################################################### # ShortDataTypeNames = { cutlass.int32: 'i', cutlass.float16: 'h', cutlass.float32: 's', cutlass.float64: 'd', cutlass.dtype.cf32: 'c', cutlass.dtype.cf64: 'z', } # DataTypeNames = { cutlass.dtype.b1: "b1", cutlass.dtype.u4: "u4", cutlass.dtype.u8: "u8", cutlass.dtype.u16: "u16", cutlass.dtype.u32: "u32", cutlass.dtype.u64: "u64", cutlass.dtype.s4: "s4", cutlass.int8: "s8", cutlass.dtype.s16: "s16", cutlass.int32: "s32", cutlass.dtype.s64: "s64", cutlass.float16: "f16", cutlass.bfloat16: "bf16", cutlass.float32: "f32", cutlass.tfloat32: "tf32", cutlass.float64: "f64", cutlass.dtype.cf16: "cf16", cutlass.dtype.cbf16: "cbf16", cutlass.dtype.cf32: "cf32", cutlass.dtype.ctf32: "ctf32", cutlass.dtype.cf64: "cf64", cutlass.dtype.cu4: "cu4", cutlass.dtype.cu8: "cu8", cutlass.dtype.cu16: "cu16", cutlass.dtype.cu32: "cu32", cutlass.dtype.cu64: "cu64", cutlass.dtype.cs4: "cs4", cutlass.dtype.cs8: "cs8", cutlass.dtype.cs16: "cs16", cutlass.dtype.cs32: "cs32", cutlass.dtype.cs64: "cs64", } DataTypeTag = { cutlass.dtype.b1: "cutlass::uint1b_t", cutlass.dtype.u4: "cutlass::uint4b_t", cutlass.dtype.u8: "uint8_t", cutlass.dtype.u16: "uint16_t", cutlass.dtype.u32: "uint32_t", cutlass.dtype.u64: "uint64_t", cutlass.dtype.s4: "cutlass::int4b_t", cutlass.int8: "int8_t", cutlass.dtype.s16: "int16_t", cutlass.int32: "int32_t", cutlass.dtype.s64: "int64_t", cutlass.float16: "cutlass::half_t", cutlass.bfloat16: "cutlass::bfloat16_t", cutlass.float32: "float", cutlass.tfloat32: "cutlass::tfloat32_t", cutlass.float64: "double", cutlass.dtype.cf16: "cutlass::complex<cutlass::half_t>", cutlass.dtype.cbf16: "cutlass::complex<cutlass::bfloat16_t>", cutlass.dtype.cf32: "cutlass::complex<float>", cutlass.dtype.ctf32: "cutlass::complex<cutlass::tfloat32_t>", cutlass.dtype.cf64: "cutlass::complex<double>", cutlass.dtype.cu4: "cutlass::complex<cutlass::uint4b_t>", cutlass.dtype.cu8: "cutlass::complex<cutlass::uint8_t>", cutlass.dtype.cu16: "cutlass::complex<cutlass::uint16_t>", cutlass.dtype.cu32: "cutlass::complex<cutlass::uint32_t>", cutlass.dtype.cu64: "cutlass::complex<cutlass::uint64_t>", cutlass.dtype.cs4: "cutlass::complex<cutlass::int4b_t>", cutlass.dtype.cs8: "cutlass::complex<cutlass::int8_t>", cutlass.dtype.cs16: "cutlass::complex<cutlass::int16_t>", cutlass.dtype.cs32: "cutlass::complex<cutlass::int32_t>", cutlass.dtype.cs64: "cutlass::complex<cutlass::int64_t>", } DataTypeSize = { cutlass.dtype.b1: 1, cutlass.dtype.u4: 4, cutlass.dtype.u8: 8, cutlass.dtype.u16: 16, cutlass.dtype.u32: 32, cutlass.dtype.u64: 64, cutlass.dtype.s4: 4, cutlass.int8: 8, cutlass.dtype.s16: 16, cutlass.int32: 32, cutlass.dtype.s64: 64, cutlass.float16: 16, cutlass.bfloat16: 16, cutlass.float32: 32, cutlass.tfloat32: 32, cutlass.float64: 64, cutlass.dtype.cf16: 32, cutlass.dtype.cbf16: 32, cutlass.dtype.cf32: 64, cutlass.dtype.ctf32: 32, cutlass.dtype.cf64: 128, cutlass.dtype.cu4: 8, cutlass.dtype.cu8: 16, cutlass.dtype.cu16: 32, cutlass.dtype.cu32: 64, cutlass.dtype.cu64: 128, cutlass.dtype.cs4: 8, cutlass.dtype.cs8: 16, cutlass.dtype.cs16: 32, cutlass.dtype.cs32: 64, cutlass.dtype.cs64: 128, } class DataTypeSizeBytes: """ Static class to mimic the `DataTypeSize` dictionary, but with checks for whether the data type key is less than a full byte or a non-integer number of bytes. """ @staticmethod def __class_getitem__(datatype): """ Returns the number of bytes in size the data type is. Raises an exception if the data type is either less than a full byte or a non-integer number of bytes in size. :param datatype: data type to query :return: number of bytes the data type occupies :rtype: int """ bits = DataTypeSize[datatype] if bits < 8: raise Exception('Data type {} is less than one byte in size.'.format(datatype)) elif bits % 8 != 0: raise Exception('Data type {} is not an integer number of bytes.'.format(datatype)) return bits // 8 ################################################################################################### # class BlasMode(enum.Enum): symmetric = enum_auto() hermitian = enum_auto() # BlasModeTag = { BlasMode.symmetric: 'cutlass::BlasMode::kSymmetric', BlasMode.hermitian: 'cutlass::BlasMode::kHermitian', } # ComplexTransformTag = { cutlass.complex_transform.none: 'cutlass::ComplexTransform::kNone', cutlass.complex_transform.conj: 'cutlass::ComplexTransform::kConjugate', } # RealComplexBijection = [ (cutlass.float16, cutlass.dtype.cf16), (cutlass.float32, cutlass.dtype.cf32), (cutlass.float64, cutlass.dtype.cf64), ] # def is_complex(data_type): for r, c in RealComplexBijection: if data_type == c: return True return False # def get_complex_from_real(real_type): for r, c in RealComplexBijection: if real_type == r: return c return cutlass.dtype.invalid # def get_real_from_complex(complex_type): for r, c in RealComplexBijection: if complex_type == c: return r return cutlass.dtype.invalid # class ComplexMultiplyOp(enum.Enum): multiply_add = enum_auto() gaussian = enum_auto() ################################################################################################### # class MathOperation(enum.Enum): multiply_add = enum_auto() multiply_add_saturate = enum_auto() xor_popc = enum_auto() multiply_add_fast_bf16 = enum_auto() multiply_add_fast_f16 = enum_auto() multiply_add_fast_f32 = enum_auto() multiply_add_complex_fast_f32 = enum_auto() multiply_add_complex = enum_auto() multiply_add_complex_gaussian = enum_auto() # MathOperationNames = { MathOperation.multiply_add: 'multiply_add', MathOperation.multiply_add_saturate: 'multiply_add_saturate', MathOperation.xor_popc: 'xor_popc', MathOperation.multiply_add_fast_bf16: 'multiply_add_fast_bf16', MathOperation.multiply_add_fast_f16: 'multiply_add_fast_f16', MathOperation.multiply_add_fast_f32: 'multiply_add_fast_f32', MathOperation.multiply_add_complex_fast_f32: 'multiply_add_complex_fast_f32', MathOperation.multiply_add_complex: 'multiply_add_complex', MathOperation.multiply_add_complex_gaussian: 'multiply_add_complex_gaussian', } # MathOperationTag = { MathOperation.multiply_add: 'cutlass::arch::OpMultiplyAdd', MathOperation.multiply_add_saturate: 'cutlass::arch::OpMultiplyAddSaturate', MathOperation.xor_popc: 'cutlass::arch::OpXorPopc', MathOperation.multiply_add_fast_bf16: 'cutlass::arch::OpMultiplyAddFastBF16', MathOperation.multiply_add_fast_f16: 'cutlass::arch::OpMultiplyAddFastF16', MathOperation.multiply_add_fast_f32: 'cutlass::arch::OpMultiplyAddFastF32', MathOperation.multiply_add_complex_fast_f32: 'cutlass::arch::OpMultiplyAddComplexFastF32', MathOperation.multiply_add_complex: 'cutlass::arch::OpMultiplyAddComplex', MathOperation.multiply_add_complex_gaussian: 'cutlass::arch::OpMultiplyAddGaussianComplex', } ################################################################################################### # LayoutTag = { cutlass.ColumnMajor: 'cutlass::layout::ColumnMajor', cutlass.RowMajor: 'cutlass::layout::RowMajor', cutlass.layout.ColumnMajorInterleaved2: 'cutlass::layout::ColumnMajorInterleaved<2>', cutlass.layout.RowMajorInterleaved2: 'cutlass::layout::RowMajorInterleaved<2>', cutlass.ColumnMajorInterleaved32: 'cutlass::layout::ColumnMajorInterleaved<32>', cutlass.RowMajorInterleaved32: 'cutlass::layout::RowMajorInterleaved<32>', cutlass.layout.ColumnMajorInterleaved64: 'cutlass::layout::ColumnMajorInterleaved<64>', cutlass.layout.RowMajorInterleaved64: 'cutlass::layout::RowMajorInterleaved<64>', cutlass.TensorNHWC: 'cutlass::layout::TensorNHWC', cutlass.layout.TensorNDHWC: 'cutlass::layout::TensorNDHWC', cutlass.layout.TensorNCHW: 'cutlass::layout::TensorNCHW', cutlass.layout.TensorNGHWC: 'cutlass::layout::TensorNGHWC', cutlass.TensorNC32HW32: 'cutlass::layout::TensorNCxHWx<32>', cutlass.TensorC32RSK32: 'cutlass::layout::TensorCxRSKx<32>', cutlass.layout.TensorNC64HW64: 'cutlass::layout::TensorNCxHWx<64>', cutlass.layout.TensorC64RSK64: 'cutlass::layout::TensorCxRSKx<64>', } # TransposedLayout = { cutlass.ColumnMajor: cutlass.RowMajor, cutlass.RowMajor: cutlass.ColumnMajor, cutlass.layout.ColumnMajorInterleaved2: cutlass.layout.RowMajorInterleaved2, cutlass.layout.RowMajorInterleaved2: cutlass.layout.ColumnMajorInterleaved2, cutlass.ColumnMajorInterleaved32: cutlass.RowMajorInterleaved32, cutlass.RowMajorInterleaved32: cutlass.ColumnMajorInterleaved32, cutlass.layout.ColumnMajorInterleaved64: cutlass.layout.RowMajorInterleaved64, cutlass.layout.RowMajorInterleaved64: cutlass.layout.ColumnMajorInterleaved64, cutlass.TensorNHWC: cutlass.TensorNHWC } # ShortLayoutTypeNames = { cutlass.ColumnMajor: 'n', cutlass.layout.ColumnMajorInterleaved2: 'n2', cutlass.ColumnMajorInterleaved32: 'n32', cutlass.layout.ColumnMajorInterleaved64: 'n64', cutlass.RowMajor: 't', cutlass.layout.RowMajorInterleaved2: 't2', cutlass.RowMajorInterleaved32: 't32', cutlass.layout.RowMajorInterleaved64: 't64', cutlass.TensorNHWC: 'nhwc', cutlass.layout.TensorNDHWC: 'ndhwc', cutlass.layout.TensorNCHW: 'nchw', cutlass.layout.TensorNGHWC: 'nghwc', cutlass.TensorNC32HW32: 'nc32hw32', cutlass.layout.TensorNC64HW64: 'nc64hw64', cutlass.TensorC32RSK32: 'c32rsk32', cutlass.layout.TensorC64RSK64: 'c64rsk64' } # ShortComplexLayoutNames = { (cutlass.ColumnMajor, cutlass.complex_transform.none): 'n', (cutlass.ColumnMajor, cutlass.complex_transform.conj): 'c', (cutlass.RowMajor, cutlass.complex_transform.none): 't', (cutlass.RowMajor, cutlass.complex_transform.conj): 'h' } # CuTeLayoutTag = { cute.GMMAMajor.K: 'cute::GMMA::Major::K', cute.GMMAMajor.MN: 'cute::GMMA::Major::MN' } ################################################################################################### # class SideMode(enum.Enum): Left = enum_auto() Right = enum_auto() # SideModeTag = { SideMode.Left: 'cutlass::SideMode::kLeft', SideMode.Right: 'cutlass::SideMode::kRight' } # ShortSideModeNames = { SideMode.Left: 'ls', SideMode.Right: 'rs' } ################################################################################################### # class FillMode(enum.Enum): Lower = enum_auto() Upper = enum_auto() # FillModeTag = { FillMode.Lower: 'cutlass::FillMode::kLower', FillMode.Upper: 'cutlass::FillMode::kUpper' } # ShortFillModeNames = { FillMode.Lower: 'l', FillMode.Upper: 'u' } ################################################################################################### # class DiagType(enum.Enum): NonUnit = enum_auto() Unit = enum_auto() # DiagTypeTag = { DiagType.NonUnit: 'cutlass::DiagType::kNonUnit', DiagType.Unit: 'cutlass::DiagType::kUnit' } # ShortDiagTypeNames = { DiagType.NonUnit: 'nu', DiagType.Unit: 'un' } ################################################################################################### OpcodeClassNames = { cutlass.OpClass.Simt: 'simt', cutlass.OpClass.TensorOp: 'tensorop', cutlass.OpClass.WmmaTensorOp: 'wmma_tensorop', cutlass.OpClass.SparseTensorOp: 'sptensorop' } OpcodeClassTag = { cutlass.OpClass.Simt: 'cutlass::arch::OpClassSimt', cutlass.OpClass.TensorOp: 'cutlass::arch::OpClassTensorOp', cutlass.OpClass.WmmaTensorOp: 'cutlass::arch::OpClassWmmaTensorOp', cutlass.OpClass.SparseTensorOp: 'cutlass::arch::OpClassSparseTensorOp' } ################################################################################################### # class OperationKind(enum.Enum): Gemm = enum_auto() RankK = enum_auto() Rank2K = enum_auto() Trmm = enum_auto() Symm = enum_auto() Conv2d = enum_auto() Conv3d = enum_auto() # OperationKindNames = { OperationKind.Gemm: 'gemm', OperationKind.RankK: 'rank_k', OperationKind.Rank2K: 'rank_2k', OperationKind.Trmm: 'trmm', OperationKind.Symm: 'symm', OperationKind.Conv2d: 'conv2d', OperationKind.Conv3d: 'conv3d' } # ArchitectureNames = { 50: 'maxwell', 60: 'pascal', 61: 'pascal', 70: 'volta', 75: 'turing', 80: 'ampere', 90: 'hopper' } # SharedMemPerCC = { 70: 96 << 10, # 96KB of SMEM 72: 96 << 10, # 96KB of SMEM 75: 64 << 10, # 64KB of SMEM 80: 160 << 10, # 164KB of SMEM - 4KB reserved for the driver 86: 100 << 10, # 100KB of SMEM 87: 160 << 10, # 164KB of SMEM - 4KB reserved for the driver 89: 100 << 10, # 100KB of SMEM 90: 227 << 10, # 228KB of SMEM - 1KB reserved for the driver } ################################################################################################### class GemmKind(enum.Enum): Gemm = enum_auto() Sparse = enum_auto() Universal = enum_auto() PlanarComplex = enum_auto() PlanarComplexArray = enum_auto() Grouped = enum_auto() # GemmKindNames = { GemmKind.Gemm: "gemm", GemmKind.Sparse: "spgemm", GemmKind.Universal: "gemm", GemmKind.PlanarComplex: "gemm_planar_complex", GemmKind.PlanarComplexArray: "gemm_planar_complex_array", GemmKind.Grouped: "gemm_grouped" } # class RankKKind(enum.Enum): Universal = enum_auto() # RankKKindNames = { RankKKind.Universal: "rank_k" } # class TrmmKind(enum.Enum): Universal = enum_auto() # TrmmKindNames = { TrmmKind.Universal: "trmm" } # class SymmKind(enum.Enum): Universal = enum_auto() # SymmKindNames = { SymmKind.Universal: "symm" } # class SwizzlingFunctor(enum.Enum): Identity1 = enum_auto() Identity2 = enum_auto() Identity4 = enum_auto() Identity8 = enum_auto() Horizontal = enum_auto() BatchedIdentity1 = enum_auto() StridedDgradIdentity1 = enum_auto() StridedDgradIdentity4 = enum_auto() StridedDgradHorizontal = enum_auto() # SwizzlingFunctorTag = { cutlass.IdentitySwizzle1: 'cutlass::gemm::threadblock::GemmIdentityThreadblockSwizzle<1>', SwizzlingFunctor.Identity2: 'cutlass::gemm::threadblock::GemmIdentityThreadblockSwizzle<2>', SwizzlingFunctor.Identity4: 'cutlass::gemm::threadblock::GemmIdentityThreadblockSwizzle<4>', SwizzlingFunctor.Identity8: 'cutlass::gemm::threadblock::GemmIdentityThreadblockSwizzle<8>', SwizzlingFunctor.Horizontal: 'cutlass::gemm::threadblock::GemmHorizontalThreadblockSwizzle', SwizzlingFunctor.BatchedIdentity1: "cutlass::gemm::threadblock::GemmBatchedIdentityThreadblockSwizzle", SwizzlingFunctor.StridedDgradIdentity1: 'cutlass::conv::threadblock::StridedDgradIdentityThreadblockSwizzle<1>', SwizzlingFunctor.StridedDgradIdentity4: 'cutlass::conv::threadblock::StridedDgradIdentityThreadblockSwizzle<4>', SwizzlingFunctor.StridedDgradHorizontal: 'cutlass::conv::threadblock::StridedDgradHorizontalThreadblockSwizzle', } # class SchedulerMode(enum.Enum): Device = enum_auto(), Host = enum_auto() # SchedulerModeTag = { SchedulerMode.Device: 'cutlass::gemm::kernel::GroupScheduleMode::kDeviceOnly', SchedulerMode.Host: 'cutlass::gemm::kernel::GroupScheduleMode::kHostPrecompute' } # ShortSchedulerModeNames = { SchedulerMode.Device: 'Device', SchedulerMode.Host: 'Host' } ################################################################################################### # ConvKindTag = { cutlass.conv.Operator.fprop: 'cutlass::conv::Operator::kFprop', cutlass.conv.Operator.dgrad: 'cutlass::conv::Operator::kDgrad', cutlass.conv.Operator.wgrad: 'cutlass::conv::Operator::kWgrad' } ConvKindNames = { cutlass.conv.Operator.fprop: 'fprop', cutlass.conv.Operator.dgrad: 'dgrad', cutlass.conv.Operator.wgrad: 'wgrad', } # IteratorAlgorithmTag = { cutlass.conv.IteratorAlgorithm.analytic: 'cutlass::conv::IteratorAlgorithm::kAnalytic', cutlass.conv.IteratorAlgorithm.optimized: 'cutlass::conv::IteratorAlgorithm::kOptimized', cutlass.conv.IteratorAlgorithm.fixed_channels: 'cutlass::conv::IteratorAlgorithm::kFixedChannels', cutlass.conv.IteratorAlgorithm.few_channels: 'cutlass::conv::IteratorAlgorithm::kFewChannels' } IteratorAlgorithmNames = { cutlass.conv.IteratorAlgorithm.analytic: 'analytic', cutlass.conv.IteratorAlgorithm.optimized: 'optimized', cutlass.conv.IteratorAlgorithm.fixed_channels: 'fixed_channels', cutlass.conv.IteratorAlgorithm.few_channels: 'few_channels' } # class StrideSupport(enum.Enum): Strided = enum_auto() Unity = enum_auto() # StrideSupportTag = { StrideSupport.Strided: 'cutlass::conv::StrideSupport::kStrided', StrideSupport.Unity: 'cutlass::conv::StrideSupport::kUnity', } StrideSupportNames = { StrideSupport.Strided: '', StrideSupport.Unity: 'unity_stride', } class ConvMode(enum.Enum): CrossCorrelation = enum_auto() Convolution = enum_auto() # ConvModeTag = { ConvMode.CrossCorrelation: 'cutlass::conv::Mode::kCrossCorrelation', ConvMode.Convolution: 'cutlass::conv::Mode::kConvolution' } ################################################################################################### # class MathInstruction: """ Description of a the lowest-level matrix-multiply-accumulate operation to be used in a kernel """ def __init__(self, instruction_shape, element_a, element_b, element_accumulator, opcode_class=cutlass.OpClass.Simt, math_operation=MathOperation.multiply_add): """ :param instruction_shape: size of the [M, N, K] dimensions of the instruction :type instruction_shape: list or tuple :param element_a: data type of operand A :param element_b: data type of operand B :param element_accumulator: data type used in accumulation :param opcode_class: higher-level class of the instruction (e.g., SIMT or Tensor Core) :type opcode_class: cutlass.OpClass :param math_operation: the type of low-level operation to be performed (e.g., multiply accumulate) :type math_operation: MathOperation """ self.instruction_shape = instruction_shape self.element_a = element_a self.element_b = element_b self.element_accumulator = element_accumulator self.opcode_class = opcode_class self.math_operation = math_operation # class TileDescription: """ Description of a tile of computation to be performed in the kernel, encompassing threadblock, cluster, and warp shapes, stage count, and math instruction specification """ def __init__(self, threadblock_shape, stages, warp_count, math_instruction, cluster_shape=[1, 1, 1], persistent=False): """ :param threadblock_shape: shape of a threadblock tyle :type threadblock_shape: list or tuple :param stages: number of pipeline stages in the operation. For SM90 kernels, this can be set to `None` and the maximum number of stages that can be supported for an operation on a given architecture will be computed at a later time :type stages: int or None :param warp_count: number of warps in each [M, N, K] dimension of a threadblock tile :type warp_count: list, tuple, or None :param math_instruction: specification of the instruction type and shape to be performed and the types of its operands :type math_instruction: MathInstruction :param cluster_shape: number of threadblocks in the [X, Y, Z] dimensions of a threadblock cluster :param persistent: whether the kernel uses persistent warp-specialized threadblocks (only available for SM90+) :type persistent: bool """ self.threadblock_shape = threadblock_shape self.cluster_shape = cluster_shape self.persistent: bool = persistent self.stages: int = stages self.math_instruction = math_instruction # Number of warps along x, y, z directions self.warp_count = warp_count @property def num_threads(self): """ Returns the number of threads in the threadblock :return: number of threads in the threadblock :rtype: int or None (if warp count is None) """ if self.warp_count is not None: threads = 32 for cnt in self.warp_count: threads *= cnt return threads return None def procedural_name(self): """ Returns a name identifying the tile description :return: name identifying the tile description :rtype: int """ emit_stages = 0 if self.stages is None else self.stages name = "%dx%dx%d_%dx%d_%dx%d" % ( self.cluster_shape[0], self.cluster_shape[1], self.cluster_shape[2], self.threadblock_shape[0], self.threadblock_shape[1], self.threadblock_shape[2], emit_stages) if self.persistent: name += '_persistent' return name # class TensorDescription: def __init__(self, element, layout, alignment=1, complex_transform=cutlass.complex_transform.none): self.element = element self.layout = layout self.alignment = min(128 // DataTypeSize[self.element], alignment) self.complex_transform = complex_transform # class SymmetricTensorDescription: def __init__(self, element, layout, fill_mode, alignment=1, complex_transform=cutlass.complex_transform.none, side_mode=SideMode.Left): self.element = element self.layout = layout self.fill_mode = fill_mode self.alignment = alignment self.complex_transform = complex_transform self.side_mode = side_mode # class TriangularTensorDescription: def __init__(self, element, layout, side_mode, fill_mode, diag_type, alignment=1, complex_transform=cutlass.complex_transform.none): self.element = element self.layout = layout self.side_mode = side_mode self.fill_mode = fill_mode self.diag_type = diag_type self.alignment = alignment self.complex_transform = complex_transform ################################################################################################### # def CalculateSmemUsagePerStage(operation): """ Returns the amount of shared memory in bytes consumed in a single stage of a kernel. :param op: operation for which the maximum stages should be computed. If stages are set via the `op.tile_description.stages` parameter, this setting is ignored in the present calculation :type op: pycutlass.Operation :return: number of bytes of shared memory consumed by a single stage :rtype: int """ m, n, k = operation.tile_description.threadblock_shape if operation.operation_kind == OperationKind.Gemm: stage_barrier_bytes = 32 return (DataTypeSize[operation.A.element] * m * k // 8) + \ (DataTypeSize[operation.B.element] * k * n // 8) + stage_barrier_bytes else: raise Exception('Unsupported operation kind {}.'.format(operation.operation_kind)) # def CalculateSmemUsage(operation): """ Returns the amount of shared memory in bytes consumed by a kernel. :param op: operation for which the maximum stages should be computed. If stages are set via the `op.tile_description.stages` parameter, this setting is ignored in the present calculation :type op: pycutlass.Operation :return: int """ return operation.tile_description.stages * CalculateSmemUsagePerStage(operation) class ApiVersion(enum.Enum): """ Differentiate between CUTLASS 2.x and 3.x API versions """ v2x = enum_auto() v3x = enum_auto() def api_version(arch, opclass, datatype): """ Returns whether the architecture, opcode class, and datatype in question require using CUTLASS 2.x or 3.x for code emission. :param arch: compute capability of device on which to run :type arch: int :param opclass: class of the operation being performed :type opclass: cutlass.OpClass :param datatype: data type to be used in operation (assumes that ElementA and ElementB are the same) :return: API version to be used in code emission :rtype: ApiVersion """ if arch >= 90 and opclass == cutlass.OpClass.TensorOp and (datatype != cutlass.float64): return ApiVersion.v3x else: return ApiVersion.v2x ###################################################################################################
################################################################################ # # Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: BSD-3-Clause # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # 3. Neither the name of the copyright holder nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ################################################################################ from typing import Union from typeguard import typechecked GemmOperation = 'Union[GemmOperationUniversal, GemmOperationGrouped]' Tensor = 'Union[cuda.CUdeviceptr, np.ndarray, torch.Tensor, cp.ndarray]'
################################################################################ # # Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: BSD-3-Clause # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # 3. Neither the name of the copyright holder nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ################################################################################ from pycutlass import * from pycutlass.c_types import get_reduction_params import cutlass from cuda import cuda try: import torch torch_available = True except ImportError: torch_available = False import numpy as np from typing import Union from cuda import cudart class ReductionOperation: pass class ReductionArguments: """ Arguments of reduction """ def __init__(self, operation: ReductionOperation, problem_size: 'list[int]', partitions: int, workspace: cuda.CUdeviceptr, destination: 'Union[cuda.CUdeviceptr, np.ndarray, torch.Tensor]', source: 'Union[cuda.CUdeviceptr, np.ndarray, torch.Tensor]', **kwargs) -> None: # tensor_C can be interpreted as the bias with bias=True in keyword args if "bias" in kwargs.keys(): self.bias = kwargs["bias"] else: # by default, tensor_C is not bias self.bias = False self.operation = operation #: pointer to the workspace self.ptr_workspace = workspace #: number of split-k partitions self.partitions = partitions if isinstance(destination, np.ndarray): self.host_D = destination self.destination_buffer = NumpyFrontend.argument(destination, True) self.source_buffer = NumpyFrontend.argument(source, False) self.ptr_destination = cuda.CUdeviceptr( self.destination_buffer.ptr) self.ptr_source = cuda.CUdeviceptr(self.source_buffer.ptr) elif torch_available and isinstance(destination, torch.Tensor): self.ptr_destination = TorchFrontend.argument(destination) self.ptr_source = TorchFrontend.argument(source) elif isinstance(destination, cuda.CUdeviceptr): self.ptr_destination = destination self.ptr_source = source else: raise TypeError("unknown Type") self.problem_size = MatrixCoord_( problem_size[0], problem_size[1] ) self.partition_stride = problem_size[0] * \ problem_size[1] * DataTypeSize[operation.C.element] // 8 if "output_op" in kwargs.keys(): self.output_op = kwargs['output_op'] else: self.output_op = self.operation.epilogue_type(1.0, 0.0) # get arguments self.get_arguments() @staticmethod def get_tensor_ref(extent: 'tuple[int]', device_ptr: cuda.CUdeviceptr, layout: cutlass.layout): if layout == cutlass.RowMajor: return TensorRef2D_(int(device_ptr), extent[1]) else: raise ValueError("unknown layout type") def get_arguments(self): ref_workspace = ReductionArguments.get_tensor_ref( extent=[self.problem_size.row, self.problem_size.column], device_ptr=self.ptr_workspace, layout=cutlass.RowMajor) if self.bias: ref_source = ReductionArguments.get_tensor_ref( extent=[0, 0], device_ptr=self.ptr_source, layout=cutlass.RowMajor) else: ref_source = ReductionArguments.get_tensor_ref( extent=[self.problem_size.row, self.problem_size.column], device_ptr=self.ptr_source, layout=cutlass.RowMajor) ref_destination = ReductionArguments.get_tensor_ref( extent=[self.problem_size.row, self.problem_size.column], device_ptr=self.ptr_destination, layout=cutlass.RowMajor) self.c_arguments = self.operation.argument_type( self.problem_size, self.partitions, self.partition_stride, ref_workspace, ref_destination, ref_source, self.output_op ) params_ = self.operation.rt_module.get_args( ctypes.byref(self.c_arguments)) self.host_workspace = bytearray(params_.contents) def sync(self): err, = cudart.cudaDeviceSynchronize() if err != cuda.CUresult.CUDA_SUCCESS: raise RuntimeError("CUDA Error %s" % str(err)) if hasattr(self, "host_D"): err, = cuda.cuMemcpyDtoH( self.host_D, self.ptr_destination, self.host_D.size * self.host_D.itemsize) if err != cuda.CUresult.CUDA_SUCCESS: raise RuntimeError("CUDA Error %s" % str(err)) def free(self): if hasattr(self, "destination_buffer"): del self.destination_buffer if hasattr(self, "source_buffer"): del self.source_buffer class ReductionRT(ExecutableOperation): """ ReductionRT manages the CUTLASS runtime components for reduction """ KernelTemplate = r''' extern "C" __global__ void ${operation_name}(${operation_name}${operation_suffix}::Params params) { // Dynamic shared memory base pointer extern __shared__ int SharedStorageBase[]; // Declare pointer to dynamic shared memory. ${operation_name}${operation_suffix}::SharedStorage *shared_storage = reinterpret_cast<${operation_name}${operation_suffix}::SharedStorage *>(SharedStorageBase); ${operation_name}${operation_suffix} op; op(params, *shared_storage); } ''' HostTemplate = r''' extern "C" { // Get the size of params in bytes int ${operation_name}_get_param_size(){ return sizeof(${operation_name}${operation_suffix}::Params); } // Get the size of dynamic shared memory in bytes int ${operation_name}_shared_memory_size() { return int(sizeof(${operation_name}${operation_suffix}::SharedStorage)); } // Get the params as byte array char* ${operation_name}_get_params(${operation_name}${operation_suffix}::Params* params){ char *bytes = ((char*)(params)); char *output = new char[sizeof(${operation_name}${operation_suffix}::Params)]; for (unsigned int i = 0; i < sizeof(${operation_name}${operation_suffix}::Params); i ++) output[i] = bytes[i]; return output; } } ''' def __init__(self, operation: ReductionOperation): super().__init__(operation) self.operation: ReductionOperation = operation self.emitter = EmitReductionInstance('_type') self.elements_per_access = self.operation.count self.argument_type, self.epilogue_type = get_reduction_params(operation.epilogue_functor) self.argtype = [ctypes.POINTER(self.argument_type)] def emit(self): return self.emitter.emit(self.operation) def plan(self, arguments: ReductionArguments): block_shape = [self.operation.shape.column( ) // self.elements_per_access, self.operation.shape.row(), 1] grid_shape = [ (arguments.problem_size.row + self.operation.shape.row() - 1) // self.operation.shape.row(), (arguments.problem_size.column + self.operation.shape.column() - 1) // self.operation.shape.column(), 1 ] return LaunchConfiguration(grid_shape, block_shape, self.shared_memory_capacity) def initialize(self): err, = cuda.cuFuncSetAttribute( self.kernel, attrib=cuda.CUfunction_attribute.CU_FUNC_ATTRIBUTE_MAX_DYNAMIC_SHARED_SIZE_BYTES, value=self.shared_memory_capacity) if err != cuda.CUresult.CUDA_SUCCESS: raise RuntimeError('Cuda Error: {}'.format(err)) class ReductionOperation: """ CUTLASS Reduction Operation shape: shape of CTA outputop: output operator r """ def __init__(self, shape: cutlass.MatrixCoord, C: TensorDescription, element_accumulator, element_workspace=None, element_compute=None, epilogue_functor=None, count: int = 1, partitions_per_stage: int = 4) -> None: """ Constructor """ self.shape = shape #: epilogue functor (default: LinearCombination) self.epilogue_functor = epilogue_functor #: datatype of accumulator self.element_accumulator = element_accumulator if element_workspace is None: #: datatype of workspace self.element_workspace = element_accumulator else: #: datatype of workspace self.element_workspace = element_workspace if element_compute is None: #: datatype of workspace self.element_compute = element_accumulator else: #: datatype of workspace self.element_compute = element_compute #: datatype of output self.element_output = C.element #: operand C self.C: TensorDescription = C #: reduce op processing size self.count: int = count #: number of partitions to reduce per stage self.partitions_per_stage: int = partitions_per_stage self.rt_module: ReductionRT = ReductionRT(self) self.argument_type = self.rt_module.argument_type self.epilogue_type = self.rt_module.epilogue_type # def extended_name(self): extend_name = "${element_workspace}_${element_accumulator}_${element_compute}_${element_output}" return SubstituteTemplate(extend_name, { 'element_workspace': DataTypeNames[self.element_workspace], 'element_accumulator': DataTypeNames[self.element_accumulator], 'element_compute': DataTypeNames[self.element_compute], 'element_output': DataTypeNames[self.element_output] }) # def configuration_name(self): ''' The full procedural name indicates architecture, extended name, tile size''' configuration_name = "cutlass_reduce_split_k_${extended_name}_${threadblock}" threadblock = "%dx%d" % ( self.shape.row(), self.shape.column() ) return SubstituteTemplate( configuration_name, { 'extended_name': self.extended_name(), 'threadblock': threadblock } ) # def procedural_name(self): ''' The full procedural name indicates architecture, extended name, tile size''' return self.configuration_name() def run(self, arguments: ReductionArguments) -> cuda.CUresult: """ Configure and launch the cuda kernel with input arguments """ # get launch configuration launch_config = self.rt_module.plan(arguments) # get the host and device workspace host_workspace = arguments.host_workspace device_workspace = None # launch the kernel err = self.rt_module.run( host_workspace, device_workspace, launch_config) if err != cuda.CUresult.CUDA_SUCCESS: raise RuntimeError('CUDA Error %s' % str(err)) return err class EmitReductionInstance: def __init__(self, operation_suffix='') -> None: self.operation_suffix = operation_suffix self.includes = [ "cutlass/cutlass.h", "cutlass/numeric_types.h", "cutlass/arch/arch.h", "cutlass/arch/mma.h", "cutlass/layout/matrix.h", "cutlass/gemm/device/gemm.h", "cutlass/gemm/device/gemm_universal_adapter.h", "cutlass/gemm/kernel/default_gemm_universal.h", "cutlass/reduction/kernel/reduce_split_k.h", "cutlass/reduction/thread/reduction_operators.h" ] self.template = """ // Reduction kernel instance using ${operation_name}_base = typename cutlass::reduction::kernel::ReduceSplitK< cutlass::MatrixShape<${shape_row}, ${shape_column}>, ${epilogue_functor}, cutlass::reduction::thread::ReduceAdd< ${element_accumulator}, ${element_output}, ${count}>, ${partition_per_stage}>; struct ${operation_name}${operation_suffix}: public ${operation_name}_base { }; """ def emit(self, operation: ReductionOperation): epilogue_vector_length = int(min( operation.C.alignment * DataTypeSize[operation.C.element], 128) / DataTypeSize[operation.C.element]) values = { 'operation_name': operation.configuration_name(), 'operation_suffix': self.operation_suffix, 'shape_row': str(operation.shape.row()), 'shape_column': str(operation.shape.column()), 'epilogue_functor': operation.epilogue_functor.emit(), 'element_output': DataTypeTag[operation.element_output], 'epilogue_vector_length': str(epilogue_vector_length), 'element_accumulator': DataTypeTag[operation.element_accumulator], 'element_compute': DataTypeTag[operation.element_compute], 'element_workspace': DataTypeTag[operation.element_workspace], 'count': str(operation.count), 'partition_per_stage': str(operation.partitions_per_stage) } return SubstituteTemplate(self.template, values)
################################################################################################# # # Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: BSD-3-Clause # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # 3. Neither the name of the copyright holder nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ################################################################################################# from .frontend import CupyFrontend from typeguard import typechecked from pycutlass.frontend import * from typing import Union import numpy as np from cuda import cuda try: import torch torch_available = True except ImportError: torch_available = False from cuda import cudart try: import cupy as cp cupy_available = True except ImportError: cupy_available = False # @typechecked class ArgumentBase: """ Base class for operation arguments """ def __init__(self, A: 'Union[cuda.CUdeviceptr, np.ndarray, torch.Tensor, cp.ndarray]', B: 'Union[cuda.CUdeviceptr, np.ndarray, torch.Tensor, cp.ndarray]', C: 'Union[cuda.CUdeviceptr, np.ndarray, torch.Tensor, cp.ndarray]', D: 'Union[cuda.CUdeviceptr, np.ndarray, torch.Tensor, cp.ndarray]', **kwargs) -> None: # tensor_C can be interpreted as the bias with bias=True in keyword args if "bias" in kwargs.keys(): self.bias = kwargs["bias"] else: # by default, tensor_C is not bias self.bias = False # preprocessing input tensors if isinstance(A, np.ndarray): self.host_D = D self.buffer_A = NumpyFrontend.argument(A, False) self.buffer_B = NumpyFrontend.argument(B, False) self.buffer_C = NumpyFrontend.argument(C, False) self.buffer_D = NumpyFrontend.argument(D, True) self.ptr_A = self.buffer_A.ptr self.ptr_B = self.buffer_B.ptr self.ptr_C = self.buffer_C.ptr self.ptr_D = self.buffer_D.ptr # number of elements in C self.tensor_c_numel = C.size elif torch_available and isinstance(A, torch.Tensor): self.ptr_A = TorchFrontend.argument(A) self.ptr_B = TorchFrontend.argument(B) self.ptr_C = TorchFrontend.argument(C) self.ptr_D = TorchFrontend.argument(D) # number of elements in C self.tensor_c_numel = C.numel() elif isinstance(A, cuda.CUdeviceptr): self.ptr_A = A self.ptr_B = B self.ptr_C = C self.ptr_D = D elif cupy_available and isinstance(A, cp.ndarray): self.ptr_A = CupyFrontend.argument(A) self.ptr_B = CupyFrontend.argument(B) self.ptr_C = CupyFrontend.argument(C) self.ptr_D = CupyFrontend.argument(D) # number of elements in C self.tensor_c_numel = C.size else: raise TypeError( "Unsupported Frontend. Only support numpy and torch") def sync(self, stream_sync=True): if stream_sync: err, = cudart.cudaDeviceSynchronize() if err != cuda.CUresult.CUDA_SUCCESS: raise RuntimeError("CUDA Error %s" % str(err)) if hasattr(self, "host_D"): err, = cuda.cuMemcpyDtoH( self.host_D, self.ptr_D, self.host_D.size * self.host_D.itemsize) if err != cuda.CUresult.CUDA_SUCCESS: raise RuntimeError("CUDA Error %s" % str(err))
################################################################################ # # Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved # SPDX-License-Identifier: BSD-3-Clause # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # 3. Neither the name of the copyright holder nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ################################################################################ import numpy as np from cuda import cuda from pycutlass.memory_manager import * from typing import TYPE_CHECKING try: import torch torch_available = True except ImportError: torch_available = False if TYPE_CHECKING: import torch try: import cupy as cp cupy_available = True except ImportError: cupy_available = False if TYPE_CHECKING: import cupy as cp class NumpyFrontend: """ Frontend node for numpy """ @staticmethod def argument(np_tensor: 'np.ndarray', is_output: 'bool') -> cuda.CUdeviceptr: """Convert the input numpy tensor to CUDA device pointer :param np_tensor: input numpy nd array :param is_output: whether the tensor is output :return: CUDA device pointer """ # copy the data to device if is_output: return device_mem_alloc(np_tensor.size * np_tensor.itemsize) else: return todevice(np_tensor) class TorchFrontend: """ Frontend node for torch """ @staticmethod def argument(torch_tensor: 'torch.Tensor') -> cuda.CUdeviceptr: """Convert the input torch tensor to CUDA device pointer :param torch_tensor: input torch tensor :param is_output: whether the tensor is output :return: CUDA device pointer """ # check the device of torch_tensor if not torch_tensor.is_cuda: torch_tensor = torch_tensor.to("cuda") return cuda.CUdeviceptr(torch_tensor.data_ptr()) class CupyFrontend: """ Frontend node for cupy """ @staticmethod def argument(cupy_ndarray: 'cp.ndarray'): return cuda.CUdeviceptr(int(cupy_ndarray.data.ptr))
import re def SubstituteTemplate(template, values): text = template changed = True while changed: changed = False for key, value in values.items(): regex = "\\$\\{%s\\}" % key newtext = re.sub(regex, value, text) if newtext != text: changed = True text = newtext return text from pycutlass.type_hint import * from pycutlass.tensor_ref import * from pycutlass.operation import * from pycutlass.epilogue import * from pycutlass.parser import * from pycutlass.compiler import ArtifactManager from pycutlass.memory_manager import * from pycutlass.arguments import * from pycutlass.library import * from pycutlass.c_types import * from pycutlass.gemm_operation import * from pycutlass.conv2d_operation import * from pycutlass.compiler import * from pycutlass.utils import * from pycutlass.frontend import * from pycutlass.reduction_operation import * from pycutlass.compiler import * from pycutlass.utils.device import device_cc # module-wide variables import sys this = sys.modules[__name__] # artifact manager this.compiler = ArtifactManager() try: if not hasattr(this, 'DEVICE_CC') or this.DEVICE_CC is None: this.DEVICE_CC = device_cc() except: this.DEVICE_CC = None def get_memory_pool(init_pool_size=0, max_pool_size=2**34): this.memory_pool = PoolMemoryManager( init_pool_size=init_pool_size, max_pool_size=max_pool_size ) return this.memory_pool
################################################################################ # # Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved # SPDX-License-Identifier: BSD-3-Clause # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # 3. Neither the name of the copyright holder nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ################################################################################ from typeguard import typechecked from cuda import cuda from typing import Union import numpy as np from typeguard import typechecked from pycutlass import * # @typechecked class Conv2dArguments(ArgumentBase): """ Argument wrapper for Conv2d. It encodes problem information and user-provide tensors into the kernel's argument. :param operation: the Conv2d operation to take the argument :type operation: :class:`pycutlass.Conv2dOperation` :param problem_size: the Conv2d problem size :type problem_size: :class:`cutlass.conv.Conv2dProblemSize` :param A: tensor A :type A: cuda.CUdeviceptr | numpy.ndarray | torch.Tensor | cupy.ndarray :param B: tensor B :type B: cuda.CUdeviceptr | numpy.ndarray | torch.Tensor | cupy.ndarray :param C: tensor C :type C: cuda.CUdeviceptr | numpy.ndarray | torch.Tensor | cupy.ndarray :param D: tensor D :type D: cuda.CUdeviceptr | numpy.ndarray | torch.Tensor | cupy.ndarray :param split_k_mode: conv2d split K mode, defaults to cutlass.conv.SplitKMode.Serial :type split_k_mode: cutlass.conv.SplitKMode, optional :param output_op: output operator, optional :type output_op: :class:`pycutlass.LinearCombinationFunctorArguments` """ def __init__(self, operation: 'Conv2dOperation', problem_size: 'cutlass.conv.Conv2dProblemSize', A: 'Union[cuda.CUdeviceptr, np.ndarray, torch.Tensor]', B: 'Union[cuda.CUdeviceptr, np.ndarray, torch.Tensor]', C: 'Union[cuda.CUdeviceptr, np.ndarray, torch.Tensor]', D: 'Union[cuda.CUdeviceptr, np.ndarray, torch.Tensor]', split_k_mode: 'cutlass.conv.SplitKMode' = cutlass.conv.SplitKMode.Serial, **kwargs) -> None: self.operation = operation #: convolution kind self.conv_kind: cutlass.conv.Operator = operation.conv_kind self.layout_A: cutlass.layout = operation.A.layout self.layout_B: cutlass.layout = operation.B.layout self.layout_C: cutlass.layout = operation.C.layout self.element_A = operation.A.element self.element_B = operation.B.element self.element_C = operation.C.element if self.layout_C == cutlass.TensorNC32HW32: B = self.reorder_tensor_B(B, problem_size) super().__init__(A, B, C, D, **kwargs) # preprocessing output ops if 'output_op' in kwargs.keys() and \ split_k_mode != cutlass.conv.SplitKMode.Parallel: self.output_op = kwargs['output_op'] else: self.output_op = self.operation.epilogue_type(1.0, 0.0) if "split_k_slices" in kwargs.keys(): self.split_k_mode = split_k_mode self.split_k_slices = kwargs["split_k_slices"] else: self.split_k_mode = cutlass.conv.SplitKMode.Serial self.split_k_slices = 1 #: problem_size self.problem_size: cutlass.conv.Conv2dProblemSize = problem_size self.problem_size.split_k_slices = self.split_k_slices if hasattr(self, "tensor_c_numel"): c_coord = cutlass.conv.implicit_gemm_tensor_c_extent( self.conv_kind, problem_size) if (self.tensor_c_numel == c_coord.at(3) and self.tensor_c_numel < c_coord.size()): self.bias = True # # initialize the argument # self.initialize() # @typechecked def reorder_tensor_B(self, tensor_B: 'np.ndarray', problem_size: 'cutlass.conv.Conv2dProblemSize'): """ Reorder tensor_B for interleaved layout :param tensor_B: input tensor B :type tensor_B: numpy.ndarray :param problem_size: Conv2d problem size :type problem_size: :class:`cutlass.conv.Conv2dProblemSize` :return: reordered tensor B :rtype: numpy.ndarray """ reordered_tensor_B = np.empty_like(tensor_B) tensor_ref_B = self.get_tensor_ref( tensor_B, self.element_B, self.layout_B, problem_size, "b") reordered_tensor_ref_B = self.get_tensor_ref( reordered_tensor_B, self.element_B, self.layout_B, problem_size, "b") cutlass.conv.host.reorder_convK( reordered_tensor_ref_B, tensor_ref_B, self.conv_kind, problem_size) return reordered_tensor_B def get_tensor_ref( self, tensor, dtype, tensor_layout, problem_size, operand): if operand == "a": tensor_coord = cutlass.conv.implicit_gemm_tensor_a_extent( self.conv_kind, problem_size) elif operand == "b": tensor_coord = cutlass.conv.implicit_gemm_tensor_b_extent( self.conv_kind, problem_size) elif operand in ["c", "d"]: tensor_coord = cutlass.conv.implicit_gemm_tensor_c_extent( self.conv_kind, problem_size) else: raise ValueError("unknown operand: " + operand) # Zero stride trick if operand == "c" and self.bias: tensor_coord = cutlass.Tensor4DCoord(0, 0, 0, 0) layout = tensor_layout.packed(tensor_coord) return TensorRef(tensor, dtype, layout).tensor_ref def get_arguments(self, semaphore): ref_A = TensorRef_(self.get_tensor_ref( self.ptr_A, self.element_A, self.layout_A, self.problem_size, "a")) ref_B = TensorRef_(self.get_tensor_ref( self.ptr_B, self.element_B, self.layout_B, self.problem_size, "b")) ref_C = TensorRef_(self.get_tensor_ref( self.ptr_C, self.element_C, self.layout_C, self.problem_size, "c")) ref_D = TensorRef_(self.get_tensor_ref( self.ptr_D, self.element_C, self.layout_C, self.problem_size, "d")) self.c_arguments = self.operation.argument_type( Conv2DProblemSize(self.problem_size), ref_A, ref_B, ref_C, ref_D, self.output_op, self.split_k_mode ) self.semaphore = semaphore def initialize(self): """ Initialize the kernel arguments handling following stuffs 1. get kernel launch configuration including grid, cta size, and dynamic shared memory capacity 2. allocate and initialize device workspace 3. get kernel params as bytearray for NVRTC input """ # get launch configuration self.launch_config = self.operation.rt_module.plan(self) # allocate and initialize device workspace device_workspace_size = \ self.operation.rt_module.get_device_workspace_size(self) if device_workspace_size > 0: self.workspace_buffer = device_mem_alloc(device_workspace_size) workspace_ptr = self.workspace_buffer.ptr err, = cuda.cuMemsetD32( workspace_ptr, 0, device_workspace_size // 4) else: workspace_ptr = None # get kernel params as bytearray semaphore = 0 if workspace_ptr is not None and \ self.split_k_mode == cutlass.conv.SplitKMode.Parallel: self.ptr_D = workspace_ptr elif workspace_ptr is not None and \ self.split_k_mode == cutlass.conv.SplitKMode.Serial: semaphore = workspace_ptr self.get_arguments(semaphore) params_ = self.operation.rt_module.get_args(ctypes.byref( self.c_arguments), ctypes.c_void_p(int(self.semaphore))) self.host_workspace = bytearray(params_.contents) self.device_workspace = None def sync(self): """ Synchronize the arguments. If the input tensor is in host, copy it from device to host. """ return super().sync() # @typechecked class Conv2dRT(ExecutableOperation): """ Conv2dRT manages the CUTLASS runtime components """ KernelTemplate = r''' extern "C" __global__ void ${operation_name}(${operation_name}${operation_suffix}::Params params) { // Dynamic shared memory base pointer extern __shared__ int SharedStorageBase[]; // Declare pointer to dynamic shared memory. ${operation_name}${operation_suffix}::SharedStorage *shared_storage = reinterpret_cast<${operation_name}${operation_suffix}::SharedStorage *>(SharedStorageBase); ${operation_name}${operation_suffix} op; op(params, *shared_storage); } ''' HostTemplate = r''' extern "C" { // Get the size of params in bytes int ${operation_name}_get_param_size(){ return sizeof(${operation_name}${operation_suffix}::Params); } // Get the size of dynamic shared memory in bytes int ${operation_name}_shared_memory_size() { return int(sizeof(${operation_name}${operation_suffix}::SharedStorage)); } // Get the params as byte array char* ${operation_name}_get_params(${operation_name}${operation_suffix}::Arguments* arguments, int *semaphore=nullptr){ typename ${operation_name}${operation_suffix}::Params* params; params = new ${operation_name}${operation_suffix}::Params(*arguments, semaphore); char *bytes = ((char*)(params)); char *output = new char[sizeof(${operation_name}${operation_suffix}::Params)]; for (unsigned int i = 0; i < sizeof(${operation_name}${operation_suffix}::Params); i ++) output[i] = bytes[i]; return output; } } ''' def __init__(self, operation: 'Conv2dOperation'): super().__init__(operation) self.argument_type, self.epilogue_type = get_conv2d_arguments(operation.epilogue_functor) self.argtype = [ctypes.POINTER(self.argument_type), ctypes.c_void_p] self.conv_kind = operation.conv_kind self.operation: Conv2dOperation = operation self.emitter = EmitConv2dInstance('_type') self.threads: int = operation.tile_description.num_threads self.swizzle_functor = operation.swizzling_functor def emit(self): return self.emitter.emit(self.operation) # @typechecked def get_device_workspace_size(self, arguments: Conv2dArguments): workspace_bytes = 0 launch_config = arguments.launch_config self.conv_kind = self.operation.conv_kind if arguments.split_k_mode == cutlass.conv.SplitKMode.Parallel: problem_size = arguments.problem_size workspace_bytes = DataTypeSize[self.operation.C.element] \ * launch_config.grid[2] * cutlass.conv.implicit_gemm_tensor_c_size( self.conv_kind, problem_size ) // 8 elif arguments.split_k_mode == cutlass.conv.SplitKMode.Serial and \ arguments.split_k_slices > 1: workspace_bytes = launch_config.grid[0] * launch_config.grid[1] * 4 return workspace_bytes # @typechecked def plan(self, arguments: Conv2dArguments): tile_size = cutlass.gemm.GemmCoord( self.operation.tile_description.threadblock_shape[0], self.operation.tile_description.threadblock_shape[1], self.operation.tile_description.threadblock_shape[2] ) grid = self.swizzle_functor.get_grid_shape( self.swizzle_functor.get_tiled_shape( self.conv_kind, arguments.problem_size, tile_size, arguments.split_k_slices ) ) return LaunchConfiguration( [grid.x, grid.y, grid.z], [self.threads, 1, 1], self.shared_memory_capacity) def initialize(self): err, = cuda.cuFuncSetAttribute( self.kernel, attrib=cuda.CUfunction_attribute.CU_FUNC_ATTRIBUTE_MAX_DYNAMIC_SHARED_SIZE_BYTES, value=self.shared_memory_capacity) if err != cuda.CUresult.CUDA_SUCCESS: raise RuntimeError('Cuda Error: {}'.format(err)) # class Conv2dOperation: """ CUTLASS Conv2d operation description. :param conv_kind: convolution operator :type conv_kind: :class:`cutlass.conv.Operator` :param iterator_algorithm: Selects among several implementation variants trading off performance with simplicity :type iterator_algorithm: :class:`cutlass.conv.IteratorAlgorithm` :param arch: GPU compute capability (sm_xx) :type arch: int :param tile_description: tile description :type tile_description: :class:`pycutlass.TileDescription` :param A: tensor A description :type A: :class:`pycutlass.TensorDescription` :param B: tensor B description :type B: :class:`pycutlass.TensorDescription` :param C: tensor C description :type C: :class:`pycutlass.TensorDescription` :param D: tensor D description :type D: :class:`pycutlass.TensorDescription` :param element_epilogue: element type for computation in epilogue \ :type element_epilogue: cutlass.int8 | cutlass.int32 | cutlass.float16 | \ cutlass.bfloat16 | cutlass.float32 | cutlass.float64 :param stride_support: distinguish among partial specializations that \ accelerate certain problems where convolution stride is unit \ :type stride_support: :class:`cutlass.conv.StrideSupport` :param epilogue_functor: convolution epilogue functor :type epilogue_functor: :class:`EpilogueFunctor` :param swizzling_functor: threadblock swizzling functor """ # def __init__(self, conv_kind: cutlass.conv.Operator, iterator_algorithm: cutlass.conv.IteratorAlgorithm, arch: int, tile_description: TileDescription, A: TensorDescription, B: TensorDescription, C: TensorDescription, stride_support, epilogue_functor, swizzling_functor=cutlass.IdentitySwizzle1): self.operation_kind: OperationKind = OperationKind.Conv2d self.arch: int = arch self.tile_description: TileDescription = tile_description self.conv_kind = conv_kind self.A: TensorDescription = A self.B: TensorDescription = B self.C: TensorDescription = C self.epilogue_functor = epilogue_functor self.iterator_algorithm = iterator_algorithm self.stride_support = stride_support self.swizzling_functor = swizzling_functor() self.rt_module: Conv2dRT = Conv2dRT(self) self.argument_type = self.rt_module.argument_type self.epilogue_type = self.rt_module.epilogue_type def run(self, arguments: Conv2dArguments) -> cuda.CUresult: """ Launch the cuda kernel with input arguments :param arguments: conv2d arguments :type arguments: :class:`pycutlass.Conv2dArguments` """ # launch the kernel err = self.rt_module.run( arguments.host_workspace, arguments.device_workspace, arguments.launch_config) if err != cuda.CUresult.CUDA_SUCCESS: raise RuntimeError('CUDA Error %s' % str(err)) return err # # Get function name # def procedural_name(self): ''' The full procedural name indicates architecture, extended name, tile size, and layout. ''' return self.configuration_name() # def configuration_name(self): ''' The full procedural name indicates architecture, extended name, tile size, and layout. ''' opcode_class_name = OpcodeClassNames[self.tile_description.math_instruction.opcode_class] threadblock = "%dx%d_%dx%d" % ( self.tile_description.threadblock_shape[0], self.tile_description.threadblock_shape[1], self.tile_description.threadblock_shape[2], self.tile_description.stages ) if self.stride_support == StrideSupport.Unity: configuration_name = "cutlass_sm${arch}_${opcode_class}_${extended_name}_${threadblock}_${layout}_unity_stride_align${alignment}" else: configuration_name = "cutlass_sm${arch}_${opcode_class}_${extended_name}_${threadblock}_${layout}_align${alignment}" return SubstituteTemplate( configuration_name, { 'arch': str(self.arch), 'opcode_class': opcode_class_name, 'extended_name': self.extended_name(), 'threadblock': threadblock, 'layout': self.layout_name(), 'alignment': "%d" % self.A.alignment, } ) # def extended_name(self): ''' Append data types if they differ from compute type. ''' if self.C.element != self.tile_description.math_instruction.element_accumulator and \ self.A.element != self.tile_description.math_instruction.element_accumulator: extended_name = "${element_c}_${core_name}_${element_a}" elif self.C.element == self.tile_description.math_instruction.element_accumulator and \ self.A.element != self.tile_description.math_instruction.element_accumulator: extended_name = "${core_name}_${element_a}" else: extended_name = "${core_name}" extended_name = SubstituteTemplate(extended_name, { 'element_a': DataTypeNames[self.A.element], 'element_c': DataTypeNames[self.C.element], 'core_name': self.core_name() }) return extended_name # def layout_name(self): return "%s" % (ShortLayoutTypeNames[self.A.layout]) # def core_name(self): ''' The basic operation kind is prefixed with a letter indicating the accumulation type. ''' intermediate_type = '' if self.tile_description.math_instruction.opcode_class == cutlass.OpClass.TensorOp: inst_shape = "%dx%dx%d" % tuple( self.tile_description.math_instruction.instruction_shape) if self.tile_description.math_instruction.element_a != self.A.element and \ self.tile_description.math_instruction.element_a != self.accumulator_type(): intermediate_type = DataTypeNames[self.tile_description.math_instruction.element_a] else: inst_shape = '' return "%s%s%s%s_%s" % (ShortDataTypeNames[self.accumulator_type()], inst_shape, intermediate_type, ConvKindNames[self.conv_kind], IteratorAlgorithmNames[self.iterator_algorithm]) # def is_complex(self): complex_operators = [ MathOperation.multiply_add_complex, MathOperation.multiply_add_complex_gaussian ] return self.tile_description.math_instruction.math_operation in complex_operators # def accumulator_type(self): accum = self.tile_description.math_instruction.element_accumulator if self.is_complex(): return get_complex_from_real(accum) return accum ################################################################################################### # # Emits single instances of a CUTLASS device-wide operator # ################################################################################################### class EmitConv2dInstance: def __init__(self, operation_suffix=''): self.operation_suffix = operation_suffix self.includes = [ "cutlass/cutlass.h", "cutlass/conv/kernel/default_conv2d_fprop.h", "cutlass/conv/kernel/default_conv2d_dgrad.h", "cutlass/conv/kernel/default_conv2d_wgrad.h" ] self.template = """ // Conv2d${conv_kind_name} ${iterator_algorithm_name} kernel instance "${operation_name}" using ${operation_name}_base = typename cutlass::conv::kernel::DefaultConv2d${conv_kind_name}< ${element_a}, ${layout_a}, ${element_b}, ${layout_b}, ${element_c}, ${layout_c}, ${element_accumulator}, ${opcode_class}, ${arch}, cutlass::gemm::GemmShape<${threadblock_shape_m}, ${threadblock_shape_n}, ${threadblock_shape_k}>, cutlass::gemm::GemmShape<${warp_shape_m}, ${warp_shape_n}, ${warp_shape_k} >, cutlass::gemm::GemmShape<${instruction_shape_m}, ${instruction_shape_n}, ${instruction_shape_k}>, ${epilogue_functor}, ${swizzling_functor}, // cutlass::gemm::threadblock::GemmSplitKIdentityThreadblockSwizzle<>, ${stages}, ${math_operator}, ${iterator_algorithm}, ${stride_support}, ${align_a}, ${align_b} >::Kernel; struct ${operation_name}${operation_suffix}: public ${operation_name}_base { }; """ def emit(self, operation): warp_shape = [int(operation.tile_description.threadblock_shape[idx] / operation.tile_description.warp_count[idx]) for idx in range(3)] epilogue_vector_length = int(min( operation.C.alignment * DataTypeSize[operation.C.element], 128) / DataTypeSize[operation.C.element]) values = { 'operation_name': operation.procedural_name(), 'operation_suffix': self.operation_suffix, 'conv_kind': ConvKindTag[operation.conv_kind], 'conv_kind_name': ConvKindNames[operation.conv_kind].capitalize(), 'element_a': DataTypeTag[operation.A.element], 'layout_a': LayoutTag[operation.A.layout], 'element_b': DataTypeTag[operation.B.element], 'layout_b': LayoutTag[operation.B.layout], 'element_c': DataTypeTag[operation.C.element], 'layout_c': LayoutTag[operation.C.layout], 'element_accumulator': DataTypeTag[operation.accumulator_type()], 'opcode_class': OpcodeClassTag[operation.tile_description.math_instruction.opcode_class], 'arch': "cutlass::arch::Sm%d" % operation.arch, 'threadblock_shape_m': str(operation.tile_description.threadblock_shape[0]), 'threadblock_shape_n': str(operation.tile_description.threadblock_shape[1]), 'threadblock_shape_k': str(operation.tile_description.threadblock_shape[2]), 'warp_shape_m': str(warp_shape[0]), 'warp_shape_n': str(warp_shape[1]), 'warp_shape_k': str(warp_shape[2]), 'instruction_shape_m': str(operation.tile_description.math_instruction.instruction_shape[0]), 'instruction_shape_n': str(operation.tile_description.math_instruction.instruction_shape[1]), 'instruction_shape_k': str(operation.tile_description.math_instruction.instruction_shape[2]), 'epilogue_vector_length': str(epilogue_vector_length), 'epilogue_functor': operation.epilogue_functor.emit(), 'swizzling_functor': operation.swizzling_functor.tag(), 'stages': str(operation.tile_description.stages), 'iterator_algorithm': IteratorAlgorithmTag[operation.iterator_algorithm], 'iterator_algorithm_name': IteratorAlgorithmNames[operation.iterator_algorithm].capitalize(), 'stride_support': StrideSupportTag[operation.stride_support], 'math_operator': 'cutlass::arch::OpMultiplyAddComplex' if operation.is_complex() else MathOperationTag[operation.tile_description.math_instruction.math_operation], 'align_a': str(operation.A.alignment), 'align_b': str(operation.B.alignment), } return SubstituteTemplate(self.template, values)
################################################################################ # # Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: BSD-3-Clause # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # 3. Neither the name of the copyright holder nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ################################################################################ from typeguard import typechecked import numpy as np try: import torch torch_available = True except ImportError: torch_available = False from cuda import cuda try: import cupy as cp cupy_available = True except ImportError: cupy_available = False import cutlass # @typechecked class TensorRef: """ Python Wrapper for cutlass.TensorRef """ def __init__(self, tensor, dtype, layout) -> None: if isinstance(tensor, np.ndarray): ptr = cuda.CUdeviceptr(tensor.__array_interface__['data'][0]) elif torch_available and isinstance(tensor, torch.Tensor): ptr = cuda.CUdeviceptr(tensor.data_ptr()) elif cupy_available and isinstance(tensor, cp.ndarray): ptr = cuda.CUdeviceptr(int(tensor.data.ptr)) elif isinstance(tensor, cuda.CUdeviceptr): ptr = tensor elif isinstance(tensor, int): ptr = cuda.CUdeviceptr(tensor) else: raise NotImplementedError(tensor) # the dtype(0) is used to overload between different data types # with the same layout self.tensor_ref = cutlass.get_tensor_ref(int(ptr), dtype(0), layout)
################################################################################ # # Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved # SPDX-License-Identifier: BSD-3-Clause # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # 3. Neither the name of the copyright holder nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ################################################################################ import ctypes from cuda import cuda from pycutlass.utils.device import device_cc from cuda import __version__ as __cuda_version__ _version_splits = [int(x) for x in __cuda_version__.split('.')] supports_cluster_launch = device_cc() >= 90 and (_version_splits[0] > 11 or (_version_splits[0] == 11 and _version_splits[1] >= 8)) ################################################################################ # # Launch configuration # ################################################################################ class LaunchConfiguration: def __init__(self, grid=[1, 1, 1], block=[1, 1, 1], smem=0): self.grid = grid self.block = block self.shared_memory_capacity = smem ################################################################################ # # Base class for an executable operation # # ############################################################################## class ExecutableOperation: ''' ''' def __init__(self, operation): self.operation = operation self.module = None self.kernel = None # def name(self): return self.operation.procedural_name() # def emit(self): return '' # def can_implement(self, configuration, arguments): raise NotImplementedError() # def get_host_workspace_size(self, arguments): raise NotImplementedError() # def get_device_workspace_size(self, arguments): raise NotImplementedError() # def plan(self, arguments): raise NotImplementedError() # def initialize(self, host_workspace, device_workspace, launch_config, arguments, stream=cuda.CUstream(0)): raise NotImplementedError() # def run_with_clusters(self, launch_config, kernel_params, stream=cuda.CUstream(0)): if hasattr(self.operation, 'tile_description') and hasattr(self.operation.tile_description, 'cluster_shape'): attr = cuda.CUlaunchAttribute() attr.value.clusterDim.x, attr.value.clusterDim.y, attr.value.clusterDim.z = self.operation.tile_description.cluster_shape attr.id = cuda.CUstreamAttrID.CU_LAUNCH_ATTRIBUTE_CLUSTER_DIMENSION attrs = [attr] # Allow for non-portable cluster sizes err, = cuda.cuFuncSetAttribute( self.kernel, cuda.CUfunction_attribute.CU_FUNC_ATTRIBUTE_NON_PORTABLE_CLUSTER_SIZE_ALLOWED, 1) if err != cuda.CUresult.CUDA_SUCCESS: return err else: attrs = [] config = cuda.CUlaunchConfig() config.gridDimX, config.gridDimY, config.gridDimZ = launch_config.grid config.blockDimX, config.blockDimY, config.blockDimZ = launch_config.block config.blockDimZ = launch_config.block[2] config.sharedMemBytes = launch_config.shared_memory_capacity config.hStream = stream config.attrs = attrs config.numAttrs = len(attrs) err, = cuda.cuLaunchKernelEx(config, f=self.kernel, kernelParams=kernel_params, extra=0) return err # def run_without_clusters(self, launch_config, kernel_params, stream=cuda.CUstream(0)): err, = cuda.cuLaunchKernel( self.kernel, launch_config.grid[0], launch_config.grid[1], launch_config.grid[2], launch_config.block[0], launch_config.block[1], launch_config.block[2], launch_config.shared_memory_capacity, stream, kernel_params, 0) return err # def run(self, host_workspace, device_workspace, launch_config, stream=cuda.CUstream(0)): cArg = (ctypes.c_char * len(host_workspace) ).from_buffer(host_workspace) packed = (ctypes.c_void_p * 1)() packed[0] = ctypes.addressof(cArg) if supports_cluster_launch: return self.run_with_clusters(launch_config, packed, stream) else: return self.run_without_clusters(launch_config, packed, stream)
################################################################################ # # Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: BSD-3-Clause # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # 3. Neither the name of the copyright holder nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ################################################################################ from typing import Generic, TypeVar from treelib import Tree import numpy as np from pycutlass import * import pycutlass import ast import textwrap import inspect ################################################################################ # Type annotation for input arguments ################################################################################ Ttype = TypeVar("Ttype") Dtype = TypeVar("Dtype") class NDArray(np.ndarray, Generic[Ttype, Dtype]): pass ################################################################################ # Operations ################################################################################ operators = { ast.Add: "Add", ast.Div: "Div", ast.Eq: "Equal", ast.Mult: "Mult" } ################################################################################ # AST Node abstractions ################################################################################ class UnaryNode: cnt = 0 # Concept: this is created by the BinOp Node in python ast def __init__(self, element_accumulator, element_compute, elements_per_access, node, args) -> None: if isinstance(node, BinOpNode): self.op = node.op elif isinstance(node, ast.Call): if isinstance(node.func, ast.Name): self.op = node.func.id elif isinstance(node.func, ast.Attribute): self.op = node.func.value.id else: raise TypeError else: raise TypeError self.tag = "Unary" + self.op + str(UnaryNode.cnt) self.id = self.op + str(UnaryNode.cnt) self.args = args UnaryNode.cnt += 1 self.type = "tensor" self.epilogue_op = getattr(pycutlass, self.op)(element_compute) # data types self.element_accumulator = element_accumulator self.element_compute = element_compute self.elements_per_access = elements_per_access def get_epilogue_node(self, visitors): self.epilogue_node = UnaryOp( self.element_accumulator, self.element_compute, self.elements_per_access, *visitors, self.epilogue_op) def get_argument(self, visitor_args, kwargs): epilogue_ops = [] for arg in self.args: try: epilogue_ops.append(kwargs[arg]) except: epilogue_ops.append(arg) # direct arguments like constant self.argument = self.epilogue_node.argument_type(self.epilogue_op.argument_type(*epilogue_ops), *visitor_args) class BinOpNode: cnt = 0 # Concept: this is created by the BinOp Node in python ast def __init__(self, element_accumulator, element_compute, elements_per_access, node) -> None: self.op = operators[type(node.op)] self.tag = "Binary" + self.op + str(BinOpNode.cnt) self.id = self.op + str(BinOpNode.cnt) self.args = None BinOpNode.cnt += 1 self.type = "tensor" self.epilogue_op = getattr(pycutlass, "Vector"+self.op)(element_compute) # data types self.element_accumulator = element_accumulator self.element_compute = element_compute self.elements_per_access = elements_per_access def get_epilogue_node(self, visitors): self.epilogue_node = BinaryOp( self.element_accumulator, self.element_compute, self.elements_per_access, *visitors, self.epilogue_op) def get_argument(self, visitor_args, kwargs): self.argument = self.epilogue_node.argument_type(self.epilogue_op.argument_type(self.args), *visitor_args) class NameNode: # Concept: this is created by the Name Node in python ast def __init__(self, node) -> None: try: self.id = node.id except: self.id = node.targets[0].id self.tag = self.id class ScalarInputNode(NameNode): # Concept: scalar def __init__(self, node) -> None: super().__init__(node) self.tag = "Scalar:" + self.tag self.type = "scalar" class AccumulatorNode(NameNode): # Concept: VisitorOpAccumulator def __init__(self, element_accumulator, elements_per_access, node) -> None: super().__init__(node) self.tag = "Accum:" + self.tag self.type = "tensor" self.element_accumulator = element_accumulator self.elements_per_access = elements_per_access def get_epilogue_node(self, visitors): self.epilogue_node = AccumulatorOp( self.element_accumulator, self.elements_per_access) def get_argument(self, visitor_args, kwargs): self.argument = self.epilogue_node.argument_type() class TensorInputNode(NameNode): # Concept: VisitorOpTensorInput def __init__(self, element_accumulator, node) -> None: super().__init__(node) self.tag = "TensorInput:" + self.tag self.type = "tensor" self.element_accumulator = element_accumulator def get_epilogue_node(self, *args): self.epilogue_node = TensorInputOp(self.element_accumulator) def get_argument(self, visitor_args, kwargs): self.argument = self.epilogue_node.argument_type( kwargs[self.id + "_ptr"], kwargs["problem_size"][1], kwargs["problem_size"][0] * kwargs["problem_size"][1]) class RowBroadcastNode(NameNode): # Concept: VisitorOpRowBroadcast def __init__(self, element_accumulator, element_fragment, node) -> None: super().__init__(node) # self.tag = "RowBroadcast:" + self.tag self.type = "tensor" self.element_accumulator = element_accumulator self.element_fragment = element_fragment def get_epilogue_node(self, *args): self.epilogue_node = RowBroadcastOp( self.element_accumulator, self.element_fragment) def get_argument(self, visitor_args, kwargs): self.argument = self.epilogue_node.argument_type(kwargs[self.id + "_ptr"], kwargs["problem_size"][1]) class ColumnBroadcastNode(NameNode): # Concept: VisitorOpColumnBroadcast def __init__(self, element_accumulator, element_fragment, node) -> None: super().__init__(node) self.tag = "ColumnBroadcast:" + self.tag self.type = "tensor" self.element_accumulator = element_accumulator self.element_fragment = element_fragment def get_epilogue_node(self, *args): self.epilogue_node = ColumnBroadcastOp( self.element_accumulator, self.element_fragment) def get_argument(self, visitor_args, kwargs): self.argument = self.epilogue_node.argument_type(kwargs[self.id + "_ptr"], kwargs["problem_size"][0]) class TensorOutputNode(NameNode): # Concept: VisitorOpTensorOutput def __init__(self, element_accumulator, node) -> None: super().__init__(node) self.tag = "TensorOutput:" + self.tag self.type = "tensor" self.element_accumulator = element_accumulator def get_epilogue_node(self, visitors): self.epilogue_node = TensorOutputOp(self.element_accumulator, *visitors) def get_argument(self, visitor_args, kwargs): self.argument = self.epilogue_node.argument_type(kwargs[self.id + "_ptr"], kwargs["problem_size"][1], *visitor_args, kwargs["problem_size"][0] * kwargs["problem_size"][1]) class RowReductionNode: # Concept: RowReductionOp def __init__(self, element_accumulator, element_reduction, element_reduction_accumulator, id, factor) -> None: # self.id = id self.tag = "RowReduction:" + self.id self.type = "tensor" self.element_accumulator = element_accumulator self.element_reduction = element_reduction self.element_reduction_accumulator = element_reduction_accumulator self.factor = factor def get_epilogue_node(self, visitors): self.epilogue_node = RowReductionOp( self.element_accumulator, self.element_reduction, self.element_reduction_accumulator, *visitors) def get_batch_stride(self, problem_size): return problem_size[0] * ((problem_size[1] + self.factor - 1) // self.factor) def get_argument(self, visitor_args, kwargs): self.argument = self.epilogue_node.argument_type(kwargs[self.id + "_ptr"], *visitor_args, self.get_batch_stride(kwargs["problem_size"])) class ColumnReductionNode: # Concept: ColumnReductionOp def __init__(self, element_accumulator, element_reduction, element_reduction_accumulator, id, factor) -> None: # self.id = id self.tag = "ColumnReduction:" + self.id self.type = "tensor" self.element_accumulator = element_accumulator self.element_reduction = element_reduction self.element_reduction_accumulator = element_reduction_accumulator self.factor = factor def get_epilogue_node(self, visitors): self.epilogue_node = ColumnReductionOp( self.element_accumulator, self.element_reduction, self.element_reduction_accumulator, *visitors) def get_batch_stride(self, problem_size): return problem_size[1] * ((problem_size[0] + self.factor - 1) // self.factor) def get_argument(self, visitor_args, kwargs): self.argument = self.epilogue_node.argument_type(kwargs[self.id + '_ptr'], *visitor_args, self.get_batch_stride(kwargs["problem_size"])) ################################################################################ # Epilogue parser function ################################################################################ class EpilogueAST(ast.NodeVisitor): def __init__(self, epilogue, tile_description, element_accumulator, elements_per_access, element_compute, element_output) -> None: # self.tile_description = tile_description self.element_accumulator = element_accumulator self.elements_per_access = elements_per_access self.element_compute = element_compute self.element_output = element_output self.epilogue = epilogue self.source = textwrap.dedent(inspect.getsource(epilogue.__call__)) self.ast_tree = ast.parse(self.source) self.epilogue_tree = Tree() # print(ast.dump(self.ast_tree, indent=4)) # For Debug purpose # input arguments self.input_args = {} # return nodes self.returns = [] # reduction source nodes self.reduction_source = {} # stack used to keep the parent node id self.stack = [] # visit the AST self.visit(self.ast_tree) # visit the name node def visit_Name(self, node): # append the return ids into self.returns if self.stack[-1] == "return": self.returns.append(node.id) else: # accum is produced from accumulator node if node.id == "accum": name_node = AccumulatorNode( self.element_accumulator, self.elements_per_access, node) else: # for input nodes if node.id in self.input_args.keys(): type = self.input_args[node.id][0] if type == "tensor": name_node = TensorInputNode(self.element_accumulator, node) elif type == "row": name_node = RowBroadcastNode(self.element_accumulator, self.element_compute, node) elif type == "column": name_node = ColumnBroadcastNode(self.element_accumulator, self.element_compute, node) elif type == "scalar": name_node = ScalarInputNode(node) else: raise ValueError(type) # for output nodes else: name_node = TensorOutputNode(self.element_accumulator, node) self.epilogue_tree.create_node(name_node.tag, name_node.id, data=name_node, parent=self.stack[-1]) def visit_Assign(self, node): pre_assign_node = self.epilogue_tree.get_node(node.targets[0].id) if pre_assign_node is None: # The assign is to a root node # skip the reduction nodes if isinstance(node.value, ast.Call): if isinstance(node.value.func, ast.Name): func_type = node.value.func.id elif isinstance(node.value.func, ast.Attribute): func_type = node.value.func.value.id else: raise TypeError if func_type == 'reduction_op': self.reduction_source[node.value.args[0].id] = [node.value.args[1].value, node.value.args[2].value, node.targets[0].id] return name_node = TensorOutputNode(self.element_accumulator, node) self.epilogue_tree.create_node(name_node.tag, name_node.id, data=name_node) self.stack.append(name_node.id) else: if node.targets[0].id in self.returns or node.targets[0].id in self.reduction_source.keys(): self.stack.append(node.targets[0].id) else: self.stack.append(pre_assign_node.predecessor(self.epilogue_tree.identifier)) self.epilogue_tree.remove_node(node.targets[0].id) # get child tag self.visit(node.value) self.stack.pop() def visit_Call(self, node): if isinstance(node.func, ast.Name): func_type = node.func.id elif isinstance(node.func, ast.Attribute): func_type = node.func.value.id else: raise TypeError if func_type == "reduction_op": self.visit(node.args[0]) else: arg_list = [] for idx, arg in enumerate(node.args): if idx == 0: continue if isinstance(arg, ast.Constant): arg_list.append(arg.value) elif isinstance(arg, ast.Name): arg_list.append(arg.id) else: raise TypeError unary_node = UnaryNode(self.element_accumulator, self.element_compute, self.elements_per_access, node, arg_list) self.epilogue_tree.create_node(unary_node.tag, unary_node.id, parent=self.stack[-1], data=unary_node) self.stack.append(unary_node.id) self.visit(node.args[0]) self.stack.pop() def visit_BinOp(self, node): binop = BinOpNode(self.element_accumulator, self.element_compute, self.elements_per_access, node) self.epilogue_tree.create_node(binop.tag, binop.id, data=binop, parent=self.stack[-1]) self.stack.append(binop.id) self.visit(node.left) self.visit(node.right) self.stack.pop() def visit_Return(self, node): self.stack.append("return") self.visit(node.value) self.stack.pop() # # A function definition def visit_FunctionDef(self, node: ast.FunctionDef): # visit args for arg in node.args.args: if arg.arg == "self": continue if isinstance(arg.annotation, ast.Constant): self.input_args[arg.arg] = [arg.annotation.value, ] # visit the assign in the reverse order for idx in range(len(node.body)): self.visit(node.body[-1-idx]) # # Tree optimization pass # # pass 1: lower Binary to Unary def pass_binary_2_unary(self, tree, nid): node = tree.get_node(nid) if isinstance(node.data, BinOpNode): lhs_node = tree.get_node(node.successors(tree.identifier)[0]) left_type = lhs_node.data.type rhs_node = tree.get_node(node.successors(tree.identifier)[1]) right_type = rhs_node.data.type if left_type == "scalar" and right_type == "tensor": node.data = UnaryNode( self.element_accumulator, self.element_compute, self.elements_per_access, node.data, [lhs_node.data.id,]) node.tag = node.data.tag tree.remove_node(lhs_node.data.id) self.pass_binary_2_unary(tree, rhs_node.data.id) elif left_type == "tensor" and right_type == "scalar": node.data = UnaryNode( self.element_accumulator, self.element_compute, self.elements_per_access, node.data, [rhs_node.id,]) node.tag = node.data.tag tree.remove_node(rhs_node.data.id) self.pass_binary_2_unary(tree, lhs_node.data.id) else: self.pass_binary_2_unary(tree, lhs_node.data.id) self.pass_binary_2_unary(tree, rhs_node.data.id) else: for child in node.successors(tree.identifier): self.pass_binary_2_unary(tree, child) # pass 2: inject reduction nodes def pass_inject_reduction(self, tree, nid): node = tree.get_node(nid) if isinstance(node.data, TensorOutputNode): if node.data.id in self.reduction_source.keys(): direction = self.reduction_source[node.data.id][0] target = self.reduction_source[node.data.id][-1] if direction == 'row': reduction_node = RowReductionNode( self.element_accumulator, self.element_output, self.element_accumulator, target, self.tile_description.threadblock_shape[1]) elif direction == "column": reduction_node = ColumnReductionNode( self.element_accumulator, self.element_output, self.element_accumulator, target, self.tile_description.threadblock_shape[0]) else: raise ValueError(direction) child_nid = node.successors(tree.identifier)[0] # if this output node is injected only for reduction if node.data.id not in self.returns: # get reduction config from disc node.data = reduction_node node.tag = reduction_node.tag self.pass_inject_reduction(tree, child_nid) # if this output node is also a tensor output, inject reduction as its children else: # get child node tree.create_node(reduction_node.tag, reduction_node.id, data=reduction_node, parent=node.data.id) tree.move_node(child_nid, reduction_node.id) child = tree.get_node(child_nid) for grand_child in child.successors(tree.identifier): self.pass_inject_reduction(tree, grand_child) else: for child in node.successors(tree.identifier): self.pass_inject_reduction(tree, child) else: for child in node.successors(tree.identifier): self.pass_inject_reduction(tree, child) def pass_inject_epilogue_op(self, tree, nid): node = tree.get_node(nid) visitors = [] for child in node.successors(tree.identifier): visitors.append(self.pass_inject_epilogue_op(tree, child)) node.data.get_epilogue_node(visitors) return node.data.epilogue_node def get_arguments(self, tree, nid, kwargs): node = tree.get_node(nid) visitor_args = [] for child in node.successors(tree.identifier): visitor_args.append(self.get_arguments(tree, child, kwargs)) node.data.get_argument(visitor_args, kwargs) return node.data.argument class EpilogueVisitTree: KernelTemplate = """ ${visitor} using ${operation_name}_EpilogueVisitor = cutlass::epilogue::threadblock::EpilogueVisitorGeneric<${visitor_name}>; """ def __init__(self, elementwise_functor, tile_description, element_accumulator, elements_per_access, element_compute, element_output) -> None: # # data types self.tile_description = tile_description self.element_accumulator = element_accumulator self.elements_per_access = elements_per_access self.element_compute = element_compute self.element_output = element_output self.elementwise_functor = elementwise_functor pass def initialize(self): function = EpilogueAST(self, self.tile_description, self.element_accumulator, self.elements_per_access, self.element_compute, self.element_output) # tree = function.epilogue_tree self.tree = tree function.pass_binary_2_unary(self.tree, self.tree.root) function.pass_inject_reduction(self.tree, self.tree.root) function.pass_inject_epilogue_op(self.tree,self.tree.root) visitor = self.tree.get_node(self.tree.root).data.epilogue_node self.visitor = visitor class _Argument(ctypes.Structure): _fields_ = [ ("visitor_arg", visitor.argument_type) ] def __init__(self, **kwargs) -> None: # process input args _kwargs = {} for input_key in function.input_args.keys(): if input_key == "accum": continue if function.input_args[input_key][0] == "scalar": continue # tensor input else: setattr(self, "buffer_tensor_" + input_key, NumpyFrontend.argument(kwargs[input_key], False)) setattr(self, input_key + "_ptr", int(getattr(self, "buffer_tensor_" + input_key).ptr)) _kwargs[input_key+"_ptr"] = getattr(self, input_key + "_ptr") # process the return args for ret in function.returns: setattr(self, "buffer_tensor_" + ret, NumpyFrontend.argument(kwargs[ret], True)) setattr(self, ret + "_ptr", int(getattr(self, "buffer_tensor_" + ret).ptr)) _kwargs[ret+"_ptr"] = getattr(self, ret + "_ptr") setattr(self, "host_tensor_" + ret, kwargs[ret]) _kwargs.update(kwargs) function.get_arguments(tree, tree.root, _kwargs) self.visitor_arg = tree.get_node(tree.root).data.argument def sync(self, stream_sync=True): if stream_sync: err, = cudart.cudaDeviceSynchronize() if err != cuda.CUresult.CUDA_SUCCESS: raise RuntimeError("CUDA Error %s" % str(err)) for ret in function.returns: err, = cuda.cuMemcpyDtoH( getattr(self, "host_tensor_" + ret), cuda.CUdeviceptr(getattr(self, ret + "_ptr")), getattr(self, "host_tensor_" + ret).size * getattr(self, "host_tensor_" + ret).itemsize ) if err != cuda.CUresult.CUDA_SUCCESS: raise RuntimeError("CUDA Error %s" % str(err)) pass self.epilogue_type = _Argument def emit(self, operation): values = { 'visitor': self.visitor.emit(operation), 'operation_name': operation.procedural_name(), 'visitor_name': self.visitor.instance_name } return SubstituteTemplate(self.KernelTemplate, values)
################################################################################ # # Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved # SPDX-License-Identifier: BSD-3-Clause # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # 3. Neither the name of the copyright holder nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ################################################################################ from ast import Num from audioop import mul from pipes import Template import struct from pycutlass.library import DataTypeTag from pycutlass import * import cutlass from scipy.special import erf from pycutlass.c_types import MatrixCoord_ from pycutlass.frontend import NumpyFrontend from cuda import cuda from cuda import cudart dtype2ctype = { cutlass.float16: ctypes.c_uint16, cutlass.float32: ctypes.c_float, cutlass.float64: ctypes.c_double, cutlass.int32: ctypes.c_int32 } ################################################################################################# # # Epilogue Functors # ################################################################################################# class EpilogueFunctorBase: """ Base class for thread-level epilogue functors """ def __init__(self) -> None: pass def emit(self, tag, template_argument): template = """${tag}<${arguments}>""" arguments = "" for idx, arg in enumerate(template_argument): arguments += arg if idx < len(template_argument) - 1: arguments += ", " values = { "tag": tag, "arguments": arguments } return SubstituteTemplate(template, values) class LinearCombination(EpilogueFunctorBase): """ Apply a linear combination operator to an array of elements D = alpha * accumulator + beta * source :param element_output: data type used to load and store tensors :param epilogue_vector_length: number of elements computed per operation. Usually it is 128/sizeof_bits<ElementOutput_>, but we use 64 and 32 sometimes when there are not enough data to store :param element_accumulator: Accumulator data type :param element_epilogue: data type used to compute linear combination """ tag = "cutlass::epilogue::thread::LinearCombination" def __init__( self, element_output, epilogue_vector_length, element_accumulator=None, element_epilogue=None) -> None: # TODO bind ScaleType super().__init__() if element_accumulator is None: element_accumulator = element_output if element_epilogue is None: element_epilogue = element_output self.element_output = element_output self.element_accumulator = element_accumulator self.element_epilogue = element_epilogue self.epilogue_vector_length = epilogue_vector_length self.template_arguments = [ DataTypeTag[element_output], str(epilogue_vector_length), DataTypeTag[element_accumulator], DataTypeTag[element_epilogue] ] # get epilogue output op type c_element_epilogue = dtype2ctype[self.element_epilogue] element_epilogue = self.element_epilogue class _EpilogueOutputOpParams(ctypes.Structure): _fields_ = [ ("alpha_data", ctypes.c_longlong*2), ("beta_data", ctypes.c_longlong*2), ("alpha", c_element_epilogue), ("beta", c_element_epilogue), ("alpha_ptr", ctypes.c_void_p), ("beta_ptr", ctypes.c_void_p), ] def __init__(self, alpha, beta, *args) -> None: self.alpha = element_epilogue(alpha).storage self.beta = element_epilogue(beta).storage self.epilogue_type = _EpilogueOutputOpParams def emit(self): return super().emit(self.tag, self.template_arguments) class LinearCombinationClamp(LinearCombination): """ Applies a linear combination operator to an array of elements then clamps the output before converting to the output element type. D = alpha * accumulator + beta * source + uniform :param element_output: data type used to load and store tensors :param epilogue_vector_length: number of elements computed per operation. Usually it is 128/sizeof_bits<ElementOutput_>, but we use 64 and 32 sometimes when there are not enough data to store :param element_accumulator: Accumulator data type :param element_epilogue: data type used to compute linear combination """ tag = "cutlass::epilogue::thread::LinearCombinationClamp" def __init__( self, element_output, epilogue_vector_length, element_accumulator=None, element_epilogue=None) -> None: # Base constructor super().__init__( element_output, epilogue_vector_length, element_accumulator, element_epilogue) c_element_epilogue = dtype2ctype[self.element_epilogue] element_epilogue = self.element_epilogue class _EpilogueOutputOpParams(ctypes.Structure): _fields_ = [ ("alpha", c_element_epilogue), ("beta", c_element_epilogue), ("alpha_ptr", ctypes.c_void_p), ("beta_ptr", ctypes.c_void_p), ] def __init__(self, alpha, beta, *args) -> None: self.alpha = element_epilogue(alpha).storage self.beta = element_epilogue(beta).storage self.epilogue_type = _EpilogueOutputOpParams class FastLinearCombinationClamp(EpilogueFunctorBase): """ Applies a linear combination operator to an array of elements then clamps the output before converting to the output element type. D = alpha * accumulator + beta * source Note: The below method only when problem_size_K <= 256 for signed int8 gemm or problem_size_K <= 128 for unsigned int8 gemm. The default approach is above. :param element_output: data type used to load and store tensors :param epilogue_vector_length: number of elements computed per operation. Usually it is 128/sizeof_bits<ElementOutput_>, but we use 64 and 32 sometimes when there are not enough data to store """ tag = "cutlass::epilogue::thread::FastLinearCombinationClamp" def __init__(self, element_output, epilogue_vector_length, *args) -> None: super().__init__() self.template_arguments = [ DataTypeTag[element_output], str(epilogue_vector_length) ] self.element_accumulator = cutlass.int32 self.element_epilogue = cutlass.float32 # get epilogue output op c_element_epilogue = dtype2ctype[self.element_epilogue] element_epilogue = self.element_epilogue class _EpilogueOutputOpParams(ctypes.Structure): _fields_ = [ ("alpha", c_element_epilogue), ("beta", c_element_epilogue), ("alpha_ptr", ctypes.c_void_p), ("beta_ptr", ctypes.c_void_p), ] def __init__(self, alpha, beta, *args) -> None: self.alpha = element_epilogue(alpha).storage self.beta = element_epilogue(beta).storage self.epilogue_type = _EpilogueOutputOpParams def emit(self): return super().emit(self.tag, self.template_arguments) class LinearCombinationGeneric(LinearCombination): """ Applies a linear combination operator followed by an activation function to an array of elements. D = activation(alpha * accumulator + beta * source) :param activation_functor: input activation functor :param element_output: data type used to load and store tensors :param epilogue_vector_length: number of elements computed per operation. Usually it is 128/sizeof_bits<ElementOutput_>, but we use 64 and 32 sometimes when there are not enough data to store :param element_accumulator: Accumulator data type :param element_epilogue: data type used to compute linear combination """ tag = "cutlass::epilogue::thread::LinearCombinationGeneric" def __init__( self, activation_functor, element_output, epilogue_vector_length, element_accumulator=None, element_epilogue=None) -> None: super().__init__( element_output, epilogue_vector_length, element_accumulator, element_epilogue) self.template_arguments = [ activation_functor.emit(),] + self.template_arguments self.activation_functor = activation_functor self.element_epilogue = element_epilogue # get epilogue output op self.epilogue_type = self.activation_functor.epilogue_output_op(self.element_epilogue) class ActivationFunctor: """ Base class for frequently used activation functions """ def __init__(self, element_compute) -> None: pass @staticmethod def numpy(x: np.ndarray): raise NotImplementedError() def emit(self): return self.tag @staticmethod def epilogue_output_op(element_epilogue): c_element_epilogue = dtype2ctype[element_epilogue] class _EpilogueOutputOpParams(ctypes.Structure): _fields_ = [ ("alpha", c_element_epilogue), ("beta", c_element_epilogue), ("alpha_ptr", ctypes.c_void_p), ("beta_ptr", ctypes.c_void_p), ] def __init__(self, alpha, beta, *args) -> None: self.alpha = element_epilogue(alpha).storage self.beta = element_epilogue(beta).storage return _EpilogueOutputOpParams # identity operator class identity(ActivationFunctor): def numpy(x: np.ndarray): return x # ReLu operator, class relu(ActivationFunctor): tag = "cutlass::epilogue::thread::ReLu" def __init__(self, element_compute): super().__init__(element_compute) class _Arguments(ctypes.Structure): _fields_ = [ ("threshold", dtype2ctype[element_compute]) ] def __init__(self, threshold=0.) -> None: self.threshold = element_compute(threshold).storage self.argument_type = _Arguments def emit_visitor(self): return "cutlass::ReLUVisitor" @staticmethod def numpy(x: np.ndarray): return np.maximum(x, 0) # Leaky ReLu operator class leaky_relu(ActivationFunctor): tag = "cutlass::epilogue::thread::LeakyReLU" def __init__(self, element_compute) -> None: super().__init__(element_compute) class _Arguments(ctypes.Structure): _fields_ = [ ("leaky_alpha", dtype2ctype[element_compute]) ] def __init__(self, leaky_alpha) -> None: self.leaky_alpha = element_compute(leaky_alpha).storage self.argument_type = _Arguments def emit_visitor(self): return "cutlass::LeakyReLUVisitor" @staticmethod def numpy(x: np.ndarray, leaky_alpha): return np.maximum(x, 0) + np.minimum(x, 0) * leaky_alpha def epilogue_output_op(self, element_epilogue): c_element_epilogue = dtype2ctype[element_epilogue] class _EpilogueOutputOpParams(ctypes.Structure): _fields_ = [ ("alpha", c_element_epilogue), ("beta", c_element_epilogue), ("alpha_ptr", ctypes.c_void_p), ("beta_ptr", ctypes.c_void_p), ("leaky_alpha", c_element_epilogue) ] def __init__(self, alpha, beta, leaky_alpha=0.2, *args) -> None: self.alpha = element_epilogue(alpha).storage self.beta = element_epilogue(beta).storage self.alpha_ptr = 0 self.beta_ptr = 0 self.leaky_alpha = element_epilogue(leaky_alpha).storage return _EpilogueOutputOpParams # Tanh operator class tanh(ActivationFunctor): tag = "cutlass::epilogue::thread::Tanh" def __init__(self, element_compute) -> None: super().__init__(element_compute) class _Arguments(ctypes.Structure): _fields_ = [ ("tmp", ctypes.c_int) ] def __init__(self, *args) -> None: self.tmp = 0 self.argument_type = _Arguments def emit_visitor(self): return "cutlass::TanhVisitor" @staticmethod def numpy(x: np.ndarray): return np.tanh(x) def sigmoid_op(x: np.ndarray): return 1. / (1. + np.exp(-x)) # Sigmoid operator class sigmoid(ActivationFunctor): tag = "cutlass::epilogue::thread::Sigmoid" @staticmethod def numpy(x: np.ndarray): return sigmoid_op(x) # SiLu operator class silu(ActivationFunctor): tag = "cutlass::epilogue::thread::SiLu" @staticmethod def numpy(x: np.ndarray): return x * sigmoid_op(x) # Hardswish operator class hardswish(ActivationFunctor): tag = "cutlass::epilogue::thread::HardSwish" @staticmethod def numpy(x: np.ndarray): relu6 = np.minimum(np.maximum(x + 3., 0), 6.) return x * relu6 / 6. # GELU operator class gelu(ActivationFunctor): tag = "cutlass::epilogue::thread::GELU" @staticmethod def numpy(x: np.ndarray): return 0.5 * x * (1 + erf(x / np.sqrt(2.))) # reduction operator def reduction_op(tensor, direction, math, factor): batch, m, n = tensor.shape if math == "Add": if direction == "row": num_cta_n = (n + factor - 1) // factor reduction = np.transpose( np.sum(tensor.reshape(batch, m, num_cta_n, factor), axis=-1), axes=[0, 2, 1]).flatten() elif direction == "column": num_cta_m = (m + factor - 1) // factor reduction = np.sum( tensor.reshape(batch, num_cta_m, factor, n), axis=-2).flatten() else: raise NotImplementedError return reduction else: raise NotImplementedError # # GELU operator implemented using the taylor series approximation # class GELU_taylor(ActivationFunctor): # tag = "cutlass::epilogue::thread::GELU_taylor" # # Computes backwards pass for GELU operator # class dGELU(ActivationFunctor): # tag = "cutlass::epilogue::thread::dGELU" ################################################################################ # Epilogue Visitor ################################################################################ class LayerNorm(EpilogueFunctorBase): """ Apply a linear combination operator to an array of elements D = alpha * accumulator + beta * source :param element_output: data type used to load and store tensors :param epilogue_vector_length: number of elements computed per operation. Usually it is 128/sizeof_bits<ElementOutput_>, but we use 64 and 32 sometimes when there are not enough data to store :param element_accumulator: Accumulator data type :param element_epilogue: data type used to compute linear combination """ KernelTemplate = """ cutlass::epilogue::threadblock::EpilogueVisitorLayerNorm< cutlass::gemm::GemmShape<${threadblock_shape_m}, ${threadblock_shape_n}, ${threadblock_shape_k}>, ${operation_name}_default::kThreadCount, ${operation_name}_default::Epilogue::OutputTileIterator, ${operation_name}_default::Epilogue::AccumulatorFragmentIterator::AccumulatorTile, ${element_compute}, // element_compute ${element_variance}, // element_variance ${element_mean}, // element_mean ${element_layer_norm_compute}, // element_layer_norm_compute ${epilogue_functor}, ${shifted_k}>; """ headers = ["gemm/gemm_universal_with_visitor.h", "epilogue/epilogue_visitor_with_layernorm.h"] def __init__( self, elementwise_functor, element_variance=None, element_mean=None, element_layer_norm_compute=None, shifted_k=True) -> None: # TODO bind ScaleType super().__init__() self.elementwise_functor = elementwise_functor self.element_compute = elementwise_functor.element_epilogue self.element_output = elementwise_functor.element_output if element_variance is None: self.element_variance = self.element_output if element_mean is None: self.element_mean = self.element_output if element_layer_norm_compute is None: self.element_layer_norm_compute = self.element_compute if shifted_k: self.shifted_k = "true" else: self.shifted_k = "false" # get epilogue output op elementwise_params_type = self.elementwise_functor.epilogue_type class _EpilogueVisitorParams(ctypes.Structure): _fields_ = [ ("element_wise", elementwise_params_type), ("ptr_Variance", ctypes.c_void_p), ("ptr_Mean_", ctypes.c_void_p), ("ptr_Shifted_K_", ctypes.c_void_p), ("extent", MatrixCoord_) ] def __init__(self, elementwise_params, variance, mean, shift_k, extent) -> None: self.element_wise = elementwise_params if isinstance(variance, np.ndarray): self.buffer_variance = NumpyFrontend.argument(variance, False) self.buffer_mean = NumpyFrontend.argument(mean, False) self.buffer_shift_k = NumpyFrontend.argument(shift_k, False) self.ptr_Variance = int(self.buffer_variance.ptr) self.ptr_Mean_ = int(self.buffer_mean.ptr) self.ptr_Shifted_K_ = int(self.buffer_shift_k.ptr) self.extent = MatrixCoord_(extent[0], extent[1]) self.host_variance = variance self.host_mean = mean self.host_shift_k = shift_k def sync(self, stream_sync=True): if stream_sync: err, = cudart.cudaDeviceSynchronize() if err != cuda.CUresult.CUDA_SUCCESS: raise RuntimeError("CUDA Error %s" % str(err)) # if hasattr(self, "host_variance"): err, = cuda.cuMemcpyDtoH( self.host_variance, cuda.CUdeviceptr(self.ptr_Variance), self.host_variance.size * self.host_variance.itemsize) err, = cuda.cuMemcpyDtoH( self.host_mean, cuda.CUdeviceptr(self.ptr_Mean_), self.host_mean.size * self.host_mean.itemsize) err, = cuda.cuMemcpyDtoH( self.host_shift_k, cuda.CUdeviceptr(self.ptr_Shifted_K_), self.host_shift_k.size * self.host_shift_k.itemsize) if err != cuda.CUresult.CUDA_SUCCESS: raise RuntimeError("CUDA Error %s" % str(err)) self.epilogue_type = _EpilogueVisitorParams def emit(self, operation): values = { 'threadblock_shape_m': str(operation.tile_description.threadblock_shape[0]), 'threadblock_shape_n': str(operation.tile_description.threadblock_shape[1]), 'threadblock_shape_k': str(operation.tile_description.threadblock_shape[2]), 'operation_name': operation.procedural_name(), 'element_compute': DataTypeTag[self.element_compute], 'element_variance': DataTypeTag[self.element_variance], 'element_mean': DataTypeTag[self.element_mean], 'element_layer_norm_compute': DataTypeTag[self.element_layer_norm_compute], 'epilogue_functor': self.elementwise_functor.emit(), 'shifted_k': self.shifted_k } return SubstituteTemplate(self.KernelTemplate, values) class AccumulatorOp: Template = """ using ${instance_name} = cutlass::epilogue::threadblock::VisitorOpAccumulator<${element_accumulator}, ${elements_per_access}>; """ counter = 0 def __init__(self, element_accumulator, elements_per_access) -> None: self.element_accumulator = element_accumulator self.elements_per_access = elements_per_access self.instance_name = "AccumulatorOp%d" % AccumulatorOp.counter AccumulatorOp.counter += 1 class _Arguments(ctypes.Structure): _fields_ = [ ("tmp", ctypes.c_int) ] def __init__(self): self.tmp = 0 self.argument_type = _Arguments def emit(self, *args): values = { "instance_name": self.instance_name, "element_accumulator": DataTypeTag[self.element_accumulator], "elements_per_access": str(self.elements_per_access) } return SubstituteTemplate(self.Template, values) class LinearCombinationOp: Template = """ ${visitor_a} ${visitor_b} using ${instance_name} = cutlass::epilogue::threadblock::VisitorOpLinearCombination< ${element_accumulator}, ${element_compute}, ${elements_per_access}, ${visitor_a_name}, ${visitor_b_name}>; """ counter = 0 def __init__(self, element_accumulator, element_compute, elements_per_access, visitor_a, visitor_b) -> None: # self.element_accumulator = element_accumulator self.element_compute = element_compute self.elements_per_access = elements_per_access self.visitor_a = visitor_a self.visitor_b = visitor_b self.instance_name = "LinearCombinationOp%d" % LinearCombinationOp.counter LinearCombinationOp.counter += 1 class _Arguments(ctypes.Structure): _fields_ = [ ("alpha", dtype2ctype[self.element_compute]), ("beta", dtype2ctype[self.element_compute]), ("visitor_a", self.visitor_a.argument_type), ("visitor_b", self.visitor_b.argument_type) ] def __init__(self, alpha, beta, visitor_a_arg, visitor_b_arg) -> None: self.alpha = element_compute(alpha).storage self.beta = element_compute(beta).storage self.visitor_a = visitor_a_arg self.visitor_b = visitor_b_arg self.argument_type = _Arguments def emit(self, operation): values = { "instance_name": self.instance_name, "element_accumulator": DataTypeTag[self.element_accumulator], "element_compute": DataTypeTag[self.element_compute], "elements_per_access": str(self.elements_per_access), "visitor_a_name": self.visitor_a.instance_name, "visitor_b_name": self.visitor_b.instance_name, "visitor_a": self.visitor_a.emit(operation), "visitor_b": self.visitor_b.emit(operation) } return SubstituteTemplate(self.Template, values) class VectorAdd: def __init__(self, *args) -> None: class _Arguments(ctypes.Structure): _fields_ = [ ("tmp", ctypes.c_int) ] def __init__(self, *args) -> None: self.tmp = 0 self.argument_type = _Arguments def emit(self): return "cutlass::VectorAdd" class VectorMult: def __init__(self, *args) -> None: class _Arguments(ctypes.Structure): _fields_ = [ ("tmp", ctypes.c_int) ] def __init__(self, *args) -> None: self.tmp = 0 self.argument_type = _Arguments def emit(self): return "cutlass::VectorMult" class BinaryOp: Template = """ ${visitor_a} ${visitor_b} using ${instance_name} = cutlass::epilogue::threadblock::VisitorOpBinary< ${element_accumulator}, ${element_compute}, ${elements_per_access}, ${visitor_a_name}, ${visitor_b_name}, ${binary_op}>; """ counter = 0 def __init__(self, element_accumulator, element_compute, elements_per_access, visitor_a, visitor_b, binary_op) -> None: # self.element_accumulator = element_accumulator self.element_compute = element_compute self.elements_per_access = elements_per_access self.visitor_a = visitor_a self.visitor_b = visitor_b self.binary_op = binary_op self.instance_name = "BinaryOp%d" % BinaryOp.counter BinaryOp.counter += 1 class _Arguments(ctypes.Structure): _fields_ = [ ("binary_param", binary_op.argument_type), ("visitor_a", self.visitor_a.argument_type), ("visitor_b", self.visitor_b.argument_type) ] def __init__(self, binary_param, visitor_a_arg, visitor_b_arg) -> None: self.binary_param = binary_param self.visitor_a = visitor_a_arg self.visitor_b = visitor_b_arg self.argument_type = _Arguments def emit(self, operation): values = { "instance_name": self.instance_name, "element_accumulator": DataTypeTag[self.element_accumulator], "element_compute": DataTypeTag[self.element_compute], "elements_per_access": str(self.elements_per_access), "visitor_a_name": self.visitor_a.instance_name, "visitor_b_name": self.visitor_b.instance_name, "visitor_a": self.visitor_a.emit(operation), "visitor_b": self.visitor_b.emit(operation), "binary_op": self.binary_op.emit() } return SubstituteTemplate(self.Template, values) class Mult: def __init__(self, element_compute) -> None: class _Arguments(ctypes.Structure): _fields_ = [ ("alpha", dtype2ctype[element_compute]) ] def __init__(self, alpha) -> None: self.alpha = element_compute(alpha).storage self.argument_type = _Arguments def emit_visitor(self): return "cutlass::Mult" class UnaryOp: Template = """ ${visitor} using ${instance_name} = cutlass::epilogue::threadblock::VisitorOpUnary< ${element_accumulator}, ${element_compute}, ${elements_per_access}, ${visitor_name}, ${unary_op}>; """ counter = 0 def __init__(self, element_accumulator, element_compute, elements_per_access, visitor, unary_op) -> None: # self.element_accumulator = element_accumulator self.element_compute = element_compute self.elements_per_access = elements_per_access self.visitor = visitor self.unary_op = unary_op self.instance_name = "UnaryOp%d" % UnaryOp.counter UnaryOp.counter += 1 class _Arguments(ctypes.Structure): _fields_ = [ ("unary_param", unary_op.argument_type), ("visitor_arg", self.visitor.argument_type) ] def __init__(self, unary_param, visitor_arg) -> None: self.unary_param = unary_param self.visitor_arg = visitor_arg self.argument_type = _Arguments def emit(self, operation): values = { "instance_name": self.instance_name, "element_accumulator": DataTypeTag[self.element_accumulator], "element_compute": DataTypeTag[self.element_compute], "elements_per_access": str(self.elements_per_access), "visitor_name": self.visitor.instance_name, "unary_op": self.unary_op.emit_visitor(), "visitor": self.visitor.emit(operation) } return SubstituteTemplate(self.Template, values) class RowBroadcastOp: Template = """ using ${instance_name} = cutlass::epilogue::threadblock::VisitorOpRowBroadcast< ${element_accumulator}, ${element_fragment}, ${input_tile_iterator}>; """ counter = 0 def __init__(self, element_accumulator, element_fragment) -> None: self.element_accumulator = element_accumulator self.element_fragment = element_fragment self.instance_name = "RowBroadcastOp%d" % RowBroadcastOp.counter RowBroadcastOp.counter += 1 class _Arguments(ctypes.Structure): _fields_ = [ ("broadcast_ptr", ctypes.c_void_p), ("batch_stride", ctypes.c_longlong) ] def __init__(self, broadcast_ptr, batch_stride=0): self.broadcast_ptr = int(broadcast_ptr) self.batch_stride = batch_stride self.argument_type = _Arguments def emit(self, operation): values = { "instance_name": self.instance_name, "element_accumulator": DataTypeTag[self.element_accumulator], "element_fragment": DataTypeTag[self.element_fragment], "input_tile_iterator": operation.procedural_name() + "_default::Epilogue::OutputTileIterator" } return SubstituteTemplate(self.Template, values) class ColumnBroadcastOp: Template = """ using ${instance_name} = cutlass::epilogue::threadblock::VisitorOpColumnBroadcast< ${element_accumulator}, ${element_fragment}, ${input_tile_iterator}>; """ counter = 0 def __init__(self, element_accumulator, element_fragment) -> None: self.element_accumulator = element_accumulator self.element_fragment = element_fragment self.instance_name = "ColumnBroadcastOp%d" % ColumnBroadcastOp.counter ColumnBroadcastOp.counter += 1 class _Arguments(ctypes.Structure): _fields_ = [ ("broadcast_ptr", ctypes.c_void_p), ("batch_stride", ctypes.c_longlong) ] def __init__(self, broadcast_ptr, batch_stride=0): self.broadcast_ptr = int(broadcast_ptr) self.batch_stride = batch_stride self.argument_type = _Arguments def emit(self, operation): values = { "instance_name": self.instance_name, "element_accumulator": DataTypeTag[self.element_accumulator], "element_fragment": DataTypeTag[self.element_fragment], "input_tile_iterator": operation.procedural_name() + "_default::Epilogue::OutputTileIterator" } return SubstituteTemplate(self.Template, values) class TensorInputOp: Template = """ using ${instance_name} = cutlass::epilogue::threadblock::VisitorOpTensorInput< ${element_accumulator}, ${input_tile_iterator}>; """ counter = 0 def __init__(self, element_accumulator) -> None: self.element_accumulator = element_accumulator self.instance_name = "TensorInputOp%d" % TensorInputOp.counter TensorInputOp.counter += 1 class _Arguments(ctypes.Structure): _fields_ = [ ("input_ptr", ctypes.c_void_p), ("ldt", ctypes.c_int), ("batch_stride", ctypes.c_longlong) ] def __init__(self, input_ptr, ldt, batch_stride=0) -> None: self.input_ptr = int(input_ptr) self.ldt = ldt self.batch_stride = batch_stride self.argument_type = _Arguments def emit(self, operation): values = { "instance_name": self.instance_name, "element_accumulator": DataTypeTag[self.element_accumulator], "input_tile_iterator": operation.procedural_name() + "_default::Epilogue::OutputTileIterator" } return SubstituteTemplate(self.Template, values) class TensorOutputOp: Template = """ ${visitor} using ${instance_name} = cutlass::epilogue::threadblock::VisitorOpTensorOutput< ${element_accumulator}, ${output_tile_iterator}, ${visitor_name}>; """ counter = 0 def __init__(self, element_accumulator, visitor) -> None: self.element_accumulator = element_accumulator self.visitor = visitor self.instance_name = "TensorOutputOp%d" % TensorOutputOp.counter TensorOutputOp.counter += 1 class _Arguments(ctypes.Structure): _fields_ = [ ("output_ptr", ctypes.c_void_p), ("ldt", ctypes.c_int), ("batch_stride", ctypes.c_longlong), ("visitor_arg", self.visitor.argument_type) ] def __init__(self, output_ptr, ldt, visitor_arg, batch_stride=0) -> None: self.output_ptr = int(output_ptr) self.ldt = int(ldt) self.visitor_arg = visitor_arg self.batch_stride = batch_stride self.argument_type = _Arguments def emit(self, operation): values = { "instance_name": self.instance_name, "element_accumulator": DataTypeTag[self.element_accumulator], "output_tile_iterator": operation.procedural_name() + "_default::Epilogue::OutputTileIterator", "visitor_name": self.visitor.instance_name, "visitor": self.visitor.emit(operation) } return SubstituteTemplate(self.Template, values) class ColumnReductionOp: Template = """ ${visitor} using ${instance_name} = cutlass::epilogue::threadblock::VisitorOpColumnReduction< cutlass::gemm::GemmShape<${threadblock_shape_m}, ${threadblock_shape_n}, ${threadblock_shape_k}>, ${element_accumulator}, ${element_reduction}, ${element_reduction_accumulator}, ${output_tile_iterator}, ${visitor_name}>; """ counter = 0 def __init__(self, element_accumulator, element_reduction, element_reduction_accumulator, visitor) -> None: self.element_accumulator = element_accumulator self.element_reduction = element_reduction self.element_reduction_accumulator = element_reduction_accumulator self.visitor = visitor self.instance_name = "ColumnReductionOp%d" % ColumnReductionOp.counter ColumnReductionOp.counter += 1 class _Arguments(ctypes.Structure): _fields_ = [ ("reduction_ptr", ctypes.c_void_p), ("batch_stride", ctypes.c_longlong), ("visitor_arg", self.visitor.argument_type) ] def __init__(self, reduction_ptr, visitor_arg, batch_stride=0) -> None: self.reduction_ptr = reduction_ptr self.batch_stride = batch_stride self.visitor_arg = visitor_arg self.argument_type = _Arguments def emit(self, operation): values = { "instance_name": self.instance_name, 'threadblock_shape_m': str(operation.tile_description.threadblock_shape[0]), 'threadblock_shape_n': str(operation.tile_description.threadblock_shape[1]), 'threadblock_shape_k': str(operation.tile_description.threadblock_shape[2]), "element_accumulator": DataTypeTag[self.element_accumulator], "element_reduction": DataTypeTag[self.element_reduction], "element_reduction_accumulator": DataTypeTag[self.element_reduction_accumulator], "output_tile_iterator": operation.procedural_name() + "_default::Epilogue::OutputTileIterator", "visitor_name": self.visitor.instance_name, "visitor": self.visitor.emit(operation) } return SubstituteTemplate(self.Template, values) class RowReductionOp: Template = """ ${visitor} using ${instance_name} = cutlass::epilogue::threadblock::VisitorOpRowReduction< cutlass::gemm::GemmShape<${threadblock_shape_m}, ${threadblock_shape_n}, ${threadblock_shape_k}>, ${element_accumulator}, ${element_reduction}, ${element_reduction_accumulator}, ${output_tile_iterator}, ${visitor_name}>; """ counter = 0 def __init__(self, element_accumulator, element_reduction, element_reduction_accumulator, visitor) -> None: self.element_accumulator = element_accumulator self.element_reduction = element_reduction self.element_reduction_accumulator = element_reduction_accumulator self.visitor = visitor self.instance_name = "RowReductionOp%d" % RowReductionOp.counter RowReductionOp.counter += 1 class _Arguments(ctypes.Structure): _fields_ = [ ("reduction_ptr", ctypes.c_void_p), ("batch_stride", ctypes.c_longlong), ("visitor_arg", self.visitor.argument_type) ] def __init__(self, reduction_ptr, visitor_arg, batch_stride=0) -> None: self.reduction_ptr = reduction_ptr self.visitor_arg = visitor_arg self.batch_stride = batch_stride self.argument_type = _Arguments def emit(self, operation): values = { "instance_name": self.instance_name, 'threadblock_shape_m': str(operation.tile_description.threadblock_shape[0]), 'threadblock_shape_n': str(operation.tile_description.threadblock_shape[1]), 'threadblock_shape_k': str(operation.tile_description.threadblock_shape[2]), "element_accumulator": DataTypeTag[self.element_accumulator], "element_reduction": DataTypeTag[self.element_reduction], "element_reduction_accumulator": DataTypeTag[self.element_reduction_accumulator], "output_tile_iterator": operation.procedural_name() + "_default::Epilogue::OutputTileIterator", "visitor_name": self.visitor.instance_name, "visitor": self.visitor.emit(operation) } return SubstituteTemplate(self.Template, values)
################################################################################################# # # Copyright (c) 2017 - 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: BSD-3-Clause # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # 3. Neither the name of the copyright holder nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ################################################################################################# import pycutlass from pycutlass.test.gemm_testbed import getTensorRef, getTensorView, transpose from pycutlass import * import numpy as np import cutlass from bfloat16 import bfloat16 class TestbedGrouped: def __init__(self, operation: GemmOperationGrouped, seed: int = 2080) -> None: pycutlass.compiler.add_module([operation]) self.seed = seed self.operation = operation element_size = DataTypeSize[operation.A.element] self.dtype_A = self.numpy_type(operation.A.element) self.dtype_B = self.numpy_type(operation.B.element) self.dtype_C = self.numpy_type(operation.C.element) self.dtype_D = self.numpy_type(operation.C.element) if element_size == 1: self.scope_max = 1 self.scope_min = 0 elif element_size <= 8: self.scope_max = 1 self.scope_min = -1 elif element_size == 16: self.scope_max = 4 self.scope_min = -4 else: self.scope_max = 8 self.scope_min = -8 #: compute type self.compute_type = operation.epilogue_functor.element_epilogue self.accumulator_type = operation.tile_description.math_instruction.element_accumulator @staticmethod def numpy_type(type): if type == cutlass.float64: return np.float64 elif type == cutlass.float32: return np.float32 elif type == cutlass.float16: return np.float16 elif type == cutlass.bfloat16: return bfloat16 elif type == cutlass.int32: return np.int32 elif type == cutlass.int8: return np.int8 else: raise ValueError("unsupported type: %s" % ShortDataTypeNames[type]) def uniform_init(self, size, dtype): if dtype in [np.float32, np.float16, bfloat16, np.float64]: return np.ceil( np.random.uniform( low=self.scope_min - 0.5, high=self.scope_max - 0.5, size=size).astype(dtype) ) else: return np.random.uniform( low=self.scope_min - 1, high=self.scope_max + 1, size=size).astype(dtype) def print_problem_size(self, p): problem_size = "problem: %d, %d, %d\n" % (p.m(), p.n(), p.k()) print(problem_size) def run(self, problem_count: int, alpha: float = 1.0, beta: float = 0.0) -> bool: assert get_allocated_size( ) == 0, "%d byte of pool memory is not released in previous run" % get_allocated_size() # initialize np.random.seed(self.seed) # generate the problem sizes problem_sizes = [] tensor_As = [] tensor_Bs = [] tensor_Cs = [] tensor_Ds = [] tensor_D_refs = [] for i in range(problem_count): if self.dtype_A == np.int8: if i == 0: problem_size = cutlass.gemm.GemmCoord(48, 16, 32) else: problem_size = cutlass.gemm.GemmCoord( 16 * np.random.randint(0, 64) + 48, 16 * np.random.randint(0, 64) + 48, 16 * np.random.randint(0, 64) + 48 ) else: if i == 0: problem_size = cutlass.gemm.GemmCoord(48, 16, 8) else: problem_size = cutlass.gemm.GemmCoord( 8 * np.random.randint(0, 64) + 24, 8 * np.random.randint(0, 64) + 24, 8 * np.random.randint(0, 64) + 24 ) tensor_As.append( self.uniform_init( size=(problem_size.m() * problem_size.k(),), dtype=self.dtype_A) ) tensor_Bs.append( self.uniform_init( size=(problem_size.n() * problem_size.k(),), dtype=self.dtype_B) ) tensor_Cs.append( self.uniform_init( size=(problem_size.m() * problem_size.n(),), dtype=self.dtype_C) ) tensor_Ds.append( np.zeros( shape=(problem_size.m() * problem_size.n(),), dtype=self.dtype_D ) ) tensor_D_refs.append( np.ones( shape=(problem_size.m() * problem_size.n(),), dtype=self.dtype_D ) ) problem_sizes.append(problem_size) arguments = GemmGroupedArguments( operation=self.operation, problem_sizes=problem_sizes, A=tensor_As, B=tensor_Bs, C=tensor_Cs, D=tensor_Ds, output_op=self.operation.epilogue_type(alpha, beta) ) self.operation.run(arguments) arguments.sync() # # Reference check # alpha = self.compute_type(alpha).value() beta = self.compute_type(beta).value() init_acc = self.accumulator_type(0).value() for idx, problem_size in enumerate(problem_sizes): if self.operation.switched: tensor_ref_A = getTensorRef( tensor_As[idx], problem_size, "a", transpose(self.operation.B.layout)) tensor_ref_B = getTensorRef( tensor_Bs[idx], problem_size, "b", transpose(self.operation.A.layout)) tensor_ref_C = getTensorRef( tensor_Cs[idx], problem_size, "c", transpose(self.operation.C.layout)) tensor_ref_D_ref = getTensorRef( tensor_D_refs[idx], problem_size, "d", transpose(self.operation.C.layout)) else: tensor_ref_A = getTensorRef( tensor_As[idx], problem_size, "a", self.operation.A.layout) tensor_ref_B = getTensorRef( tensor_Bs[idx], problem_size, "b", self.operation.B.layout) tensor_ref_C = getTensorRef( tensor_Cs[idx], problem_size, "c", self.operation.C.layout) tensor_ref_D_ref = getTensorRef( tensor_D_refs[idx], problem_size, "d", self.operation.C.layout) tensor_view_D_ref = getTensorView( tensor_D_refs[idx], problem_size, "d", self.operation.C.layout) cutlass.test.gemm.host.gemm(problem_size, alpha, tensor_ref_A, tensor_ref_B, beta, tensor_ref_C, tensor_ref_D_ref, init_acc) tensor_view_D = getTensorView( tensor_Ds[idx], problem_size, "d", self.operation.C.layout) passed = cutlass.test.gemm.host.equals( tensor_view_D, tensor_view_D_ref) try: assert passed except AssertionError: self.print_problem_size(problem_size) del arguments assert get_allocated_size( ) == 0, "%d byte of pool memory is not released after current run" % get_allocated_size() return passed