Skip to content

Commit 7539c62

Browse files
author
Denis Deyneko
committed
Adding support for external logger in disk ann index operations.
1 parent 66c4253 commit 7539c62

File tree

6 files changed

+260
-60
lines changed

6 files changed

+260
-60
lines changed

include/logging.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#pragma once
2+
3+
#include "windows_customizations.h"
4+
5+
namespace diskann
6+
{
7+
namespace logging
8+
{
9+
enum LogLevel
10+
{
11+
Debug,
12+
Info,
13+
Status,
14+
Warning,
15+
Error,
16+
Assert
17+
};
18+
19+
// Interface for a logger that can be used to log messages at various levels.
20+
class ILogger
21+
{
22+
public:
23+
virtual ~ILogger() = default;
24+
25+
// Log a message associated with a specific level, title, file name, function, and line number.
26+
virtual void Write(char const *filename,
27+
char const *function,
28+
unsigned lineNumber,
29+
LogLevel level,
30+
char const *title,
31+
char const *message) = 0;
32+
33+
// Abort the program.
34+
virtual void Abort() = 0;
35+
};
36+
37+
DISKANN_DLLEXPORT void RegisterLogger(ILogger *logger);
38+
}
39+
}

nuget/DiskANN-preview.nuspec

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
3+
<metadata>
4+
<id>DiskANN-preview.win-x64</id>
5+
<version>0.13.716.3</version>
6+
<authors>DiskANN team</authors>
7+
<owners>DiskANN team</owners>
8+
<requireLicenseAcceptance>false</requireLicenseAcceptance>
9+
<license type="file">LICENSE</license>
10+
<licenseUrl>https://aka.ms/deprecateLicenseUrl</licenseUrl>
11+
<projectUrl>https://github.com/microsoft/DiskANN</projectUrl>
12+
<description>The package includes Windows x64 libraries
13+
14+
The goal of the project is to build scalable, performant and cost-effective approximate nearest neighbor search algorithms. This release has the code from the DiskANN paper published in NeurIPS 2019, and improvements. This code reuses and builds upon some of the code for NSG algorithm.</description>
15+
<copyright>Copyright (c) Microsoft Corporation</copyright>
16+
<tags>DiskANN ANNS ANN nearest neighbor vector search</tags>
17+
<dependencies>
18+
<dependency id="intelopenmp.redist.win" version="[2022.0.3.3747]" />
19+
</dependencies>
20+
</metadata>
21+
<files>
22+
<file src="x64\Release\diskann.lib" target="lib\native\win-x64\retail" />
23+
<file src="x64\Release\diskann.dll" target="lib\native\win-x64\retail" />
24+
<file src="x64\Release\diskann.pdb" target="lib\native\win-x64\retail" />
25+
<file src="x64\Debug\diskann.lib" target="lib\native\win-x64\debug" />
26+
<file src="x64\Debug\diskann.dll" target="lib\native\win-x64\debug" />
27+
<file src="x64\Debug\diskann.pdb" target="lib\native\win-x64\debug" />
28+
<file src="include\**" target="lib\native\include\diskann" />
29+
<file src="LICENSE" target="LICENSE" />
30+
</files>
31+
</package>

src/distance.cpp

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
#include "distance.h"
1818
#include "utils.h"
19-
#include "logger.h"
19+
#include "logging_internal.h"
2020
#include "ann_exception.h"
2121

