# Copyright 2026 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # # The neutral SOMA77 skeleton comes from NVIDIA Kimodo (https://github.com/nv-tlabs/kimodo), Apache-2.0. from dataclasses import dataclass import torch from diffusers.configuration_utils import ConfigMixin, register_to_config from diffusers.models.modeling_utils import ModelMixin from diffusers.utils import BaseOutput @dataclass class GEMXSOMASkeletonOutput(BaseOutput): """ Args: global_rot_mats (`torch.Tensor` of shape `(..., num_joints, 3, 3)`): The rotation of every joint in world space. posed_joints (`torch.Tensor` of shape `(..., num_joints, 3)`): The position of every joint in world space, in meters. """ global_rot_mats: torch.Tensor posed_joints: torch.Tensor class GEMXSOMASkeleton(ModelMixin, ConfigMixin): """The neutral SOMA77 skeleton of Kimodo, Y up and facing +Z at rest. Joints are listed parents first. Its local rotations are the SOMA body pose parameters: the root rotation followed by the 76 body joint rotations.""" @register_to_config def __init__(self, joint_names: list[str], parents: list[int]): super().__init__() self.register_buffer("neutral_joints", torch.zeros(len(joint_names), 3)) def forward( self, local_rot_mats: torch.Tensor, root_positions: torch.Tensor, return_dict: bool = True ) -> GEMXSOMASkeletonOutput | tuple[torch.Tensor, torch.Tensor]: """ Args: local_rot_mats (`torch.Tensor` of shape `(..., num_joints, 3, 3)`): The rotation of every joint relative to its parent. root_positions (`torch.Tensor` of shape `(..., 3)`): The world position of the root joint, in meters. return_dict (`bool`, defaults to `True`): Whether to return a `GEMXSOMASkeletonOutput` instead of a tuple. Returns: `GEMXSOMASkeletonOutput` or a `(global_rot_mats, posed_joints)` tuple. """ neutral_joints = self.neutral_joints.to(local_rot_mats.dtype) global_rot_mats, posed_joints = [], [] for joint, parent in enumerate(self.config.parents): if parent < 0: global_rot_mats.append(local_rot_mats[..., joint, :, :]) posed_joints.append(root_positions) continue offset = neutral_joints[joint] - neutral_joints[parent] global_rot_mats.append(global_rot_mats[parent] @ local_rot_mats[..., joint, :, :]) posed_joints.append(posed_joints[parent] + global_rot_mats[parent] @ offset) global_rot_mats = torch.stack(global_rot_mats, dim=-3) posed_joints = torch.stack(posed_joints, dim=-2) if not return_dict: return (global_rot_mats, posed_joints) return GEMXSOMASkeletonOutput(global_rot_mats=global_rot_mats, posed_joints=posed_joints)