1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
|
"""This file defines an additional layer of abstraction on top of the SARIF OM."""
from __future__ import annotations
import dataclasses
import enum
from typing import Any, FrozenSet, List, Optional, Sequence, Set, Tuple, Type, TypeVar
from torch.onnx._internal.diagnostics.infra import formatter, sarif
class Level(enum.Enum):
"""The level of a diagnostic.
This class is used to represent the level of a diagnostic. The levels are defined
by the SARIF specification, and are not modifiable. For alternative categories,
please use infra.Tag instead.
"""
NONE = "none"
NOTE = "note"
WARNING = "warning"
ERROR = "error"
levels = Level
class Tag(enum.Enum):
"""The tag of a diagnostic. This class can be inherited to define custom tags."""
pass
@dataclasses.dataclass(frozen=True)
class Rule:
id: str
name: str
message_default_template: str
short_description: Optional[str] = None
full_description: Optional[str] = None
help_uri: Optional[str] = None
@classmethod
def from_sarif(cls, **kwargs) -> Rule:
"""Returns a rule from the SARIF reporting descriptor."""
short_description = (
kwargs["short_description"]["text"]
if "short_description" in kwargs
else None
)
full_description = (
kwargs["full_description"]["markdown"]
if "full_description" in kwargs
else None
)
help_uri = kwargs["help_uri"] if "help_uri" in kwargs else None
rule = cls(
id=kwargs["id"],
name=kwargs["name"],
message_default_template=kwargs["message_strings"]["default"]["text"],
short_description=short_description,
full_description=full_description,
help_uri=help_uri,
)
return rule
def sarif(self) -> sarif.ReportingDescriptor:
"""Returns a SARIF reporting descriptor of this Rule."""
short_description = (
sarif.MultiformatMessageString(text=self.short_description)
if self.short_description is not None
else None
)
full_description = (
sarif.MultiformatMessageString(text="", markdown=self.full_description)
if self.full_description is not None
else None
)
return sarif.ReportingDescriptor(
id=self.id,
name=self.name,
short_description=short_description,
full_description=full_description,
help_uri=self.help_uri,
)
@dataclasses.dataclass
class Location:
uri: str
message: str
line: Optional[int] = None
start_column: Optional[int] = None
end_column: Optional[int] = None
def sarif(self) -> sarif.Location:
"""Returns the SARIF representation of this location."""
return sarif.Location(
physical_location=sarif.PhysicalLocation(
artifact_location=sarif.ArtifactLocation(uri=self.uri),
region=sarif.Region(
start_line=self.line,
start_column=self.start_column,
end_line=self.line,
end_column=self.end_column,
),
),
message=sarif.Message(text=self.message),
)
@dataclasses.dataclass
class Stack:
frame_locations: List[Location] = dataclasses.field(default_factory=list)
def sarif(self) -> sarif.Stack:
"""Returns the SARIF representation of this stack."""
return sarif.Stack(
frames=[
sarif.StackFrame(location=loc.sarif()) for loc in self.frame_locations
]
)
def add_frame(
self,
uri: str,
message: str,
line: Optional[int] = None,
start_column: Optional[int] = None,
end_column: Optional[int] = None,
) -> None:
"""Adds a frame to the stack."""
self.frame_locations.append(
Location(
uri=uri,
message=message,
line=line,
start_column=start_column,
end_column=end_column,
)
)
# This is a workaround for mypy not supporting Self from typing_extensions.
_Diagnostic = TypeVar("_Diagnostic", bound="Diagnostic")
@dataclasses.dataclass
class Diagnostic:
rule: Rule
level: Level
message_args: Optional[Tuple[Any, ...]]
locations: List[Location] = dataclasses.field(default_factory=list)
stacks: List[Stack] = dataclasses.field(default_factory=list)
additional_message: Optional[str] = None
tags: List[Tag] = dataclasses.field(default_factory=list)
def sarif(self) -> sarif.Result:
"""Returns the SARIF Result representation of this diagnostic."""
if self.message_args is None:
self.message_args = tuple()
message = self.rule.message_default_template.format(*self.message_args)
if self.additional_message is not None:
message = f"{message}\n{self.additional_message}"
sarif_result = sarif.Result(
message=sarif.Message(text=message),
level=self.level.value,
rule_id=self.rule.id,
)
sarif_result.locations = [location.sarif() for location in self.locations]
sarif_result.stacks = [stack.sarif() for stack in self.stacks]
sarif_result.properties = sarif.PropertyBag(
tags=[tag.value for tag in self.tags]
)
return sarif_result
def with_location(self: _Diagnostic, location: Location) -> _Diagnostic:
"""Adds a location to the diagnostic."""
self.locations.append(location)
return self
def with_stack(self: _Diagnostic, stack: Stack) -> _Diagnostic:
"""Adds a stack to the diagnostic."""
self.stacks.append(stack)
return self
def with_additional_message(self: _Diagnostic, message: str) -> _Diagnostic:
"""Adds an additional message to the diagnostic."""
if self.additional_message is None:
self.additional_message = message
else:
self.additional_message = f"{self.additional_message}\n{message}"
return self
@dataclasses.dataclass
class RuleCollection:
_rule_id_name_set: FrozenSet[Tuple[str, str]] = dataclasses.field(init=False)
def __post_init__(self) -> None:
self._rule_id_name_set = frozenset(
{
(field.default.id, field.default.name)
for field in dataclasses.fields(self)
if isinstance(field.default, Rule)
}
)
def __contains__(self, rule: Rule) -> bool:
"""Checks if the rule is in the collection."""
return (rule.id, rule.name) in self._rule_id_name_set
@classmethod
def custom_collection_from_list(
cls, new_collection_class_name: str, rules: Sequence[Rule]
) -> RuleCollection:
"""Creates a custom class inherited from RuleCollection with the list of rules."""
return dataclasses.make_dataclass(
new_collection_class_name,
[
(
formatter.kebab_case_to_snake_case(rule.name),
type(rule),
dataclasses.field(default=rule),
)
for rule in rules
],
bases=(cls,),
)()
@dataclasses.dataclass(frozen=True)
class DiagnosticTool:
name: str
version: str
rules: RuleCollection
diagnostic_type: Type[Diagnostic] = dataclasses.field(default=Diagnostic)
_triggered_rules: Set[Rule] = dataclasses.field(init=False, default_factory=set)
def __post_init__(self) -> None:
if not issubclass(self.diagnostic_type, Diagnostic):
raise TypeError(
"Expected diagnostic_type to be a subclass of Diagnostic, "
f"but got {self.diagnostic_type}"
)
def sarif(self) -> sarif.Tool:
"""Returns the SARIF Tool representation."""
return sarif.Tool(
driver=sarif.ToolComponent(
name=self.name,
version=self.version,
rules=[rule.sarif() for rule in self._triggered_rules],
)
)
def create_diagnostic(
self,
rule: Rule,
level: Level,
message_args: Optional[Tuple[Any, ...]],
**kwargs,
) -> Diagnostic:
"""Creates a diagnostic for the given arguments.
Args:
rule: The rule that triggered the diagnostic.
level: The level of the diagnostic.
message_args: The arguments to format the rule's message template.
**kwargs: Additional arguments to pass to the Diagnostic constructor.
Returns:
The created diagnostic.
Raises:
ValueError: If the rule is not supported by the tool.
"""
if rule not in self.rules:
raise ValueError(
f"Rule '{rule.id}:{rule.name}' is not supported by this tool '{self.name} {self.version}'."
f" Supported rules are: {self.rules._rule_id_name_set}"
)
self._triggered_rules.add(rule)
return self.diagnostic_type(rule, level, message_args, **kwargs)
class Invocation:
# TODO: Implement this.
def __init__(self) -> None:
raise NotImplementedError()
@dataclasses.dataclass
class DiagnosticOptions:
"""
Options for diagnostic context.
"""
@dataclasses.dataclass
class DiagnosticContext:
tool: DiagnosticTool
options: Optional[DiagnosticOptions] = None
_diagnostics: List[Diagnostic] = dataclasses.field(init=False, default_factory=list)
_invocation: Invocation = dataclasses.field(init=False)
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
return True
def sarif(self) -> sarif.Run:
"""Returns the SARIF Run object."""
return sarif.Run(
tool=self.tool.sarif(),
results=[diagnostic.sarif() for diagnostic in self._diagnostics],
)
def diagnose(
self,
rule: Rule,
level: Level,
message_args: Optional[Tuple[Any, ...]] = None,
**kwargs,
) -> Diagnostic:
"""Creates a diagnostic for the given arguments.
Args:
rule: The rule that triggered the diagnostic.
level: The level of the diagnostic.
message_args: The arguments to format the rule's message template.
**kwargs: Additional arguments to pass to the Diagnostic constructor.
Returns:
The created diagnostic.
Raises:
ValueError: If the rule is not supported by the tool.
"""
diagnostic = self.tool.create_diagnostic(rule, level, message_args, **kwargs)
self._diagnostics.append(diagnostic)
return diagnostic
|