2222
namespace diskann
@@ -529,37 +529,33 @@ template <> diskann::Distance<float> *get_distance_function(diskann::Metric m)
529529
{
530530
if (Avx2SupportedCPU)
531531
{
532-
diskann::cout << "L2: Using AVX2 distance computation DistanceL2Float" << std::endl;
532+
Log(logging::Info, "Index", "L2: Using AVX2 distance computation DistanceL2Float");
533533
return new diskann::DistanceL2Float();
534534
}
535535
else if (AvxSupportedCPU)
536536
{
537-
diskann::cout << "L2: AVX2 not supported. Using AVX distance computation" << std::endl;
537+
Log(logging::Info, "Index", "L2: AVX2 not supported. Using AVX distance computation");
538538
return new diskann::AVXDistanceL2Float();
539539
}
540540
else
541541
{
542-
diskann::cout << "L2: Older CPU. Using slow distance computation" << std::endl;
542+
Log(logging::Info, "Index", "L2: Older CPU. Using slow distance computation");
543543
return new diskann::SlowDistanceL2Float();
544544
}
545545
}
546546
else if (m == diskann::Metric::COSINE)
547547
{
548-
diskann::cout << "Cosine: Using either AVX or AVX2 implementation" << std::endl;
548+
Log(logging::Info, "Index", "Cosine: Using either AVX or AVX2 implementation");
549549
return new diskann::DistanceCosineFloat();
550550
}
551551
else if (m == diskann::Metric::INNER_PRODUCT)
552552
{
553-
diskann::cout << "Inner product: Using AVX2 implementation "
554-
"AVXDistanceInnerProductFloat"
555-
<< std::endl;
553+
Log(logging::Info, "Index", "Inner product: Using AVX2 implementation AVXDistanceInnerProductFloat");
556554
return new diskann::AVXDistanceInnerProductFloat();
557555
}
558556
else if (m == diskann::Metric::FAST_L2)
559557
{
560-
diskann::cout << "Fast_L2: Using AVX2 implementation with norm "
561-
"memoization DistanceFastL2<float>"
562-
<< std::endl;
558+
Log(logging::Info, "Index", "Fast_L2: Using AVX2 implementation with norm memoization DistanceFastL2<float>");
563559
return new diskann::DistanceFastL2<float>();
564560
}
565561
else
@@ -568,7 +564,7 @@ template <> diskann::Distance<float> *get_distance_function(diskann::Metric m)
568564
stream << "Only L2, cosine, and inner product supported for floating "
569565
"point vectors as of now."
570566
<< std::endl;
571-
diskann::cerr << stream.str() << std::endl;
567+
Log(logging::Error, "Index", stream.str().c_str());
572568
throw diskann::ANNException(stream.str(), -1, __FUNCSIG__, __FILE__, __LINE__);
573569
}
574570
}
@@ -579,34 +575,30 @@ template <> diskann::Distance<int8_t> *get_distance_function(diskann::Metric m)
579575
{
580576
if (Avx2SupportedCPU)
581577
{
582-
diskann::cout << "Using AVX2 distance computation DistanceL2Int8." << std::endl;
578+
Log(logging::Info, "Index", "Using AVX2 distance computation DistanceL2Int8.");
583579
return new diskann::DistanceL2Int8();
584580
}
585581
else if (AvxSupportedCPU)
586582
{
587-
diskann::cout << "AVX2 not supported. Using AVX distance computation" << std::endl;
583+
Log(logging::Info, "Index", "AVX2 not supported. Using AVX distance computation");
588584
return new diskann::AVXDistanceL2Int8();
589585
}
590586
else
591587
{
592-
diskann::cout << "Older CPU. Using slow distance computation "
593-
"SlowDistanceL2Int<int8_t>."
594-
<< std::endl;
588+
Log(logging::Info, "Index", "Older CPU. Using slow distance computation SlowDistanceL2Int<int8_t>.");
595589
return new diskann::SlowDistanceL2Int<int8_t>();
596590
}
597591
}
598592
else if (m == diskann::Metric::COSINE)
599593
{
600-
diskann::cout << "Using either AVX or AVX2 for Cosine similarity "
601-
"DistanceCosineInt8."
602-
<< std::endl;
594+
Log(logging::Info, "Index", "Using either AVX or AVX2 for Cosine similarity DistanceCosineInt8.");
603595
return new diskann::DistanceCosineInt8();
604596
}
605597
else
606598
{
607599
std::stringstream stream;
608600
stream << "Only L2 and cosine supported for signed byte vectors." << std::endl;
609-
diskann::cerr << stream.str() << std::endl;
601+
Log(logging::Error, "Index", stream.str().c_str());
610602
throw diskann::ANNException(stream.str(), -1, __FUNCSIG__, __FILE__, __LINE__);
611603
}
612604
}
@@ -616,26 +608,24 @@ template <> diskann::Distance<uint8_t> *get_distance_function(diskann::Metric m)
616608
if (m == diskann::Metric::L2)
617609
{
618610
#ifdef _WINDOWS
619-
diskann::cout << "WARNING: AVX/AVX2 distance function not defined for Uint8. Using "
620-
"slow version. "
621-
"Contact gopalsr@microsoft.com if you need AVX/AVX2 support."
622-
<< std::endl;
611+
Log(logging::Warning,
612+
"Index",
613+
"WARNING: AVX/AVX2 distance function not defined for Uint8. Using slow version. Contact gopalsr@microsoft.com if you need AVX/AVX2 support.");
623614
#endif
624615
return new diskann::DistanceL2UInt8();
625616
}
626617
else if (m == diskann::Metric::COSINE)
627618
{
628-
diskann::cout << "AVX/AVX2 distance function not defined for Uint8. Using "
629-
"slow version SlowDistanceCosineUint8() "
630-
"Contact gopalsr@microsoft.com if you need AVX/AVX2 support."
631-
<< std::endl;
619+
Log(logging::Warning,
620+
"Index",
621+
"AVX/AVX2 distance function not defined for Uint8. Using slow version SlowDistanceCosineUint8(). Contact gopalsr@microsoft.com if you need AVX/AVX2 support.");
632622
return new diskann::SlowDistanceCosineUInt8();
633623
}
634624
else
635625
{
636626
std::stringstream stream;
637627
stream << "Only L2 and cosine supported for unsigned byte vectors." << std::endl;
638-
diskann::cerr << stream.str() << std::endl;
628+
Log(logging::Error, "Index", stream.str().c_str());
639629
throw diskann::ANNException(stream.str(), -1, __FUNCSIG__, __FILE__, __LINE__);
640630
}
641631
}

0 commit comments

Comments
 (0)