Skip to content
Open
Show file tree
Hide file tree
Changes from 19 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion library/src/otx/backend/native/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
TVModel,
VisionTransformer,
)
from .detection import ATSS, RTDETR, SSD, YOLOX, DEIMDFine, DFine, RTMDet
from .detection import ATSS, DEIMV2, RTDETR, SSD, YOLOX, DEIMDFine, DFine, RTMDet
from .instance_segmentation import MaskRCNN, MaskRCNNTV, RTMDetInst
from .keypoint_detection import RTMPose
from .segmentation import DinoV2Seg, LiteHRNet, SegNext

__all__ = [
"ATSS",
"DEIMV2",
"RTDETR",
"SSD",
"YOLOX",
Expand Down
18 changes: 10 additions & 8 deletions library/src/otx/backend/native/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ def __init__(

self._label_info = self._dispatch_label_info(label_info)
self.model_name = model_name
self.log_all_losses = False
if isinstance(data_input_params, dict):
data_input_params = DataInputParams(**data_input_params)
elif data_input_params is None:
Expand Down Expand Up @@ -212,14 +213,15 @@ def training_step(self, batch: OTXDataBatch, batch_idx: int) -> Tensor:
)
return train_loss
if isinstance(train_loss, dict):
for k, v in train_loss.items():
self.log(
f"train/{k}",
v,
on_step=True,
on_epoch=False,
prog_bar=True,
)
if self.log_all_losses:
for k, v in train_loss.items():
self.log(
f"train/{k}",
v,
on_step=True,
on_epoch=False,
prog_bar=True,
)

total_train_loss = train_loss.get("total_loss", sum(train_loss.values()))
self.log(
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import torch
from torch import nn

from otx.backend.native.models.common.layers.transformer_layers import ListForwardMixin
from otx.backend.native.models.modules.drop import build_dropout
from otx.backend.native.models.modules.norm import build_norm_layer

Expand Down Expand Up @@ -100,3 +101,52 @@ def __init__(
out_dims=out_dims,
bias=bias,
)


class SwiGLUFFNV2(nn.Module, ListForwardMixin):
"""SwiGLUFFN module.

Args:
in_features (int): Input features.
hidden_features (int | None, optional): Hidden features. Defaults to None.
out_features (int | None, optional): Output features. Defaults to None.
act_layer (Callable[..., nn.Module] | None, optional): Activation layer. Defaults to None.
drop (float, optional): Dropout rate. Defaults to 0.0.
bias (bool, optional): Whether to use bias. Defaults to True.
align_to (int, optional): Number of columns to align the hidden features to. Defaults to 8.
device (torch.device, optional): Device to use. Defaults to None.
"""

def __init__(
self,
in_features: int,
hidden_features: int | None = None,
out_features: int | None = None,
act_layer: Callable[..., nn.Module] | None = None,
drop: float = 0.0,
bias: bool = True,
align_to: int = 8,
device: torch.device | str | None = None,
) -> None:
super().__init__()
out_features = out_features or in_features
hidden_features = hidden_features or in_features
d = int(hidden_features * 2 / 3)
swiglu_hidden_features = d + (-d % align_to)
self.w1 = nn.Linear(in_features, swiglu_hidden_features, bias=bias, device=device)
self.w2 = nn.Linear(in_features, swiglu_hidden_features, bias=bias, device=device)
self.w3 = nn.Linear(swiglu_hidden_features, out_features, bias=bias, device=device)

def forward(self, x: torch.Tensor) -> torch.Tensor:
"""Apply SwiGLU transformation to input tensor.

Args:
x: Input tensor of shape (..., in_features).

Returns:
Output tensor of shape (..., out_features).
"""
x1 = self.w1(x)
x2 = self.w2(x)
hidden = nn.functional.silu(x1) * x2
return self.w3(hidden)
Loading
Loading