Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
Curio
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Jim Wallace
Curio
Commits
e34f6e70
Commit
e34f6e70
authored
1 year ago
by
Mingchung Xia
Browse files
Options
Downloads
Patches
Plain Diff
Moved dictionary operations to separate file
parent
a0dd924c
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!13
HNSW Implementation with Testcases
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
Sources/SwiftNLP/1. Data Collection/HNSWCorpus + Dictionary.swift
+7
-4
7 additions, 4 deletions
...SwiftNLP/1. Data Collection/HNSWCorpus + Dictionary.swift
Sources/SwiftNLP/1. Data Collection/HNSWCorpus.swift
+1
-48
1 addition, 48 deletions
Sources/SwiftNLP/1. Data Collection/HNSWCorpus.swift
with
8 additions
and
52 deletions
Sources/SwiftNLP/1. Data Collection/HNSWCorpus + Dictionary.swift
+
7
−
4
View file @
e34f6e70
//
//
File
.swift
//
//
HNSWCorpus + Dictionary
.swift
//
//
// Created by Mingchung Xia on 2024-02-14.
//
...
...
@@ -42,7 +42,10 @@ extension HNSWCorpus {
return
dictionary
}
private
func
addDocumentVectorPair
(
at
key
:
Int
,
document
:
String
,
vector
:
[
Scalar
])
{
dictionary
[
key
]
=
DocumentVectorPair
(
untokenizedDocument
:
document
,
vector
:
vector
)
func
addDocumentVectorPair
(
at
key
:
Int
,
document
:
String
,
vector
:
[
Scalar
])
{
dictionary
[
key
]
=
DocumentVectorPair
(
untokenizedDocument
:
document
,
vector
:
vector
)
}
}
This diff is collapsed.
Click to expand it.
Sources/SwiftNLP/1. Data Collection/HNSWCorpus.swift
+
1
−
48
View file @
e34f6e70
...
...
@@ -25,7 +25,6 @@ import Foundation
class
HNSWCorpus
<
Scalar
:
BinaryFloatingPoint
&
Codable
>
:
SNLPCorpus
{
// internal var _documentEncoder: ContextFreeEncoder<Scalar>
internal
var
_documentEncoder
:
any
SNLPEncoder
var
zeroes
:
[
Scalar
]
{
_documentEncoder
.
zeroes
as!
[
Scalar
]
}
...
...
@@ -33,7 +32,7 @@ class HNSWCorpus<Scalar: BinaryFloatingPoint & Codable>: SNLPCorpus {
var
count
:
Int
{
encodedDocuments
.
base
.
vectors
.
count
}
// Keeps track of the original document for client code
private
var
dictionary
:
[
Int
:
DocumentVectorPair
]
=
[:]
var
dictionary
:
[
Int
:
DocumentVectorPair
]
=
[:]
// typicalNeighbourhoodSize = 20 is a standard benchmark
init
(
encoding
:
ContextFreeEncoder
<
Scalar
>.
PreComputedEmbeddings
,
scalar
:
Scalar
.
Type
=
Double
.
self
,
...
...
@@ -46,12 +45,6 @@ class HNSWCorpus<Scalar: BinaryFloatingPoint & Codable>: SNLPCorpus {
_documentEncoder
=
encoder
encodedDocuments
=
DeterministicSampleVectorIndex
<
[
Scalar
]
>
(
typicalNeighborhoodSize
:
typicalNeighborhoodSize
)
}
// init(_documentEncoder: ContextFreeEncoder<Scalar>, typicalNeighborhoodSize: Int = 20) {
// self._documentEncoder = _documentEncoder
// zeroes = Array(repeating: Scalar(0), count: 384)
// encodedDocuments = DeterministicSampleVectorIndex<[Scalar]>(typicalNeighborhoodSize: typicalNeighborhoodSize)
// }
// Decodable conformance
// required init(from decoder: Decoder) throws {
...
...
@@ -72,44 +65,4 @@ class HNSWCorpus<Scalar: BinaryFloatingPoint & Codable>: SNLPCorpus {
vector
:
encodedDocuments
.
base
.
vectors
[
key
]
)
}
// @inlinable
// func addVector(_ vector: [Scalar]) {
// encodedDocuments.insert(vector)
// }
}
extension
HNSWCorpus
{
/// This extension is used for the dictionary operations
private
struct
DocumentVectorPair
{
var
untokenizedDocument
:
String
var
vector
:
[
Scalar
]
init
(
untokenizedDocument
:
String
,
vector
:
[
Scalar
])
{
self
.
untokenizedDocument
=
untokenizedDocument
self
.
vector
=
vector
}
}
@inlinable
func
getUntokenizedDocument
(
at
key
:
Int
)
->
String
{
if
let
pair
=
dictionary
[
key
]
{
return
pair
.
untokenizedDocument
}
else
{
fatalError
(
"Key
\(
key
)
not found in HNSW dictionary"
)
}
}
@inlinable
func
getVector
(
at
key
:
Int
)
->
[
Scalar
]
{
if
let
pair
=
dictionary
[
key
]
{
return
pair
.
vector
}
else
{
fatalError
(
"Key
\(
key
)
not found in HNSW dictionary"
)
}
}
private
func
addDocumentVectorPair
(
at
key
:
Int
,
document
:
String
,
vector
:
[
Scalar
])
{
dictionary
[
key
]
=
DocumentVectorPair
(
untokenizedDocument
:
document
,
vector
:
vector
)
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment