Skip to content

Commit d50361e

Browse files
authored
.Net: Use the new TensorPrimitives for numerical operations (microsoft#3113)
### Motivation and Context Fixes microsoft#3021 ### Description Obsoletes the numerical APIs (they should be removed when appropriate) and replaces usage with the new System.Numerics.Tensors.TensorPrimitives type. ### Contribution Checklist - [x] The code builds clean without any errors or warnings - [x] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [x] All unit tests pass, and I have added new tests where possible - [ ] I didn't break anyone 😄
1 parent 9ef8138 commit d50361e

17 files changed

Lines changed: 34 additions & 262 deletions

File tree

‎dotnet/Directory.Packages.props‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<PackageVersion Include="System.Diagnostics.DiagnosticSource" Version="6.0.1" />
1818
<PackageVersion Include="System.Linq.Async" Version="6.0.1" />
1919
<PackageVersion Include="System.Text.Json" Version="6.0.8" />
20+
<PackageVersion Include="System.Numerics.Tensors" Version="8.0.0-rc.2.23479.6" />
2021
<!-- Tokenizers -->
2122
<PackageVersion Include="Microsoft.ML.Tokenizers" Version="0.21.0-preview.23266.6" />
2223
<PackageVersion Include="Microsoft.DeepDev.TokenizerLib" Version="1.3.2" />

‎dotnet/samples/KernelSyntaxExamples/Example25_ReadOnlyMemoryStore.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
using System;
44
using System.Collections.Generic;
55
using System.Linq;
6+
using System.Numerics.Tensors;
67
using System.Runtime.CompilerServices;
78
using System.Runtime.InteropServices;
89
using System.Text.Json;
910
using System.Threading;
1011
using System.Threading.Tasks;
11-
using Microsoft.SemanticKernel.AI.Embeddings.VectorOperations;
1212
using Microsoft.SemanticKernel.Memory;
1313

1414
#pragma warning disable CA2201 // System.Exception is not sufficiently specific - this is a sample
@@ -140,7 +140,7 @@ public IAsyncEnumerable<string> GetCollectionsAsync(CancellationToken cancellati
140140

141141
foreach (var item in this._memoryRecords)
142142
{
143-
double similarity = embedding.Span.CosineSimilarity(item.Embedding.Span);
143+
double similarity = TensorPrimitives.CosineSimilarity(embedding.Span, item.Embedding.Span);
144144
if (similarity >= minRelevanceScore)
145145
{
146146
embeddings.Add(new(item, similarity));

‎dotnet/samples/KernelSyntaxExamples/KernelSyntaxExamples.csproj‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
<PackageReference Include="Microsoft.ML.Tokenizers" />
3131
<PackageReference Include="Microsoft.DeepDev.TokenizerLib" />
3232
<PackageReference Include="System.Linq.Async" />
33+
<PackageReference Include="System.Numerics.Tensors" />
3334
</ItemGroup>
3435
<ItemGroup>
3536
<ProjectReference Include="..\..\src\Connectors\Connectors.AI.OpenAI\Connectors.AI.OpenAI.csproj" />

‎dotnet/src/Connectors/Connectors.Memory.Sqlite/Connectors.Memory.Sqlite.csproj‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
<ItemGroup>
2121
<PackageReference Include="Microsoft.Data.Sqlite" />
22+
<PackageReference Include="System.Numerics.Tensors" />
2223
</ItemGroup>
2324

2425
<ItemGroup>

‎dotnet/src/Connectors/Connectors.Memory.Sqlite/SqliteMemoryStore.cs‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
using System.Collections.Generic;
55
using System.Globalization;
66
using System.Linq;
7+
using System.Numerics.Tensors;
78
using System.Runtime.CompilerServices;
89
using System.Text.Json;
910
using System.Threading;
1011
using System.Threading.Tasks;
1112
using Microsoft.Data.Sqlite;
12-
using Microsoft.SemanticKernel.AI.Embeddings.VectorOperations;
1313
using Microsoft.SemanticKernel.Memory;
1414
using Microsoft.SemanticKernel.Text;
1515

@@ -137,9 +137,7 @@ public async Task RemoveBatchAsync(string collectionName, IEnumerable<string> ke
137137
{
138138
if (record != null)
139139
{
140-
double similarity = embedding
141-
.Span
142-
.CosineSimilarity(record.Embedding.Span);
140+
double similarity = TensorPrimitives.CosineSimilarity(embedding.Span, record.Embedding.Span);
143141
if (similarity >= minRelevanceScore)
144142
{
145143
var entry = withEmbeddings ? record : MemoryRecord.FromMetadata(record.Metadata, ReadOnlyMemory<float>.Empty, record.Key, record.Timestamp);

‎dotnet/src/Connectors/Connectors.UnitTests/Connectors.UnitTests.csproj‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2121
<PrivateAssets>all</PrivateAssets>
2222
</PackageReference>
23+
<PackageReference Include="System.Numerics.Tensors" />
2324

2425
<!-- Because some of the referenced projects have dependencies that themselves have System.Text.Json set with a minimum of 7.0. -->
2526
<PackageReference Include="System.Text.Json" />

‎dotnet/src/Connectors/Connectors.UnitTests/Memory/Redis/RedisMemoryStoreTests.cs‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
using System.Collections.Generic;
55
using System.Collections.Immutable;
66
using System.Linq;
7+
using System.Numerics.Tensors;
78
using System.Runtime.InteropServices;
89
using System.Threading.Tasks;
9-
using Microsoft.SemanticKernel.AI.Embeddings.VectorOperations;
1010
using Microsoft.SemanticKernel.Connectors.Memory.Redis;
1111
using Microsoft.SemanticKernel.Diagnostics;
1212
using Microsoft.SemanticKernel.Memory;
@@ -906,9 +906,7 @@ private void MockSearch(string collection, ReadOnlyMemory<float> compareEmbeddin
906906

907907
foreach (var record in records)
908908
{
909-
double similarity = compareEmbedding
910-
.Span
911-
.CosineSimilarity(record.Embedding.Span);
909+
double similarity = TensorPrimitives.CosineSimilarity(compareEmbedding.Span, record.Embedding.Span);
912910
if (similarity >= threshold)
913911
{
914912
embeddings.Add(new(record, similarity));

‎dotnet/src/Plugins/Plugins.Memory/Plugins.Memory.csproj‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
<ItemGroup>
2020
<PackageReference Include="System.Text.Json" />
21+
<PackageReference Include="System.Numerics.Tensors" />
2122
</ItemGroup>
2223

2324
<ItemGroup>

‎dotnet/src/Plugins/Plugins.Memory/VolatileMemoryStore.cs‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
using System.Collections.Generic;
66
using System.Diagnostics.CodeAnalysis;
77
using System.Linq;
8+
using System.Numerics.Tensors;
89
using System.Runtime.CompilerServices;
910
using System.Threading;
1011
using System.Threading.Tasks;
11-
using Microsoft.SemanticKernel.AI.Embeddings.VectorOperations;
1212
using Microsoft.SemanticKernel.Diagnostics;
1313
using Microsoft.SemanticKernel.Memory;
1414
using Microsoft.SemanticKernel.Plugins.Memory.Collections;
@@ -172,9 +172,7 @@ public Task RemoveBatchAsync(string collectionName, IEnumerable<string> keys, Ca
172172
{
173173
if (record != null)
174174
{
175-
double similarity = embedding
176-
.Span
177-
.CosineSimilarity(record.Embedding.Span);
175+
double similarity = TensorPrimitives.CosineSimilarity(embedding.Span, record.Embedding.Span);
178176
if (similarity >= minRelevanceScore)
179177
{
180178
var entry = withEmbeddings ? record : MemoryRecord.FromMetadata(record.Metadata, ReadOnlyMemory<float>.Empty, record.Key, record.Timestamp);

‎dotnet/src/SemanticKernel.Core/AI/Embeddings/VectorOperations/CosineSimilarityOperation.cs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft. All rights reserved.
22

33
using System;
4+
using System.ComponentModel;
45
using System.Numerics;
56
using System.Runtime.InteropServices;
67

@@ -12,6 +13,8 @@ namespace Microsoft.SemanticKernel.AI.Embeddings.VectorOperations;
1213
/// <remarks>
1314
/// https://en.wikipedia.org/wiki/Cosine_similarity
1415
/// </remarks>
16+
[Obsolete("Numerical operations will be removed in a future release. Use System.Numerics.Tensors.TensorPrimitives instead.")]
17+
[EditorBrowsable(EditorBrowsableState.Never)]
1518
public static class CosineSimilarityOperation
1619
{
1720
/// <summary>

0 commit comments

Comments
 (0)