Skip to content

Commit

Permalink
Merge pull request #178 from snyk/177-environmental-considerations
Browse files Browse the repository at this point in the history
feat(1.6): add environmentalConsiderations
  • Loading branch information
nscuro authored May 26, 2024
2 parents c7405e2 + c877828 commit 1586f07
Show file tree
Hide file tree
Showing 7 changed files with 246 additions and 8 deletions.
13 changes: 13 additions & 0 deletions convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ func componentConverter(specVersion SpecVersion) func(*Component) {
convertHashes(c.Hashes, specVersion)
convertLicenses(c.Licenses, specVersion)
convertEvidence(c, specVersion)
convertModelCard(c, specVersion)

if !specVersion.supportsScope(c.Scope) {
c.Scope = ""
Expand Down Expand Up @@ -328,6 +329,18 @@ func convertOrganizationalEntity(org *OrganizationalEntity, specVersion SpecVers
}
}

func convertModelCard(c *Component, specVersion SpecVersion) {
if c.ModelCard == nil {
return
}

if specVersion < SpecVersion1_6 {
if c.ModelCard.Considerations != nil {
c.ModelCard.Considerations.EnvironmentalConsiderations = nil
}
}
}

func convertVulnerabilities(vulns *[]Vulnerability, specVersion SpecVersion) {
if vulns == nil {
return
Expand Down
19 changes: 19 additions & 0 deletions convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,22 @@ func Test_convertTools_OrganizationalEntity(t *testing.T) {
assert.Nil(t, (*bom.Annotations)[0].Annotator.Service.Provider.Address)
})
}

func Test_convertModelCard(t *testing.T) {
t.Run("spec 1.5 and lower", func(t *testing.T) {
bom := NewBOM()
bom.Metadata = &Metadata{
Component: &Component{
ModelCard: &MLModelCard{
Considerations: &MLModelCardConsiderations{
EnvironmentalConsiderations: &MLModelCardEnvironmentalConsiderations{},
},
},
},
}

bom.convert(SpecVersion1_5)

assert.Nil(t, bom.Metadata.Component.ModelCard.Considerations.EnvironmentalConsiderations)
})
}
84 changes: 78 additions & 6 deletions cyclonedx.go
Original file line number Diff line number Diff line change
Expand Up @@ -985,12 +985,18 @@ type MLModelCard struct {
}

type MLModelCardConsiderations struct {
Users *[]string `json:"users,omitempty" xml:"users>user,omitempty"`
UseCases *[]string `json:"useCases,omitempty" xml:"useCases>useCase,omitempty"`
TechnicalLimitations *[]string `json:"technicalLimitations,omitempty" xml:"technicalLimitations>technicalLimitation,omitempty"`
PerformanceTradeoffs *[]string `json:"performanceTradeoffs,omitempty" xml:"performanceTradeoffs>performanceTradeoff,omitempty"`
EthicalConsiderations *[]MLModelCardEthicalConsideration `json:"ethicalConsiderations,omitempty" xml:"ethicalConsiderations>ethicalConsideration,omitempty"`
FairnessAssessments *[]MLModelCardFairnessAssessment `json:"fairnessAssessments,omitempty" xml:"fairnessAssessments>fairnessAssessment,omitempty"`
Users *[]string `json:"users,omitempty" xml:"users>user,omitempty"`
UseCases *[]string `json:"useCases,omitempty" xml:"useCases>useCase,omitempty"`
TechnicalLimitations *[]string `json:"technicalLimitations,omitempty" xml:"technicalLimitations>technicalLimitation,omitempty"`
PerformanceTradeoffs *[]string `json:"performanceTradeoffs,omitempty" xml:"performanceTradeoffs>performanceTradeoff,omitempty"`
EthicalConsiderations *[]MLModelCardEthicalConsideration `json:"ethicalConsiderations,omitempty" xml:"ethicalConsiderations>ethicalConsideration,omitempty"`
EnvironmentalConsiderations *MLModelCardEnvironmentalConsiderations `json:"environmentalConsiderations,omitempty" xml:"environmentalConsiderations,omitempty"`
FairnessAssessments *[]MLModelCardFairnessAssessment `json:"fairnessAssessments,omitempty" xml:"fairnessAssessments>fairnessAssessment,omitempty"`
}

type MLModelCardEnvironmentalConsiderations struct {
EnergyConsumptions *[]MLModelEnergyConsumption `json:"energyConsumptions,omitempty" xml:"energyConsumptions>energyConsumption,omitempty"`
Properties *[]Property `json:"properties,omitempty" xml:"properties>property,omitempty"`
}

type MLModelCardEthicalConsideration struct {
Expand All @@ -1005,6 +1011,72 @@ type MLModelCardFairnessAssessment struct {
MitigationStrategy string `json:"mitigationStrategy,omitempty" xml:"mitigationStrategy,omitempty"`
}

type MLModelCO2Measure struct {
Value float32 `json:"value" xml:"value"`
Unit MLModelCO2Unit `json:"unit" xml:"unit"`
}

type MLModelCO2Unit string

const MLModelCO2UnitTCO2Eq MLModelCO2Unit = "tCO2eq"

type MLModelEnergyConsumption struct {
Activity MLModelEnergyConsumptionActivity `json:"activity" xml:"activity"`
EnergyProviders *[]MLModelEnergyProvider `json:"energyProviders" xml:"energyProviders"`
ActivityEnergyCost MLModelEnergyMeasure `json:"activityEnergyCost" xml:"activityEnergyCost"`
CO2CostEquivalent *MLModelCO2Measure `json:"co2CostEquivalent,omitempty" xml:"co2CostEquivalent,omitempty"`
CO2CostOffset *MLModelCO2Measure `json:"co2CostOffset,omitempty" xml:"co2CostOffset,omitempty"`
Properties *[]Property `json:"properties,omitempty" xml:"properties>property,omitempty"`
}

type MLModelEnergyConsumptionActivity string

const (
MLModelEnergyConsumptionActivityDesign MLModelEnergyConsumptionActivity = "design"
MLModelEnergyConsumptionActivityDataCollection MLModelEnergyConsumptionActivity = "data-collection"
MLModelEnergyConsumptionActivityDataPreparation MLModelEnergyConsumptionActivity = "data-preparation"
MLModelEnergyConsumptionActivityTraining MLModelEnergyConsumptionActivity = "training"
MLModelEnergyConsumptionActivityFineTuning MLModelEnergyConsumptionActivity = "fine-tuning"
MLModelEnergyConsumptionActivityValidation MLModelEnergyConsumptionActivity = "validation"
MLModelEnergyConsumptionActivityDeployment MLModelEnergyConsumptionActivity = "deployment"
MLModelEnergyConsumptionActivityInference MLModelEnergyConsumptionActivity = "inference"
MLModelEnergyConsumptionActivityOther MLModelEnergyConsumptionActivity = "other"
)

type MLModelEnergyMeasure struct {
Value float32 `json:"value" xml:"value"`
Unit MLModelEnergyUnit `json:"unit" xml:"unit"`
}

type MLModelEnergyProvider struct {
BOMRef string `json:"bom-ref,omitempty" xml:"bom-ref,attr,omitempty"`
Description string `json:"description,omitempty" xml:"description,omitempty"`
Organization *OrganizationalEntity `json:"organization" xml:"organization"`
EnergySource MLModelEnergySource `json:"energySource" xml:"energySource"`
EnergyProvided *MLModelEnergyMeasure `json:"energyProvided" xml:"energyProvided"`
ExternalReferences *[]ExternalReference `json:"externalReferences,omitempty" xml:"externalReferences>reference,omitempty"`
}

type MLModelEnergySource string

const (
MLModelEnergySourceCoal MLModelEnergySource = "coal"
MLModelEnergySourceOil MLModelEnergySource = "oil"
MLModelEnergySourceNaturalGas MLModelEnergySource = "natural-gas"
MLModelEnergySourceNuclear MLModelEnergySource = "nuclear"
MLModelEnergySourceWind MLModelEnergySource = "wind"
MLModelEnergySourceSolar MLModelEnergySource = "solar"
MLModelEnergySourceGeothermal MLModelEnergySource = "geothermal"
MLModelEnergySourceHydropower MLModelEnergySource = "hydropower"
MLModelEnergySourceBiofuel MLModelEnergySource = "biofuel"
MLModelEnergySourceUnknown MLModelEnergySource = "unknown"
MLModelEnergySourceOther MLModelEnergySource = "other"
)

type MLModelEnergyUnit string

const MLModelEnergyUnitKWH MLModelEnergyUnit = "kWh"

type MLModelParameters struct {
Approach *MLModelParametersApproach `json:"approach,omitempty" xml:"approach,omitempty"`
Task string `json:"task,omitempty" xml:"task,omitempty"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,39 @@
"mitigationStrategy": "Strategy used to address this risk"
}
],
"environmentalConsiderations": {
"energyConsumptions": [
{
"activity": "training",
"energyProviders": [
{
"bom-ref": "energy-provider-a",
"description": "An energy provider.",
"organization": {
"name": "Unlimited Energy Inc."
},
"energySource": "other",
"energyProvided": {
"value": 1,
"unit": "kWh"
}
}
],
"activityEnergyCost": {
"value": 1,
"unit": "kWh"
},
"co2CostEquivalent": {
"value": 1,
"unit": "tCO2eq"
},
"co2CostOffset": {
"value": 1,
"unit": "tCO2eq"
}
}
]
},
"fairnessAssessments": [
{
"groupAtRisk": "The groups or individuals at risk of being systematically disadvantaged by the model",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,36 @@
<mitigationStrategy>Strategy used to address this risk</mitigationStrategy>
</ethicalConsideration>
</ethicalConsiderations>
<environmentalConsiderations>
<energyConsumptions>
<energyConsumption>
<activity>training</activity>
<energyProviders bom-ref="energy-provider-a">
<description>An energy provider.</description>
<organization>
<name>Unlimited Energy Inc.</name>
</organization>
<energySource>other</energySource>
<energyProvided>
<value>1</value>
<unit>kWh</unit>
</energyProvided>
</energyProviders>
<activityEnergyCost>
<value>1</value>
<unit>kWh</unit>
</activityEnergyCost>
<co2CostEquivalent>
<value>1</value>
<unit>tCO2eq</unit>
</co2CostEquivalent>
<co2CostOffset>
<value>1</value>
<unit>tCO2eq</unit>
</co2CostOffset>
</energyConsumption>
</energyConsumptions>
</environmentalConsiderations>
<fairnessAssessments>
<fairnessAssessment>
<groupAtRisk>The groups or individuals at risk of being systematically disadvantaged by the model</groupAtRisk>
Expand Down
45 changes: 43 additions & 2 deletions testdata/valid-machine-learning.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,16 @@
"classification": "public"
}
],
"inputs": [ { "format": "string" } ],
"outputs": [ { "format": "byte[]" } ]
"inputs": [
{
"format": "string"
}
],
"outputs": [
{
"format": "byte[]"
}
]
},
"quantitativeAnalysis": {
"performanceMetrics": [
Expand Down Expand Up @@ -77,6 +85,39 @@
"mitigationStrategy": "Strategy used to address this risk"
}
],
"environmentalConsiderations": {
"energyConsumptions": [
{
"activity": "training",
"energyProviders": [
{
"bom-ref": "energy-provider-a",
"description": "An energy provider.",
"organization": {
"name": "Unlimited Energy Inc."
},
"energySource": "other",
"energyProvided": {
"value": 1,
"unit": "kWh"
}
}
],
"activityEnergyCost": {
"value": 1,
"unit": "kWh"
},
"co2CostEquivalent": {
"value": 1,
"unit": "tCO2eq"
},
"co2CostOffset": {
"value": 1,
"unit": "tCO2eq"
}
}
]
},
"fairnessAssessments": [
{
"groupAtRisk": "The groups or individuals at risk of being systematically disadvantaged by the model",
Expand Down
30 changes: 30 additions & 0 deletions testdata/valid-machine-learning.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,36 @@
<mitigationStrategy>Strategy used to address this risk</mitigationStrategy>
</ethicalConsideration>
</ethicalConsiderations>
<environmentalConsiderations>
<energyConsumptions>
<energyConsumption>
<activity>training</activity>
<energyProviders bom-ref="energy-provider-a">
<description>An energy provider.</description>
<organization>
<name>Unlimited Energy Inc.</name>
</organization>
<energySource>other</energySource>
<energyProvided>
<value>1.0</value>
<unit>kWh</unit>
</energyProvided>
</energyProviders>
<activityEnergyCost>
<value>1.0</value>
<unit>kWh</unit>
</activityEnergyCost>
<co2CostEquivalent>
<value>1.0</value>
<unit>tCO2eq</unit>
</co2CostEquivalent>
<co2CostOffset>
<value>1.0</value>
<unit>tCO2eq</unit>
</co2CostOffset>
</energyConsumption>
</energyConsumptions>
</environmentalConsiderations>
<fairnessAssessments>
<fairnessAssessment>
<groupAtRisk>The groups or individuals at risk of being systematically disadvantaged by the model</groupAtRisk>
Expand Down

0 comments on commit 1586f07

Please sign in to comment.